// File: /workspace/github_c_makefile_star2_O1/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
xorl %eax, %eax
movl %eax, 0x4ed2(%rip)      # 0x9270
movabsq $0x26aa00002000, %rcx   # imm = 0x26AA00002000
movq %rcx, 0x4eb9(%rip)      # 0x9268
movq $0x4, 0x4eba(%rip) # 0x9274
movl %eax, 0x4ebc(%rip)      # 0x927c
leaq 0x18ed(%rip), %r14      # 0x5cb4
movq 0x4c0a(%rip), %r12      # 0x8fd8
leaq 0x18b7(%rip), %r13      # 0x5c8c
leaq 0xc(%rsp), %r15
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x23a0
cmpl $0x6f, %eax
jg 0x440f
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x4437
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4e5b(%rip)      # 0x9268
jmp 0x43da
cmpl $0x76, %eax
je 0x44a0
cmpl $0x74, %eax
je 0x4484
cmpl $0x70, %eax
jne 0x46ab
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4e37(%rip)      # 0x926c
jmp 0x43da
cmpl $0x54, %eax
jne 0x44ab
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4e31(%rip)      # 0x927c
jmp 0x43da
movq (%r12), %rsi
movl $0x2, %edi
movq %r15, %rdx
callq 0x2280
testl %eax, %eax
jle 0x46c7
movl 0xc(%rsp), %eax
movl %eax, 0x4e00(%rip)      # 0x9270
jmp 0x43da
movl $0x1, 0x4e01(%rip)      # 0x9280
jmp 0x43da
movq (%r12), %rdi
callq 0x23d0
movl %eax, 0x4de1(%rip)      # 0x9274
testl %eax, %eax
jne 0x43da
jmp 0x46ce
incl 0x4dd2(%rip)            # 0x9278
jmp 0x43da
cmpl $-0x1, %eax
jne 0x46ab
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x2290
movq %rax, %r14
callq 0x2100
xorl %eax, %r14d
movl %r14d, %edi
callq 0x21e0
cmpl $0x0, 0x4da9(%rip)      # 0x9280
je 0x44fd
movl 0x4d99(%rip), %esi      # 0x9278
movl $0x1, %edi
callq 0x2300
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x46ec
cmpl $-0x1, %ebp
je 0x46d8
xorl %eax, %eax
movl %eax, 0x4e9f(%rip)      # 0x93a4
xorps %xmm0, %xmm0
movups %xmm0, 0x4e71(%rip)     # 0x9380
movl %eax, 0x4e7b(%rip)      # 0x9390
xorl %edi, %edi
callq 0x2290
movq %rax, 0x4e75(%rip)      # 0x9398
leaq 0x4e7e(%rip), %rdi      # 0x93a8
xorl %esi, %esi
callq 0x2460
xorl %eax, %eax
callq 0x3540
movl 0x4d2e(%rip), %edi      # 0x926c
callq 0x4185
movl %eax, 0x4d3b(%rip)      # 0x9284
cmpl $-0x1, %eax
je 0x470c
leaq 0x4d2f(%rip), %rdi      # 0x9288
movl $0x1, %esi
callq 0x38d4
leaq 0x4d8e(%rip), %rdi      # 0x92f8
xorl %esi, %esi
callq 0x38d4
leaq 0x10(%rsp), %rdi
callq 0x21b0
testl %eax, %eax
jne 0x4711
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
movl %eax, 0x4dd7(%rip)      # 0x9368
movl %ecx, 0x4dd5(%rip)      # 0x936c
leaq 0x138(%rsp), %rbx
movq %rbx, %rdi
callq 0x2f35
leaq -0x1718(%rip), %rdx     # 0x2e96
xorl %r12d, %r12d
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movq %rbx, %rcx
callq 0x2330
cmpl $0x0, 0x4cad(%rip)      # 0x9274
jle 0x45f9
leaq 0x15c(%rip), %rbx       # 0x472c
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 0x4c7d(%rip), %r12d     # 0x9274
jl 0x45dd
leaq 0x28(%rsp), %rdi
movq $0x1, -0x8(%rdi)
movl $0x0, 0x80(%rdi)
callq 0x2250
cmpl $-0x1, %eax
je 0x4707
leaq 0x20(%rsp), %rsi
movl $0xd, %edi
xorl %edx, %edx
callq 0x20b0
cmpl $-0x1, %eax
je 0x4707
callq 0x2440
movq %rax, %r14
movl 0x4d22(%rip), %esi      # 0x9368
leaq -0x5bf(%rip), %rcx      # 0x408e
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
movl $0x12, %edx
xorl %r8d, %r8d
callq 0x2360
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2060
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2230
cmpl $-0x1, %eax
je 0x4716
movl 0x4bfb(%rip), %edi      # 0x9284
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x12, %edx
movq %r14, %rcx
callq 0x3571
testq %rax, %rax
je 0x471b
movq %r14, %rdi
xorl %esi, %esi
callq 0x2420
jmp 0x46d8
movq 0x4946(%rip), %rcx      # 0x8ff8
movq (%rcx), %rdi
leaq 0x1647(%rip), %rsi      # 0x5d03
movl %eax, %edx
xorl %eax, %eax
callq 0x2240
jmp 0x46d3
callq 0x264a
jmp 0x46d3
callq 0x2628
movl $0x1, %ebx
movl %ebx, %eax
addq $0x2188, %rsp           # imm = 0x2188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2672
movl $0x1, %ebx
jmp 0x44f4
callq 0x4720
xorl %edi, %edi
callq 0x2400
callq 0x2703
callq 0x2718
callq 0x2694
callq 0x26de
callq 0x26b9