// File: /workspace/github_c_makefile_star2_O3/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 0x1a1b(%rip), %xmm0     # 0x5ca0
movaps %xmm0, 0x4fe4(%rip)     # 0x9270
movq $0x0, 0x4fe9(%rip) # 0x9280
leaq 0x1a3a(%rip), %r14      # 0x5cd8
movq 0x4d33(%rip), %r12      # 0x8fd8
leaq 0x1a04(%rip), %r13      # 0x5cb0
leaq 0xc(%rsp), %r15
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x23a0
cmpl $0x6f, %eax
jg 0x42e6
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x430e
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4f8c(%rip)      # 0x9270
jmp 0x42b1
cmpl $0x76, %eax
je 0x4377
cmpl $0x74, %eax
je 0x435b
cmpl $0x70, %eax
jne 0x458d
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4f68(%rip)      # 0x9274
jmp 0x42b1
cmpl $0x54, %eax
jne 0x4382
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4f62(%rip)      # 0x9284
jmp 0x42b1
movq (%r12), %rsi
movl $0x2, %edi
movq %r15, %rdx
callq 0x2280
testl %eax, %eax
jle 0x45a9
movl 0xc(%rsp), %eax
movl %eax, 0x4f31(%rip)      # 0x9278
jmp 0x42b1
movl $0x1, 0x4f32(%rip)      # 0x9288
jmp 0x42b1
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4f12(%rip)      # 0x927c
testl %eax, %eax
jne 0x42b1
jmp 0x45b0
incl 0x4f03(%rip)            # 0x9280
jmp 0x42b1
cmpl $-0x1, %eax
jne 0x458d
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x2290
movq %rax, %rbx
callq 0x2100
xorl %eax, %ebx
movl %ebx, %edi
callq 0x21e0
cmpl $0x0, 0x4edb(%rip)      # 0x9288
je 0x43cd
movl 0x4ecb(%rip), %esi      # 0x9280
movl $0x1, %ebp
movl $0x1, %edi
callq 0x2300
cmpl $-0x1, %eax
je 0x45bc
movl %r14d, 0x4fd8(%rip)     # 0x93ac
xorps %xmm0, %xmm0
movups %xmm0, 0x4faa(%rip)     # 0x9388
movl %r14d, 0x4fb3(%rip)     # 0x9398
xorl %edi, %edi
callq 0x2290
movq %rax, 0x4fad(%rip)      # 0x93a0
leaq 0x4fb6(%rip), %rdi      # 0x93b0
xorl %esi, %esi
callq 0x2460
xorl %eax, %eax
callq 0x34ec
movl 0x4e66(%rip), %edi      # 0x9274
callq 0x406b
movl %eax, 0x4e73(%rip)      # 0x928c
cmpl $-0x1, %eax
je 0x45d4
leaq 0x4e67(%rip), %rdi      # 0x9290
movl $0x1, %esi
callq 0x3870
leaq 0x4ec6(%rip), %rdi      # 0x9300
xorl %esi, %esi
callq 0x3870
leaq 0x10(%rsp), %rdi
callq 0x21b0
testl %eax, %eax
jne 0x45d9
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
movl %eax, 0x4f0f(%rip)      # 0x9370
movl %ecx, 0x4f0d(%rip)      # 0x9374
leaq 0x138(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ee5
leaq -0x164a(%rip), %rdx     # 0x2e34
xorl %r12d, %r12d
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2330
cmpl $0x0, 0x4de5(%rip)      # 0x927c
jle 0x44c9
leaq 0x154(%rip), %rbx       # 0x45f4
leaq 0x18(%rsp), %r14
leaq 0x138(%rsp), %r15
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2330
incl %r12d
cmpl 0x4db5(%rip), %r12d     # 0x927c
jl 0x44ad
leaq 0x28(%rsp), %rdi
movq $0x1, -0x8(%rdi)
movl $0x0, 0x80(%rdi)
callq 0x2250
cmpl $-0x1, %eax
je 0x45cf
leaq 0x20(%rsp), %rsi
movl $0xd, %edi
xorl %edx, %edx
callq 0x20b0
cmpl $-0x1, %eax
je 0x45cf
callq 0x2440
movq %rax, %rbx
movl 0x4e5a(%rip), %esi      # 0x9370
leaq -0x5a9(%rip), %rcx      # 0x3f74
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
movl $0x12, %edx
xorl %r8d, %r8d
callq 0x2360
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2060
movq %r14, %rdi
xorl %esi, %esi
callq 0x2230
cmpl $-0x1, %eax
je 0x45de
movl 0x4d33(%rip), %edi      # 0x928c
xorl %ebp, %ebp
xorl %esi, %esi
movl $0x12, %edx
movq %rbx, %rcx
callq 0x351d
testq %rax, %rax
je 0x45e3
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2420
movl %ebp, %eax
addq $0x2188, %rsp           # imm = 0x2188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4a64(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x1789(%rip), %rsi      # 0x5d27
movl %eax, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x45b5
callq 0x2603
jmp 0x45b5
callq 0x25e1
movl $0x1, %ebp
jmp 0x4579
callq 0x262b
jmp 0x4579
callq 0x45e8
xorl %edi, %edi
callq 0x2400
callq 0x26bc
callq 0x26d1
callq 0x264d
callq 0x2697
callq 0x2672