// File: /workspace/github_c_makefile_star2_O2/Ziddykins[P]Dawn/src/main.c
// Function: main
int main (int argc, char **argv) {
    //For getops();
    int opt;

    //Default settings for the bot
    //These will be overwritten if a configuration file is found
    //or values are specified on the command line
    char *monsters = "monsters.raw";
    char *port     = "6667";
    char *server   = "irc.dal.net";
    //char *password = "none";
    int mflag, sflag, pflag;
    mflag = sflag = pflag = 0;

    //The bot structure it self
    CALLEXIT(!(dawn = calloc(1, sizeof *dawn)))

    //Initial settings
    strncpy(dawn->nickname, "Dawn-19", MAX_NICK_LENGTH);
    strcpy(dawn->realname, "dongs");
    strcpy(dawn->ident,    "hehe");
    strcpy(dawn->password, "temp");
    strncpy(dawn->active_room, "#stacked", MAX_CHANNEL_LENGTH);

    //Load players
    persistent_load();

    while ((opt = getopt(argc, argv, "hm:n:p:r:s:")) != -1) {
        switch (opt) {
            case 'h': //print help
                print_usage(argv);
                return 0;
            case 'm': //raw monsters file
                CALLEXIT(!(monsters = malloc(strlen(optarg) + 1)))
                strcpy(monsters, optarg);
                mflag = 1;
                break;
            case 'n': //bot's nickname
                strncpy(dawn->nickname, optarg, MAX_NICK_LENGTH-1);
                break;
            case 'p': //server port
                CALLEXIT(!(port = malloc(strlen(optarg) + 1)))
                strcpy(port, optarg);
                pflag = 1;
                break;
            case 'r': //room to join
                dawn->active_room[0] = '#';
                strncpy(dawn->active_room+1, optarg, MAX_CHANNEL_LENGTH-2);
                break;
            case 's': //server to connect to
                CALLEXIT(!(server = malloc(16))) //do we need IPV6 support?
                hostname_to_ip(optarg, server);  //network.c
                sflag = 1;
                break;
            case 'w': //nickserv password
                strncpy(dawn->password, optarg, 64-1);
                break;
            default:
                print_usage(argv);
                return 1;
        }
    }

    FILE *urandom;
    CALLEXIT(!(urandom = fopen("/dev/urandom", "r")))

    unsigned int seed;
    CALLEXIT(!(fread(&seed, sizeof (seed), 1, urandom)))
    srand(seed);
    fclose(urandom);

    FILE *auth_key_file;
    CALLEXIT(!(auth_key_file = fopen("auth_key.txt", "w")))
    CALLEXIT(!(auth_key = calloc(AUTH_KEY_LEN+1, 1)))

    for(size_t i = 0; i < AUTH_KEY_LEN; i++) {
        do {
            auth_key[i] = (char)(rand() % 32);
        } while(auth_key[i] >= 26);

        if (rand()%2) {
            auth_key[i] += 'a';
        } else {
            auth_key[i] += 'A';
        }
    }

    CALLEXIT(!(fwrite(auth_key, AUTH_KEY_LEN, sizeof *auth_key, auth_key_file)))

    fclose(auth_key_file);

    printf(INFO "auth_key: %s\n", auth_key);
    auth_key_valid = 1;

    int match;
    ssize_t len;

    if (access(monsters, F_OK) != -1) {
        FILE *file = fopen(monsters, "r");
        char line[MAX_MESSAGE_BUFFER];
        char name[MAX_MONSTER_LENGTH];
        int count = 0;
        int hp, str, def, intel, mdef, gold, exp, mhp, drop_level;

        if (!file) {
            fprintf(stderr, ERR "Error opening raw monsters\n");
            return 1;
        }
        while (fgets(line, sizeof(line), file)) {
            //TODO: Add ranges to raw files
            if (sscanf(line, "%[^:]:%d:%d:%d:%d:%d:%d:%d:%d:%d",
                    name, &hp, &str, &def, &intel, &mdef, &gold, &exp, &mhp, &drop_level) != 10) {
                fprintf(stderr, ERR "main: Malformed monster file at line %d\n", count);
                return 1;
            }
            strncpy(dawn->monsters[count].name, name, 64);
            dawn->monsters[count].hp         = hp;
            dawn->monsters[count].str        = str;
            dawn->monsters[count].def        = def;
            dawn->monsters[count].intel      = intel;
            dawn->monsters[count].mdef       = mdef;
            dawn->monsters[count].gold       = gold;
            dawn->monsters[count].exp        = exp;
            dawn->monsters[count].mhp        = mhp;
            dawn->monsters[count].drop_level = drop_level;
            dawn->monsters[count].slay_cost  = gold;
            dawn->monsters[count].active     = 0;
            dawn->monsters[count].type       = DEFMON;
            dawn->monsters[count].alignment  = NEUTRAL;
            count++;
        }
        fclose(file);
    }

    dawn->login_sent = 0;
    dawn->in_rooms   = 0;

    init_send_queue();
    init_cmds();

    if (init_connect_server(server, port) == 0) {
        while ((len = recv(con_socket, buffer, MAX_RECV_BUFFER, 0)) != -1) {
            char out[MAX_MESSAGE_BUFFER];
            buffer[len] = '\0';

            //Handle keepalive pings from the server
            if (matches_regex(buffer, "PING :(.*)")) {
                printf("PONG: %s\n", regex_group[1]);
                sprintf(out, "PONG :%s\r\n", regex_group[1]);
                add_msg(out, strlen(out));
            }

            //Connected to the server, send nick/user details
            if (dawn->login_sent == 0) {
                match = matches_regex(buffer, "AUTH") || matches_regex(buffer, "Looking up your hostname...");
                if (match) {
                    printf(INFO "Server checking for AUTH, sending nick/user\n");
                    handle_login(dawn->nickname, dawn->password, dawn->realname, dawn->ident);
                    dawn->login_sent = 1;
                }
            }

            //Nickname is in use, add a random suffix
            if (matches_regex(buffer, ":.*?\\s433\\s*\\s.*")) {
                int rand_suffix = rand() % 10;
                size_t nicklen = strlen(dawn->nickname);

                fprintf(stderr, WARN "Username %s in use\n", dawn->nickname);
                snprintf(dawn->nickname+nicklen, MAX_NICK_LENGTH-nicklen, "%d", rand_suffix);
                handle_login(dawn->nickname, dawn->password, dawn->realname, dawn->ident);
            }


            //If we're logged in and we're received a
            //welcome message, join the rooms
            if (!dawn->in_rooms && dawn->login_sent == 1) {
                match = matches_regex(buffer, ":(.*?)\\s001(.*)");

                if (match) {
                    printf(INFO "Got welcome message from server, joining rooms\n");
                    sprintf(out, "JOIN %s\r\n", dawn->active_room);
                    fluctuate_market();
                    add_msg(out, strlen(out));
                    dawn->in_rooms = 1;
                }
            }

            //Upon joining rooms the bot will search for status 353, which is names, and will
            //iterate through the online people, setting their status to available if they
            //have an account on the bot
            if (dawn->in_rooms) {
                struct Message message;

                //Set lengths based on server rather than assuming
                if (matches_regex(buffer, ":.*?005.*?CHANNELLEN=(\\d+)\\s.*?NICKLEN=(\\d+)")) {
                    MAX_CHANNEL_LENGTH = (unsigned int)strtoul(regex_group[1], 0, 10) + 1;
                    MAX_NICK_LENGTH    = (unsigned int)strtoul(regex_group[2], 0, 10) + 1;
                    dawn->nickname[MAX_NICK_LENGTH] = '\0';
                    dawn->active_room[MAX_CHANNEL_LENGTH] = '\0';
                }

                //Check if user is identified
                if (matches_regex(buffer, ":(.*?)\\s307\\s(.*?)\\s(.*?)\\s:")) {
                    int pindex = get_pindex(regex_group[3]);

                    if (pindex != -1 && (dawn->players[pindex].max_auth > AL_USER || dawn->players[pindex].auth_level > AL_NOAUTH)) {
                        if (dawn->players[pindex].max_auth < AL_REG) { //upgrade
                            dawn->players[pindex].max_auth = AL_REG;
                        }

                        if (dawn->players[pindex].auth_level < dawn->players[pindex].max_auth) {
                            dawn->players[pindex].auth_level = dawn->players[pindex].max_auth;
                            snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s has been verified. (%s)\r\n",
                                dawn->active_room,
                                dawn->players[pindex].username,
                                auth_level_to_str((enum auth_level)(dawn->players[pindex].auth_level)));
                            add_msg(out, strlen(out));
                        } else {
                            snprintf(out, MAX_MESSAGE_BUFFER, "PRIVMSG %s :%s is already verified. (%s)\r\n",
                                dawn->active_room,
                                dawn->players[pindex].username,
                                auth_level_to_str((enum auth_level)(dawn->players[pindex].auth_level)));
                            add_msg(out, strlen(out));
                        }
                    }
                }

                //NAMES (Status 353)
                //This will split up the received users in the room and set
                //their status availability to 1, stripping their name of status symbols
                //and converting their username to lowercase
                if (matches_regex(buffer, ":(.*?)\\s353\\s(.*?)\\s@\\s(.*?)\\s:(.*)")) {
                    char *ch_ptr;
                    ch_ptr = strtok(regex_group[4], " @&+");
                    while (ch_ptr != NULL) {
                        int index = get_pindex(to_lower(ch_ptr));
                        if (index != -1){
                            dawn->players[index].available = 1;
                            dawn->players[index].auth_level = AL_NOAUTH;
                            sprintf(out, "WHOIS %s\r\n", dawn->players[index].username);
                            add_msg(out, strlen(out));
                        }
                        ch_ptr = strtok(NULL, " @&+\r\n");
                    }
                }

                //Kicks
                if (matches_regex(buffer, ":(.*?)!~?(.*?)@(.*?)\\s(\\w+)\\s(.*?)\\s(.*?)\\s:(.*)")) {
                    if (strcmp(regex_group[4], "KICK") == 0) {
                        int index = get_pindex(to_lower(regex_group[6]));
                        if (index != -1) {
                            dawn->players[index].available = 0;
                            dawn->players[index].auth_level = AL_NOAUTH;
                        }
                    }
                }

                //Nick changes
                if (matches_regex(buffer, ":(.*?)!~?.*?@.*?\\sNICK\\s:(.*?)\r\n")) {
                    int oldindex = get_pindex(to_lower(regex_group[1]));
                    int newindex = get_pindex(to_lower(regex_group[2]));
                    if (oldindex != -1) {
                        dawn->players[oldindex].available = 0;
                        dawn->players[oldindex].auth_level = AL_NOAUTH;
                    }
                    if (newindex != -1) {
                        dawn->players[newindex].available = 1;
                        dawn->players[newindex].auth_level = AL_NOAUTH;
                    }
                }

                //Parting and joining
                if (matches_regex(buffer, ":(.*?)!~?(.*?)@(.*?)\\s(.*?)\\s(.*?)")) {
                    int pindex = get_pindex(to_lower(regex_group[1]));
                    if (pindex != -1) {
                        if (strcmp(regex_group[4], "PART") == 0) {
                            dawn->players[pindex].available = 0;
                            dawn->players[pindex].auth_level = AL_NOAUTH;
                        } else if (strcmp(regex_group[4], "JOIN") == 0) {
                            dawn->players[pindex].available = 1;
                            dawn->players[pindex].auth_level = AL_NOAUTH;
                            sprintf(out, "WHOIS %s\r\n", regex_group[1]);
                            add_msg(out, strlen(out));
                        } else if (strcmp(regex_group[4], "QUIT") == 0) {
                            dawn->players[pindex].available = 0;
                        }
                    }
                    if (dawn->player_count == 0) {
                        struct Message temp;
                        strncpy(temp.sender_nick, dawn->nickname, MAX_NICK_LENGTH);
                        strncpy(temp.sender_hostmask, regex_group[3], MAX_HOSTNAME_LENGTH);
                        init_new_character(&temp);
                    }
                }

                //Regular messages and notices
                if (matches_regex(buffer, ":(.*?)!~?(.*?)@(.*?)\\s(.*?)\\s(.*?)\\s:(.*)\r\n")) {
                    strncpy(message.sender_nick,     to_lower(regex_group[1]), MAX_NICK_LENGTH);
                    strncpy(message.sender_ident,    regex_group[2], MAX_IDENT_LENGTH);
                    strncpy(message.sender_hostmask, regex_group[3], MAX_HOSTNAME_LENGTH);
                    strncpy(message.receiver,        regex_group[5], MAX_NICK_LENGTH);
                    strncpy(message.message,         regex_group[6], MAX_MESSAGE_BUFFER);

                    if (strcmp(regex_group[4], "PRIVMSG") == 0) {
                        if (matches_regex(message.message, CMD_LIT)) {
                            if (message.receiver[0] == '#') {
                                invoke_cmd(0, get_pindex(message.sender_nick), regex_group[0], &message, CMD_EXEC);
                            } else {
                                parse_private_message(&message);
                            }
                        }
                    } else if (strcmp(regex_group[4], "NOTICE") == 0) {
                    //    parse_notice_message(&message);
                    }
                }
            }
            bzero(buffer, MAX_MESSAGE_BUFFER);
        }
    } else {
        perror(ERR "main: Failed to connect");
        close(con_socket);
        return 1;
    }

    if (len != -1) {
        close(con_socket);
    }

    if (sflag) {
         free(server);
    }

    if (pflag) {
        free(port);
    }

    if (mflag) {
        free(monsters);
    }

    free_msg_hist_list();
    free_msg_list();
    free_event_list();
    free_cmds();
    free_map();
    free(dawn);

    if (auth_key_valid || auth_key) {
        free(auth_key);
    }

    printf(INFO "Program exiting normally [%s]\n", timestamp());

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1408, %rsp           # imm = 0x1408
movq %rsi, %rbx
movl %edi, %ebp
pushq $0x1
popq %rdi
movl $0xf2eb8, %esi          # imm = 0xF2EB8
callq 0x2110
leaq 0x17f96(%rip), %r13     # 0x1fa80
movq %rax, (%r13)
testq %rax, %rax
je 0x8895
movq %rax, %r14
movl 0x15764(%rip), %edx     # 0x1d264
leaq 0xbcd3(%rip), %rsi      # 0x137da
movq %rax, %rdi
callq 0x20e0
movl $0x676e6f64, 0x40(%r14) # imm = 0x676E6F64
movw $0x73, 0x44(%r14)
xorl %eax, %eax
movb %al, 0x84(%r14)
movl $0x65686568, 0x80(%r14) # imm = 0x65686568
movl $0x0, (%rsp)
movb %al, 0xc4(%r14)
movl $0x706d6574, 0xc0(%r14) # imm = 0x706D6574
addq $0x100, %r14            # imm = 0x100
movl 0x15708(%rip), %edx     # 0x1d260
leaq 0xbc93(%rip), %rsi      # 0x137f2
movq %r14, %rdi
callq 0x20e0
callq 0xc837
leaq 0xbc05(%rip), %rax      # 0x13778
movq %rax, 0x10(%rsp)
leaq 0xbc06(%rip), %rax      # 0x13785
movq %rax, 0x18(%rsp)
leaq 0xbbff(%rip), %rax      # 0x1378a
movq %rax, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
leaq 0xbc5e(%rip), %r14      # 0x137fb
leaq 0xbb94(%rip), %r15      # 0x13738
movq 0x15415(%rip), %r12     # 0x1cfc0
movl $0x0, 0x4(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2060
leal -0x68(%rax), %ecx
cmpl $0xf, %ecx
ja 0x7cdc
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movq (%r13), %rdi
movq (%r12), %rsi
movl 0x15681(%rip), %edx     # 0x1d264
decl %edx
callq 0x20e0
jmp 0x7bb3
movq (%r13), %rdi
movb $0x23, 0x100(%rdi)
addq $0x101, %rdi            # imm = 0x101
movq (%r12), %rsi
movl 0x15658(%rip), %edx     # 0x1d260
addl $-0x2, %edx
callq 0x20e0
jmp 0x7bb3
movq (%r13), %rdi
movl $0xc0, %eax
addq %rax, %rdi
movq (%r12), %rsi
pushq $0x3f
popq %rdx
callq 0x20e0
jmp 0x7bb3
movq (%r12), %r13
movq %r13, %rdi
callq 0x20c0
leaq 0x1(%rax), %rdi
callq 0x22c0
testq %rax, %rax
je 0x88a7
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x21c0
movl $0x0, 0x8(%rsp)
leaq 0x17e17(%rip), %r13     # 0x1fa80
jmp 0x7bb3
movq (%r12), %r13
movq %r13, %rdi
callq 0x20c0
leaq 0x1(%rax), %rdi
callq 0x22c0
testq %rax, %rax
je 0x88b0
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x21c0
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
leaq 0x17dd6(%rip), %r13     # 0x1fa80
jmp 0x7bb3
pushq $0x10
popq %rdi
callq 0x22c0
testq %rax, %rax
je 0x889e
movq (%r12), %rdi
movq %rax, 0x20(%rsp)
movq %rax, %rsi
callq 0xbf93
pushq $0x1
popq %rax
movl %eax, (%rsp)
jmp 0x7bb3
cmpl $-0x1, %eax
jne 0x7db8
leaq 0xbbe6(%rip), %rdi      # 0x138d2
leaq 0xb6d0(%rip), %rsi      # 0x133c3
callq 0x2240
testq %rax, %rax
je 0x88b9
movq %rax, %rbx
leaq 0x44(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2330
testq %rax, %rax
je 0x88c2
movl 0x44(%rsp), %edi
callq 0x21d0
movq %rbx, %rdi
callq 0x21f0
leaq 0xbc3e(%rip), %rdi      # 0x13976
leaq 0xba44(%rip), %rsi      # 0x13783
callq 0x2240
testq %rax, %rax
je 0x88cb
movq %rax, %rbx
pushq $0x19
popq %rdi
pushq $0x1
popq %rsi
callq 0x2110
movq %rax, 0x15526(%rip)     # 0x1d288
testq %rax, %rax
je 0x88d4
xorl %r14d, %r14d
pushq $0x20
popq %rbp
cmpq $0x18, %r14
je 0x7dd7
callq 0x2080
cltd
idivl %ebp
movq 0x15502(%rip), %rax     # 0x1d288
movb %dl, (%rax,%r14)
movq 0x154f7(%rip), %rax     # 0x1d288
cmpb $0x19, (%rax,%r14)
jg 0x7d77
callq 0x2080
movq 0x154e4(%rip), %rcx     # 0x1d288
andb $0x1, %al
shlb $0x5, %al
addb (%rcx,%r14), %al
addb $0x41, %al
movb %al, (%rcx,%r14)
incq %r14
jmp 0x7d71
movq %rbx, %rdi
callq 0x88f1
pushq $0x1
popq %rbx
jmp 0x8822
movq %rbx, %rdi
callq 0x88f1
xorl %ebx, %ebx
jmp 0x8822
movq 0x154aa(%rip), %rdi     # 0x1d288
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2480
testq %rax, %rax
je 0x88dd
movq %rbx, %rdi
callq 0x21f0
movq 0x15484(%rip), %rsi     # 0x1d288
leaq 0xbc78(%rip), %rdi      # 0x13a83
xorl %eax, %eax
callq 0x2040
leaq 0x15467(%rip), %rax     # 0x1d280
movl $0x1, (%rax)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x23d0
cmpl $-0x1, %eax
je 0x7f80
leaq 0xb588(%rip), %rsi      # 0x133c3
movq 0x10(%rsp), %rdi
callq 0x2240
testq %rax, %rax
je 0x8855
movq %rax, %rbx
movl $0xf1b40, %r13d         # imm = 0xF1B40
leaq 0x200(%rsp), %r14
leaq 0xb00(%rsp), %r12
xorl %ebp, %ebp
movq %r14, %rdi
movl $0x190, %esi            # imm = 0x190
movq %rbx, %rdx
callq 0x2390
testq %rax, %rax
je 0x7f71
movq %r14, %rdi
leaq 0xbc47(%rip), %rsi      # 0x13ad3
movq %r12, %rdx
leaq 0x70(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x3c(%rsp), %r9
xorl %eax, %eax
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x34(%rsp), %r10
pushq %r10
leaq 0x40(%rsp), %r10
pushq %r10
leaq 0x24(%rsp), %r10
pushq %r10
leaq 0x54(%rsp), %r10
pushq %r10
leaq 0x60(%rsp), %r10
pushq %r10
callq 0x21e0
addq $0x30, %rsp
cmpl $0xa, %eax
jne 0x8876
leaq 0x17b9d(%rip), %r15     # 0x1fa80
movq (%r15), %rdi
addq %r13, %rdi
movq %r12, %rsi
pushq $0x40
popq %rdx
callq 0x20e0
movl 0x70(%rsp), %eax
movq (%r15), %rcx
movl %eax, 0x40(%rcx,%r13)
movl 0x40(%rsp), %eax
movl %eax, 0x4c(%rcx,%r13)
movl 0x3c(%rsp), %eax
movl %eax, 0x48(%rcx,%r13)
movl 0x38(%rsp), %eax
movl %eax, 0x50(%rcx,%r13)
movl 0x34(%rsp), %eax
movl %eax, 0x54(%rcx,%r13)
movl 0xc(%rsp), %eax
movl %eax, 0x58(%rcx,%r13)
movl 0x30(%rsp), %eax
movl %eax, 0x5c(%rcx,%r13)
movl 0x2c(%rsp), %eax
movl %eax, 0x44(%rcx,%r13)
movl 0x28(%rsp), %eax
movl %eax, 0x60(%rcx,%r13)
movl 0xc(%rsp), %eax
movl %eax, 0x68(%rcx,%r13)
andl $0x0, 0x64(%rcx,%r13)
movabsq $0x200000005, %rax      # imm = 0x200000005
movq %rax, 0x70(%rcx,%r13)
incl %ebp
addq $0x78, %r13
jmp 0x7e69
movq %rbx, %rdi
callq 0x21f0
leaq 0x17b00(%rip), %r13     # 0x1fa80
movq (%r13), %rax
andq $0x0, 0x140(%rax)
callq 0xba14
callq 0x3f60
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0xbaa5
testl %eax, %eax
jne 0x8836
movl $0x80, %eax
leaq 0xb00(%rsp), %rcx
addq %rax, %rcx
movq %rcx, 0x60(%rsp)
leaq 0x200(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
leaq 0x152f7(%rip), %r14     # 0x1d2d0
movl $0x4b0, %eax            # imm = 0x4B0
addq 0x14ffb(%rip), %rax     # 0x1cfe0
movq %rax, 0x48(%rsp)
movl $0x640, %eax            # imm = 0x640
addq 0x14fea(%rip), %rax     # 0x1cfe0
movq %rax, 0x50(%rsp)
movl $0x960, %eax            # imm = 0x960
addq 0x14fd9(%rip), %rax     # 0x1cfe0
movq %rax, 0x58(%rsp)
leaq 0x70(%rsp), %r15
leaq 0xbc44(%rip), %rbx      # 0x13c5c
leaq 0x152a1(%rip), %rax     # 0x1d2c0
movl (%rax), %edi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x20a0
cmpq $-0x1, %rax
je 0x878c
movb $0x0, (%r14,%rax)
movq %r14, %rdi
leaq 0xbae7(%rip), %rsi      # 0x13b30
callq 0xc187
testl %eax, %eax
je 0x809a
movl $0x190, %eax            # imm = 0x190
leaq 0x16282(%rip), %rcx     # 0x1e2e0
leaq (%rax,%rcx), %r12
leaq 0xbad2(%rip), %rdi      # 0x13b3b
movq %r12, %rsi
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
leaq 0xbac8(%rip), %rsi      # 0x13b45
movq %r12, %rdx
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xbd04
movq (%r13), %rax
cmpl $0x0, 0x140(%rax)
jne 0x8102
movq %r14, %rdi
leaq 0xeb3b(%rip), %rsi      # 0x16bec
callq 0xc187
testl %eax, %eax
jne 0x80cd
movq %r14, %rdi
leaq 0xba8c(%rip), %rsi      # 0x13b50
callq 0xc187
testl %eax, %eax
je 0x8102
leaq 0xbce1(%rip), %rdi      # 0x13db5
callq 0x2340
movq (%r13), %rdi
leaq 0xc0(%rdi), %rsi
leaq 0x40(%rdi), %rdx
leaq 0x80(%rdi), %rcx
callq 0xc2da
movq (%r13), %rax
movl $0x1, 0x140(%rax)
movq %r14, %rdi
leaq 0xba60(%rip), %rsi      # 0x13b6c
callq 0xc187
testl %eax, %eax
jne 0x8708
movq (%r13), %rax
cmpl $0x0, 0x144(%rax)
je 0x84a7
movq %r14, %rdi
leaq 0xba8a(%rip), %rsi      # 0x13bbe
callq 0xc187
testl %eax, %eax
je 0x8193
leaq 0x1619c(%rip), %r12     # 0x1e2e0
leaq 0x190(%r12), %rdi
xorl %esi, %esi
pushq $0xa
popq %rbp
movl %ebp, %edx
callq 0x2090
incl %eax
movl %eax, 0x15100(%rip)     # 0x1d260
leaq 0x320(%r12), %rdi
xorl %esi, %esi
movl %ebp, %edx
callq 0x2090
incl %eax
movl %eax, 0x150eb(%rip)     # 0x1d264
movq (%r13), %rcx
xorl %edx, %edx
movb %dl, (%rcx,%rax)
movq (%r13), %rax
movl 0x150d4(%rip), %ecx     # 0x1d260
movb %dl, 0x100(%rax,%rcx)
movq %r14, %rdi
leaq 0xba4e(%rip), %rsi      # 0x13beb
callq 0xc187
testl %eax, %eax
je 0x825c
movq 0x48(%rsp), %rdi
callq 0xc860
cmpl $-0x1, %eax
je 0x825c
leaq 0x178bc(%rip), %rcx     # 0x1fa80
movq (%rcx), %r13
cltq
imulq $0x26a8, %rax, %rcx     # imm = 0x26A8
leaq (%rcx,%r13), %rbp
addq $0x1a0, %rbp            # imm = 0x1A0
movb 0x26a1(%rbp), %al
cmpb $0x1, %al
ja 0x81ff
cmpb $0x0, 0x26a0(%rbp)
je 0x825c
leaq 0x1a0(%r13), %rax
movb $0x2, 0x26a1(%rax,%rcx)
movb $0x2, %al
movb 0x26a0(%rbp), %cl
leaq 0xb9fc(%rip), %r12      # 0x13c08
cmpb %al, %cl
jae 0x821f
movb %al, 0x26a0(%rbp)
movl %eax, %ecx
leaq 0x97a1(%rip), %r12      # 0x119c0
addq $0x100, %r13            # imm = 0x100
movzbl %cl, %edi
callq 0xd488
movl $0x190, %esi            # imm = 0x190
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x23a0
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xbd04
movq %r14, %rdi
leaq 0xb9cd(%rip), %rsi      # 0x13c33
callq 0xc187
testl %eax, %eax
leaq 0x1780c(%rip), %r13     # 0x1fa80
movq 0x50(%rsp), %r12
pushq $0x5
popq %rbp
je 0x82fb
movq %r12, %rdi
leaq 0xb9cf(%rip), %rsi      # 0x13c57
callq 0x2380
testq %rax, %rax
je 0x82fb
movq %rax, %rdi
callq 0xbfd4
movq %rax, %rdi
callq 0xc860
cmpl $-0x1, %eax
je 0x82f4
movq (%r13), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
leaq (%rcx,%rax), %rdx
addq $0x1a0, %rdx            # imm = 0x1A0
movl $0x1, 0x2664(%rdx)
movb $0x0, 0x26a0(%rdx)
movq %r15, %rdi
leaq 0x9654(%rip), %rsi      # 0x1192e
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xbd04
xorl %edi, %edi
movq %rbx, %rsi
jmp 0x8288
movq %r14, %rdi
leaq 0xb95e(%rip), %rsi      # 0x13c63
callq 0xc187
testl %eax, %eax
je 0x8358
movq %r12, %rdi
leaq 0xb97c(%rip), %rsi      # 0x13c94
movq %rbp, %rdx
callq 0x2200
testl %eax, %eax
jne 0x8358
movq 0x58(%rsp), %rdi
callq 0xbfd4
movq %rax, %rdi
callq 0xc860
cmpl $-0x1, %eax
je 0x8358
movq (%r13), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
andl $0x0, 0x2804(%rcx,%rax)
movb $0x0, 0x2840(%rcx,%rax)
movq %r14, %rdi
leaq 0xb937(%rip), %rsi      # 0x13c99
callq 0xc187
testl %eax, %eax
je 0x83e6
leaq 0x15f6e(%rip), %r12     # 0x1e2e0
leaq 0x190(%r12), %rdi
callq 0xbfd4
movq %rax, %rdi
callq 0xc860
movl %eax, %ebp
leaq 0x320(%r12), %rdi
callq 0xbfd4
movq %rax, %rdi
callq 0xc860
cmpl $-0x1, %ebp
je 0x83c1
movq (%r13), %rcx
movslq %ebp, %rdx
imulq $0x26a8, %rdx, %rdx     # imm = 0x26A8
andl $0x0, 0x2804(%rcx,%rdx)
movb $0x0, 0x2840(%rcx,%rdx)
cmpl $-0x1, %eax
je 0x83e6
movq (%r13), %rcx
cltq
imulq $0x26a8, %rax, %rax     # imm = 0x26A8
movl $0x1, 0x2804(%rcx,%rax)
movb $0x0, 0x2840(%rcx,%rax)
movq %r14, %rdi
leaq 0xb8ca(%rip), %rsi      # 0x13cba
callq 0xc187
testl %eax, %eax
je 0x85d8
movl $0x190, %eax            # imm = 0x190
leaq 0x15ed7(%rip), %rcx     # 0x1e2e0
leaq (%rax,%rcx), %r13
movq %r13, %rdi
callq 0xbfd4
movq %rax, %rdi
callq 0xc860
cmpl $-0x1, %eax
je 0x8594
movl %eax, %ebp
movq 0x50(%rsp), %r14
movq %r14, %rdi
leaq 0xb8a6(%rip), %rsi      # 0x13cdd
pushq $0x5
popq %r12
movq %r12, %rdx
callq 0x2200
movslq %ebp, %rbp
testl %eax, %eax
je 0x851f
movq %r14, %rdi
leaq 0xb88a(%rip), %rsi      # 0x13ce2
movq %r12, %rdx
callq 0x2200
testl %eax, %eax
je 0x8542
movq %r14, %rdi
leaq 0xb875(%rip), %rsi      # 0x13ce7
movq %r12, %rdx
callq 0x2200
testl %eax, %eax
leaq 0x14e4d(%rip), %r14     # 0x1d2d0
jne 0x8594
leaq 0x175f0(%rip), %rax     # 0x1fa80
movq (%rax), %rax
imulq $0x26a8, %rbp, %rcx     # imm = 0x26A8
andl $0x0, 0x2804(%rax,%rcx)
jmp 0x8594
cmpl $0x1, 0x140(%rax)
jne 0x86f4
movq %r14, %rdi
leaq 0xb6e6(%rip), %rsi      # 0x13ba4
callq 0xc187
testl %eax, %eax
je 0x86e3
leaq 0xb922(%rip), %rdi      # 0x13df4
callq 0x2340
movq (%r13), %rdx
movl $0x100, %eax            # imm = 0x100
addq %rax, %rdx
movq %r15, %rdi
leaq 0xb6c7(%rip), %rsi      # 0x13bb4
xorl %eax, %eax
callq 0x2050
callq 0xa584
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xbd04
movq (%r13), %rax
movl $0x1, 0x144(%rax)
jmp 0x812a
leaq 0x1755a(%rip), %rax     # 0x1fa80
movq (%rax), %rax
imulq $0x26a8, %rbp, %rcx     # imm = 0x26A8
andl $0x0, 0x2804(%rax,%rcx)
movb $0x0, 0x2840(%rax,%rcx)
jmp 0x858d
leaq 0x17537(%rip), %rax     # 0x1fa80
movq (%rax), %rax
imulq $0x26a8, %rbp, %rcx     # imm = 0x26A8
movl $0x1, 0x2804(%rax,%rcx)
movb $0x0, 0x2840(%rax,%rcx)
movq %r15, %rdi
leaq 0x93be(%rip), %rsi      # 0x1192e
movq %r13, %rdx
xorl %eax, %eax
callq 0x2050
movq %r15, %rdi
callq 0x20c0
movq %r15, %rdi
movq %rax, %rsi
callq 0xbd04
leaq 0x14d3c(%rip), %r14     # 0x1d2d0
leaq 0x174e5(%rip), %r13     # 0x1fa80
movq (%r13), %rsi
cmpl $0x0, 0x148(%rsi)
jne 0x85d8
movl 0x14cb6(%rip), %edx     # 0x1d264
leaq 0xb00(%rsp), %r12
movq %r12, %rdi
callq 0x20e0
movq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rsi
pushq $0x3f
popq %rdx
callq 0x20e0
movq %r12, %rdi
callq 0xc91b
movq %r14, %rdi
leaq 0xb70a(%rip), %rsi      # 0x13cec
callq 0xc187
testl %eax, %eax
je 0x86f4
leaq 0x15cea(%rip), %rbp     # 0x1e2e0
leaq 0x190(%rbp), %rdi
callq 0xbfd4
movl 0x14c5b(%rip), %r12d    # 0x1d264
leaq 0x200(%rsp), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x20e0
leaq 0x320(%rbp), %rsi
leaq 0x240(%rsp), %rdi
pushq $0xa
popq %rdx
callq 0x20e0
leaq 0x4b0(%rbp), %rsi
movq 0x68(%rsp), %rdi
pushq $0x3f
popq %rdx
callq 0x20e0
leaq 0x7d0(%rbp), %rsi
leaq 0x2c0(%rsp), %rdi
movq %r12, %rdx
callq 0x20e0
leaq 0x960(%rbp), %rsi
movl $0x190, %edx            # imm = 0x190
leaq 0x300(%rsp), %rdi
callq 0x20e0
movabsq $0x47534d56495250, %rax # imm = 0x47534D56495250
cmpq %rax, 0x640(%rbp)
jne 0x86f4
leaq 0x300(%rsp), %rdi
leaq 0x9370(%rip), %rsi      # 0x11a09
callq 0xc187
testl %eax, %eax
je 0x86f4
cmpb $0x23, 0x2c0(%rsp)
jne 0x86d4
leaq 0x200(%rsp), %r12
movq %r12, %rdi
callq 0xc860
xorl %edi, %edi
movl %eax, %esi
leaq 0x15c19(%rip), %rdx     # 0x1e2e0
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x355c
jmp 0x86f4
leaq 0x200(%rsp), %rdi
callq 0xc34d
jmp 0x86f4
movq (%r13), %rax
cmpl $0x0, 0x144(%rax)
jne 0x812a
movl $0x190, %edx            # imm = 0x190
movq %r14, %rdi
xorl %esi, %esi
callq 0x20f0
jmp 0x8018
callq 0x2080
cltd
pushq $0xa
popq %rcx
idivl %ecx
movl %edx, %ebp
movq (%r13), %r12
movq %r12, %rdi
callq 0x20c0
movq %rax, %r13
movq 0x148ad(%rip), %rax     # 0x1cfd8
movq (%rax), %rdi
leaq 0xb448(%rip), %rsi      # 0x13b7d
movq %r12, %rdx
xorl %eax, %eax
callq 0x2320
leaq 0x1733a(%rip), %rax     # 0x1fa80
movq (%rax), %rdi
addq %r13, %rdi
movl 0x14b12(%rip), %esi     # 0x1d264
subq %r13, %rsi
leaq 0x17324(%rip), %r13     # 0x1fa80
leaq 0xaa9c(%rip), %rdx      # 0x131ff
movl %ebp, %ecx
xorl %eax, %eax
callq 0x23a0
movq (%r13), %rdi
leaq 0xc0(%rdi), %rsi
leaq 0x40(%rdi), %rdx
leaq 0x80(%rdi), %rcx
callq 0xc2da
jmp 0x8119
cmpl $0x0, (%rsp)
je 0x879c
movq 0x20(%rsp), %rdi
callq 0x2250
testb $0x1, 0x8(%rsp)
jne 0x87ad
movq 0x18(%rsp), %rdi
callq 0x2250
cmpl $0x0, 0x4(%rsp)
je 0x87be
movq 0x10(%rsp), %rdi
callq 0x2250
callq 0xbbbe
callq 0xbcb6
callq 0xe7bf
callq 0x62d4
callq 0x9f85
movq (%r13), %rdi
callq 0x2250
leaq 0x14a99(%rip), %rax     # 0x1d280
cmpl $0x0, (%rax)
setne %al
movq 0x14a94(%rip), %rdi     # 0x1d288
testq %rdi, %rdi
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x8806
callq 0x2250
callq 0x8958
leaq 0xb531(%rip), %rdi      # 0x13d43
leaq 0x14a77(%rip), %rsi     # 0x1d290
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x2040
movl %ebx, %eax
addq $0x1408, %rsp           # imm = 0x1408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb4db(%rip), %rdi      # 0x13d18
callq 0x2190
leaq 0x14a77(%rip), %rax     # 0x1d2c0
movl (%rax), %edi
callq 0x2490
jmp 0x7dc0
movq 0x1477c(%rip), %rax     # 0x1cfd8
movq (%rax), %rcx
leaq 0xb23e(%rip), %rdi      # 0x13aa4
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2480
jmp 0x7dc0
movq 0x1475b(%rip), %rax     # 0x1cfd8
movq (%rax), %rdi
leaq 0xb26d(%rip), %rsi      # 0x13af4
movl %ebp, %edx
xorl %eax, %eax
callq 0x2320
jmp 0x7dc0
leaq 0xaefa(%rip), %rdi      # 0x13796
jmp 0x88e4
leaq 0xaff4(%rip), %rdi      # 0x13899
jmp 0x88e4
leaq 0xafa4(%rip), %rdi      # 0x13852
jmp 0x88e4
leaq 0xaf50(%rip), %rdi      # 0x13807
jmp 0x88e4
leaq 0xb01f(%rip), %rdi      # 0x138df
jmp 0x88e4
leaq 0xb060(%rip), %rdi      # 0x13929
jmp 0x88e4
leaq 0xb0b1(%rip), %rdi      # 0x13983
jmp 0x88e4
leaq 0xb0f8(%rip), %rdi      # 0x139d3
jmp 0x88e4
leaq 0xb139(%rip), %rdi      # 0x13a1d
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270