// File: /workspace/github_c_makefile_star2_O0/desion[P]terminal/src/terminal.c
// Function: main
int main(int argc, char* argv[]){
    int c;
    int i;
    struct in_addr addr;
    settings_init();
    while ((c = getopt(argc, argv, "p:c:hvdl:t:T:i:")) != -1) {
        switch (c) {
        case 'p':
            settings.port = atoi(optarg);
            break;
        case 'c':
            settings.maxconns = atoi(optarg);
            break;
        case 'h':
            usage();
            exit(EXIT_SUCCESS);
        case 'l':
            if (inet_pton(AF_INET, optarg, &addr) <= 0) {
                fprintf(stderr, "Illegal address: %s\n", optarg);
                return 1;
            } else {
                settings.interf = addr;
            }
            break;
        case 'd':
            settings.daemonize = 1;
            break;
        case 'v':
            settings.verbose++;
            break;
        case 't':
            settings.num_threads = atoi(optarg);
            if (settings.num_threads == 0) {
                fprintf(stderr, "Number of threads must be greater than 0\n");
                return 1;
            }
            break;
        case 'T':
            settings.timeout = atoi(optarg);
            break;
        default:
            fprintf(stderr, "Illegal argument \"%c\"\n", c);
            return 1;
        }
    }

    srand(time(NULL)^getpid());
    //enev_init();

    if (settings.daemonize) {
        int res;
        res = daemon(1, settings.verbose);
        if (res == -1) {
            fprintf(stderr, "failed to daemon() in order to daemonize\n");
            return 1;
        }
    }

    stats_init();
    conn_init();
    /* create the socket */
    l_socket = server_socket(settings.port);
    if (l_socket == -1) {
        fprintf(stderr, "failed to listen\n");
        exit(1);
    }

    cq_init(&REQ, 1);
    cq_init(&RSP, 0);

    /* create the pipe for receive/send notify */
    int fds[2];
    if (pipe(fds)) {
        fprintf(stderr, "can't create notify pipe\n");
        exit(1);
    }
    notify_receive_fd = fds[0];
    notify_send_fd = fds[1];

    hdb_t hdb;
    hdb_init(&hdb);

    /* hdb manager thread */
    pthread_t tid;
    pthread_create(&tid, NULL, hdb_mgr, &hdb);

    /* start worker thread */
    for (i = 0; i < settings.num_threads; i++) {
        pthread_create(&tid, NULL, worker, &hdb);
    }
    
    /* signal process */
    struct sigaction sa;
    sa.sa_handler = SIG_IGN;
    sa.sa_flags = 0;
    if (sigemptyset(&sa.sa_mask) == -1 ||
        sigaction(SIGPIPE, &sa, 0) == -1) {
        perror("failed to ignore SIGPIPE; sigaction");
        exit(1);
    }

    /* libevent */
    struct event_base *main_base = event_init();
    struct event notify_event;

    event_set(&notify_event, notify_receive_fd,
        EV_READ | EV_PERSIST, notify_handler, NULL);
    event_base_set(main_base, &notify_event);

    if (event_add(&notify_event, 0) == -1) {
        fprintf(stderr, "can't monitor libevent notify pipe\n");
        exit(1);
    }

    conn *listen_conn;
    if (!(listen_conn = conn_new(l_socket, conn_listening,
            EV_READ | EV_PERSIST, main_base))) {
        fprintf(stderr, "failed to create listening connection");
        exit(1);
    }
    event_base_loop(main_base, 0);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x21c0, %rsp           # imm = 0x21C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x64a0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x2170(%rip), %rdx      # 0x750c
callq 0x23c0
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x551f
movl -0x14(%rbp), %eax
addl $-0x54, %eax
movl %eax, %ecx
movq %rcx, -0x21b0(%rbp)
subl $0x22, %eax
ja 0x54f3
movq -0x21b0(%rbp), %rax
leaq 0x20ad(%rip), %rcx      # 0x7480
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x4bf5(%rip), %rax      # 0x9fd8
movq (%rax), %rdi
callq 0x23f0
movl %eax, 0x568b(%rip)      # 0xaa7c
jmp 0x551a
movq 0x4bdb(%rip), %rax      # 0x9fd8
movq (%rax), %rdi
callq 0x23f0
movl %eax, 0x566d(%rip)      # 0xaa78
jmp 0x551a
callq 0x57e0
xorl %edi, %edi
callq 0x2430
movq 0x4bb5(%rip), %rax      # 0x9fd8
movq (%rax), %rsi
movl $0x2, %edi
leaq -0x1c(%rbp), %rdx
callq 0x22a0
cmpl $0x0, %eax
jg 0x5467
movq 0x4bb8(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movq 0x4b8e(%rip), %rax      # 0x9fd8
movq (%rax), %rdx
leaq 0x20c8(%rip), %rsi      # 0x751c
movb $0x0, %al
callq 0x2260
movl $0x1, -0x4(%rbp)
jmp 0x57d1
movl -0x1c(%rbp), %eax
movl %eax, 0x5610(%rip)      # 0xaa80
jmp 0x551a
movl $0x1, 0x5611(%rip)      # 0xaa90
jmp 0x551a
movl 0x55fe(%rip), %eax      # 0xaa88
addl $0x1, %eax
movl %eax, 0x55f5(%rip)      # 0xaa88
jmp 0x551a
movq 0x4b39(%rip), %rax      # 0x9fd8
movq (%rax), %rdi
callq 0x23f0
movl %eax, 0x55d7(%rip)      # 0xaa84
cmpl $0x0, 0x55d0(%rip)      # 0xaa84
jne 0x54da
movq 0x4b3b(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x206a(%rip), %rsi      # 0x7531
movb $0x0, %al
callq 0x2260
movl $0x1, -0x4(%rbp)
jmp 0x57d1
jmp 0x551a
movq 0x4af5(%rip), %rax      # 0x9fd8
movq (%rax), %rdi
callq 0x23f0
movl %eax, 0x559b(%rip)      # 0xaa8c
jmp 0x551a
movq 0x4afe(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
leaq 0x2054(%rip), %rsi      # 0x755b
movb $0x0, %al
callq 0x2260
movl $0x1, -0x4(%rbp)
jmp 0x57d1
jmp 0x538e
xorl %eax, %eax
movl %eax, %edi
callq 0x22b0
movq %rax, -0x21b8(%rbp)
callq 0x20f0
movl %eax, %ecx
movq -0x21b8(%rbp), %rax
movslq %ecx, %rcx
xorq %rcx, %rax
movl %eax, %edi
callq 0x2200
cmpl $0x0, 0x553f(%rip)      # 0xaa90
je 0x5592
movl 0x552f(%rip), %esi      # 0xaa88
movl $0x1, %edi
callq 0x2320
movl %eax, -0x20(%rbp)
cmpl $-0x1, -0x20(%rbp)
jne 0x5590
movq 0x4a85(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x1ff5(%rip), %rsi      # 0x7572
movb $0x0, %al
callq 0x2260
movl $0x1, -0x4(%rbp)
jmp 0x57d1
jmp 0x5592
callq 0x64f0
movb $0x0, %al
callq 0x3ce0
movl 0x54d8(%rip), %edi      # 0xaa7c
callq 0x5010
movl %eax, 0x54e5(%rip)      # 0xaa94
cmpl $-0x1, 0x54de(%rip)     # 0xaa94
jne 0x55da
movq 0x4a39(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x1fd3(%rip), %rsi      # 0x759c
movb $0x0, %al
callq 0x2260
movl $0x1, %edi
callq 0x2430
leaq 0x54b7(%rip), %rdi      # 0xaa98
movl $0x1, %esi
callq 0x42a0
leaq 0x5516(%rip), %rdi      # 0xab08
xorl %esi, %esi
callq 0x42a0
leaq -0x28(%rbp), %rdi
callq 0x21d0
cmpl $0x0, %eax
je 0x5629
movq 0x49ea(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x1f96(%rip), %rsi      # 0x75ae
movb $0x0, %al
callq 0x2260
movl $0x1, %edi
callq 0x2430
movl -0x28(%rbp), %eax
movl %eax, 0x5546(%rip)      # 0xab78
movl -0x24(%rbp), %eax
movl %eax, 0x5541(%rip)      # 0xab7c
leaq -0x2078(%rbp), %rdi
callq 0x3190
leaq -0x2080(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x2619(%rip), %rdx     # 0x3040
leaq -0x2078(%rbp), %rcx
callq 0x2350
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x540f(%rip), %eax      # 0xaa84
jge 0x56a0
leaq -0x2080(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x177(%rip), %rdx       # 0x5800
leaq -0x2078(%rbp), %rcx
callq 0x2350
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x566c
movl $0x1, %eax
movq %rax, -0x2118(%rbp)
movl $0x0, -0x2090(%rbp)
leaq -0x2118(%rbp), %rdi
addq $0x8, %rdi
callq 0x2270
cmpl $-0x1, %eax
je 0x56e5
movl $0xd, %edi
leaq -0x2118(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x20a0
cmpl $-0x1, %eax
jne 0x56fb
leaq 0x1edc(%rip), %rdi      # 0x75c8
callq 0x23b0
movl $0x1, %edi
callq 0x2430
callq 0x2460
movq %rax, -0x2120(%rbp)
movl 0x546b(%rip), %esi      # 0xab78
leaq -0x21a0(%rbp), %rdi
movl $0x12, %edx
leaq -0x8a0(%rip), %rcx      # 0x4e80
xorl %eax, %eax
movl %eax, %r8d
callq 0x2380
movq -0x2120(%rbp), %rdi
leaq -0x21a0(%rbp), %rsi
callq 0x2060
leaq -0x21a0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2250
cmpl $-0x1, %eax
jne 0x5774
movq 0x489f(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x1e89(%rip), %rsi      # 0x75ec
movb $0x0, %al
callq 0x2260
movl $0x1, %edi
callq 0x2430
movl 0x531a(%rip), %edi      # 0xaa94
movq -0x2120(%rbp), %rcx
xorl %esi, %esi
movl $0x12, %edx
callq 0x3d20
movq %rax, -0x21a8(%rbp)
cmpq $0x0, %rax
jne 0x57bc
movq 0x4857(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x1e65(%rip), %rsi      # 0x7610
movb $0x0, %al
callq 0x2260
movl $0x1, %edi
callq 0x2430
movq -0x2120(%rbp), %rdi
xorl %esi, %esi
callq 0x2440
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x21c0, %rsp           # imm = 0x21C0
popq %rbp
retq
nopl (%rax)