// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2188, %rsp           # imm = 0x2188
movq %rsi, %rbx
movl %edi, %ebp
movaps 0x1cc7(%rip), %xmm0     # 0x5ca0
movaps %xmm0, 0x5290(%rip)     # 0x9270
andq $0x0, 0x5298(%rip)      # 0x9280
leaq 0x1ce9(%rip), %r14      # 0x5cd8
movq 0x4fe2(%rip), %r13      # 0x8fd8
leaq 0x1cb3(%rip), %r15      # 0x5cb0
leaq 0xc(%rsp), %r12
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2390
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
jbe 0x403b
cmpl $0x54, %eax
je 0x409b
cmpl $0x70, %eax
je 0x4087
cmpl $0x74, %eax
je 0x40af
cmpl $0x76, %eax
jne 0x40e7
incl 0x5247(%rip)            # 0x9280
jmp 0x4002
movslq (%r15,%rcx,4), %rcx
addq %r15, %rcx
jmpq *%rcx
movq (%r13), %rdi
callq 0x23c0
movl %eax, 0x521d(%rip)      # 0x9270
jmp 0x4002
movq (%r13), %rsi
pushq $0x2
popq %rdi
movq %r12, %rdx
callq 0x2270
testl %eax, %eax
jle 0x42ee
movl 0xc(%rsp), %eax
movl %eax, 0x5202(%rip)      # 0x9278
jmp 0x4002
movl $0x1, 0x5206(%rip)      # 0x9288
jmp 0x4002
movq (%r13), %rdi
callq 0x23c0
movl %eax, 0x51de(%rip)      # 0x9274
jmp 0x4002
movq (%r13), %rdi
callq 0x23c0
movl %eax, 0x51da(%rip)      # 0x9284
jmp 0x4002
movq (%r13), %rdi
callq 0x23c0
movl %eax, 0x51be(%rip)      # 0x927c
testl %eax, %eax
jne 0x4002
movq 0x4f2b(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1c26(%rip), %rdi      # 0x5cfd
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2400
jmp 0x430a
cmpl $-0x1, %eax
jne 0x42d2
xorl %edi, %edi
callq 0x2280
movq %rax, %rbx
callq 0x2100
xorl %eax, %ebx
movl %ebx, %edi
callq 0x21d0
cmpl $0x0, 0x5179(%rip)      # 0x9288
je 0x412a
movl 0x5169(%rip), %esi      # 0x9280
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x22f0
cmpl $-0x1, %eax
je 0x430f
andl $0x0, 0x527b(%rip)      # 0x93ac
xorps %xmm0, %xmm0
movups %xmm0, 0x524d(%rip)     # 0x9388
andl $0x0, 0x5256(%rip)      # 0x9398
xorl %edi, %edi
callq 0x2280
movq %rax, 0x5250(%rip)      # 0x93a0
leaq 0x5259(%rip), %rdi      # 0x93b0
xorl %esi, %esi
callq 0x2450
xorl %eax, %eax
callq 0x31dc
movl 0x5109(%rip), %edi      # 0x9274
callq 0x3deb
movl %eax, 0x5116(%rip)      # 0x928c
cmpl $-0x1, %eax
je 0x4347
leaq 0x510a(%rip), %rdi      # 0x9290
pushq $0x1
popq %rsi
callq 0x3580
leaq 0x516b(%rip), %rdi      # 0x9300
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x3580
leaq 0x10(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
jne 0x435c
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
movl %eax, 0x51b1(%rip)      # 0x9370
movl %ecx, 0x51af(%rip)      # 0x9374
leaq 0x138(%rsp), %rbx
movq %rbx, %rdi
callq 0x2c1a
leaq -0x1652(%rip), %rdx     # 0x2b8a
leaq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2320
leaq 0x1c1(%rip), %r15       # 0x43b6
cmpl 0x5080(%rip), %r12d     # 0x927c
jge 0x4213
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x2320
incl %r12d
jmp 0x41f5
leaq 0x28(%rsp), %rdi
movq $0x1, -0x8(%rdi)
andl $0x0, 0x80(%rdi)
callq 0x2240
cmpl $-0x1, %eax
je 0x4339
pushq $0xd
popq %rdi
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x20b0
cmpl $-0x1, %eax
je 0x4339
callq 0x2430
movq %rax, %r14
movl 0x5115(%rip), %esi      # 0x9370
leaq -0x590(%rip), %rcx      # 0x3cd2
leaq 0xb8(%rsp), %rbx
pushq $0x12
popq %rdx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0x2350
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2060
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2220
cmpl $-0x1, %eax
je 0x4371
movl 0x4ff0(%rip), %edi      # 0x928c
xorl %ebx, %ebx
pushq $0x12
popq %rdx
xorl %esi, %esi
movq %r14, %rcx
callq 0x320a
testq %rax, %rax
je 0x4386
movq %r14, %rdi
xorl %esi, %esi
callq 0x2410
movl %ebx, %eax
addq $0x2188, %rsp           # imm = 0x2188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4d1f(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x1a44(%rip), %rsi      # 0x5d27
movl %eax, %edx
xorl %eax, %eax
callq 0x2230
jmp 0x430a
movq 0x4d03(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x19e5(%rip), %rsi      # 0x5ce8
xorl %eax, %eax
callq 0x2230
pushq $0x1
popq %rbx
jmp 0x42be
movq 0x4ce2(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1a1e(%rip), %rdi      # 0x5d3e
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2400
jmp 0x42be
callq 0x43aa
xorl %edi, %edi
callq 0x23f0
leaq 0x1a54(%rip), %rdi      # 0x5d94
callq 0x2380
jmp 0x43a2
movq 0x4caa(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1a10(%rip), %rdi      # 0x5d68
pushq $0x11
jmp 0x4399
movq 0x4c95(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1a0d(%rip), %rdi      # 0x5d7a
pushq $0x19
jmp 0x4399
movq 0x4c80(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1a36(%rip), %rdi      # 0x5db8
pushq $0x23
jmp 0x4399
movq 0x4c6b(%rip), %rax      # 0x8ff8
movq (%rax), %rcx
leaq 0x1a45(%rip), %rdi      # 0x5ddc
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x2400
pushq $0x1
popq %rdi
callq 0x23f0