Multi-level Meta Joke

+/u/CompileBot C

#define META_DEPTH_LEVEL 8

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char* meta_pre = "A guy walks into a bar and asks the bartender for a free drink. The bartender says \"I'll give you a free drink if you can tell me a ";
const char* meta_upper2_add = "multi-level ";
const char* meta_upper_add = "meta ";
const char* meta_base_add = "good ";
const char* meta_mid = "joke.\" So the guy says \"";
const char* meta_post = "\" So the bartender gives him a free beer.";
const char* base_joke = "What do you do when you see a spaceman? You park, man.";

char* meta_joke(int depth) {
    const int base_joke_size = strlen(base_joke);
    const int meta_size = strlen(meta_pre) + strlen(meta_mid)
                          + strlen(meta_post);
    const int base_meta_size = meta_size + strlen(meta_base_add);
    const int upper_meta_size = meta_size + strlen(meta_upper_add);
    const int upper2_meta_size = upper_meta_size + strlen(meta_upper2_add);

    char* joke;
    int joke_size = base_joke_size;

    /* Don't take no negative values. */
    if (depth < 0) {
        return "No.";
    }

    if (depth > 2) {
        joke_size = joke_size + (depth-2) * upper2_meta_size;
    }
    if (depth > 1) {
        joke_size = joke_size + upper_meta_size;
    }
    if (depth > 0) {
        joke_size = joke_size + base_meta_size;
    }

    joke = malloc(joke_size);

    if (depth > 0) {
        strcat(joke, meta_pre);
        if (depth > 2) {
            strcat(joke, meta_upper2_add);
        }
        if (depth > 1) {
            strcat(joke, meta_upper_add);
        } else {
            strcat(joke, meta_base_add);
        }
        strcat(joke, meta_mid);
        strcat(joke, meta_joke(depth-1)); // recursion, whee!
        strcat(joke, meta_post);
    } else {
        strcat(joke, base_joke);
    }

    return joke;
}

int main() {
    printf("%s\n", meta_joke(META_DEPTH_LEVEL));
    return 0;
}
/r/Jokes Thread