// File: /workspace/github_c_makefile_star2_O3/zuocheng-liu[P]ae-memcached/src/memcached.c
// Function: main
int main (int argc, char **argv) {
    conn *l_conn;
    /*conn *u_conn;*/
    struct passwd *pw;
    struct sigaction sa;
    struct rlimit rlim;
    
    /* set stderr non-buffering (for running under, say, daemontools) */
    setbuf(stderr, NULL);

    /* handle SIGINT */
    signal(SIGINT, sigint_handler);

    /* init settings */
    settings_init(&settings);
    /* process arguments */
    process_arguments(&settings, argc, argv);

    /* create mem_cache instance */
    mem_cache = mem_cache_create(48, 1.25, 40, DEFAULT_POWER_BLOCK, settings.maxbytes, 1);
    /* create AE event loop  */
    g_el = aeCreateEventLoop(AE_SETSIZE);

    if (settings.maxcore) {
        struct rlimit rlim_new;
        /*
         * First try raising to infinity; if that fails, try bringing
         * the soft limit to the hard.
         */
        if (getrlimit(RLIMIT_CORE, &rlim)==0) {
            rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
            if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
                /* failed. try raising just to the old max */
                rlim_new.rlim_cur = rlim_new.rlim_max =
                    rlim.rlim_max;
                (void) setrlimit(RLIMIT_CORE, &rlim_new);
            }
        }
        /*
         * getrlimit again to see what we ended up with. Only fail if
         * the soft limit ends up 0, because then no core files will be
         * created at all.
         */

        if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) {
            fprintf(stderr, "failed to ensure corefile creation\n");
            exit(1);
        }
    }

    /*
     * If needed, increase rlimits to allow as many connections
     * as needed.
     */

    if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
        fprintf(stderr, "failed to getrlimit number of files\n");
        exit(1);
    } else {
        int maxfiles = settings.maxconns;
        if ((int)rlim.rlim_cur < maxfiles)
            rlim.rlim_cur = maxfiles + 3;
        if (rlim.rlim_max < rlim.rlim_cur)
            rlim.rlim_max = rlim.rlim_cur;
        if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
            fprintf(stderr, "failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.\n");
            exit(1);
        }
    }

    /*
     * initialization order: first create the listening sockets
     * (may need root on low ports), then drop root if needed,
     * then daemonise if needed, then init libevent (in some cases
     * descriptors created by libevent wouldn't survive forking).
     */

    /* create the listening socket and bind it */
    if (!settings.socketpath) {
        l_socket = server_socket(settings.port, 0);
        if (l_socket == -1) {
            fprintf(stderr, "failed to listen\n");
            exit(1);
        }
    }

    if (settings.udpport > 0 && ! settings.socketpath) {
        /* create the UDP listening socket and bind it */
        u_socket = server_socket(settings.udpport, 1);
        if (u_socket == -1) {
            fprintf(stderr, "failed to listen on UDP port %d\n", settings.udpport);
            exit(1);
        }
    }

    /* lose root privileges if we have them */
    if (getuid()== 0 || geteuid()==0) {
        if (settings.username==0 || *(settings.username)=='\0') {
            fprintf(stderr, "can't run as root without the -u switch\n");
            return 1;
        }
        if ((pw = getpwnam(settings.username)) == 0) {
            fprintf(stderr, "can't find the user %s to switch to\n", settings.username);
            return 1;
        }
        if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) {
            fprintf(stderr, "failed to assume identity of user %s\n", settings.username);
            return 1;
        }
    }

    /* create unix mode sockets after dropping privileges */
    if (settings.socketpath) {
        l_socket = server_socket_unix(settings.socketpath);
        if (l_socket == -1) {
            fprintf(stderr, "failed to listen\n");
            exit(1);
        }
    }

    /* daemonize if requested */
    /* if we want to ensure our ability to dump core, don't chdir to / */
    if (settings.daemonize) {
        int res;
        res = daemon(settings.maxcore, settings.verbose);
        if (res == -1) {
            fprintf(stderr, "failed to daemon() in order to daemonize\n");
            return 1;
        }
    }


    /* initialize other stuff */
    item_init();
    stats_init(&stats);
    assoc_init();
    conn_init();

    /* managed instance? alloc and zero a bucket array */
    if (settings.managed) {
        buckets = malloc(sizeof(int)*MAX_BUCKETS);
        if (buckets == 0) {
            fprintf(stderr, "failed to allocate the bucket array");
            exit(1);
        }
        memset(buckets, 0, sizeof(int)*MAX_BUCKETS);
    }

    /* lock paged memory if needed */
    if (settings.lock_memory) {
        mlockall(MCL_CURRENT | MCL_FUTURE);
        fprintf(stderr, "warning: mlockall() not supported on this platform.  proceeding without.\n");
    }

    /*
     * ignore SIGPIPE signals; we can use errno==EPIPE if we
     * need that information
     */
    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);
    }
    /* create the initial listening connection */
    if (!(l_conn = conn_new(l_socket, conn_listening, AE_READABLE, 1, 0))) {
        fprintf(stderr, "failed to create listening connection");
        exit(1);
    }
    /* create the initial listening udp connection     
      if (u_socket > -1 &&
        !(u_conn = conn_new(u_socket, conn_read, EV_READ | EV_PERSIST, UDP_READ_BUFFER_SIZE, 1))) {
        fprintf(stderr, "failed to create udp connection");
        exit(1);
    }*/
    /* initialise clock event */
    aeCreateTimeEvent(g_el, 1000, clock_handler, NULL, NULL);
    /* initialise deletion array and timer event */
    deltotal = 200; delcurr = 0;
    todelete = malloc(sizeof(item *)*deltotal);
    aeCreateTimeEvent(g_el, 1000, timer_delete_handler, NULL, NULL);
    
    /* create command service */
    g_cmd_srv = command_service_create();
    /* register commands */
    command_service_register_handler(g_cmd_srv, "get ", 4, FIXED_PREFIX, get_handler);
    command_service_register_handler(g_cmd_srv, "bget ", 5, FIXED_PREFIX, bget_handler);

    /* writting commands */
    command_service_register_handler(g_cmd_srv, "set ", 4, FIXED_PREFIX, set_handler);
    command_service_register_handler(g_cmd_srv, "add ", 4, FIXED_PREFIX, add_handler);
    command_service_register_handler(g_cmd_srv, "replace ", 8, FIXED_PREFIX, replace_handler);
    command_service_register_handler(g_cmd_srv, "incr ", 5, FIXED_PREFIX, incr_handler);
    command_service_register_handler(g_cmd_srv, "decr ", 5, FIXED_PREFIX, decr_handler);
    command_service_register_handler(g_cmd_srv, "delete ", 7, FIXED_PREFIX, delete_handler);
    
    command_service_register_handler(g_cmd_srv, "own ", 4, FIXED_PREFIX, own_handler);
    command_service_register_handler(g_cmd_srv, "disown ", 7, FIXED_PREFIX, disown_handler);

    /* statistics commands */
    command_service_register_handler(g_cmd_srv, "stats", 5, FULL_MATCH, stats_handler);
    command_service_register_handler(g_cmd_srv, "stats reset", 11, FULL_MATCH, stats_reset_handler);
    command_service_register_handler(g_cmd_srv, "stats malloc", 12, FULL_MATCH, stats_malloc_handler);
    command_service_register_handler(g_cmd_srv, "stats maps", 10, FULL_MATCH, stats_maps_handler);
    command_service_register_handler(g_cmd_srv, "stats cachedump", 15, FULL_MATCH, stats_cachedump_handler);
    command_service_register_handler(g_cmd_srv, "stats slabs", 11, FULL_MATCH, stats_slabs_handler);
    command_service_register_handler(g_cmd_srv, "stats items", 11, FULL_MATCH, stats_items_handler);
    command_service_register_handler(g_cmd_srv, "stats sizes", 11, FULL_MATCH, stats_sizes_handler);
    
    /* other commands */
    command_service_register_handler(g_cmd_srv, "version", 7, FULL_MATCH, version_handler);
    command_service_register_handler(g_cmd_srv, "quit", 4, FULL_MATCH, quit_handler);
    command_service_register_handler(g_cmd_srv, "slabs reassign ", 15, FIXED_PREFIX, quit_handler);
    command_service_register_handler(g_cmd_srv, "flush_all", 9, FIXED_PREFIX, flush_all_handler);
    command_service_register_handler(g_cmd_srv, "bg ", 3, FIXED_PREFIX, bg_handler);

    /* save the PID in if we're a daemon */
    if (settings.daemonize && NULL != settings.pid_file) {
        FILE *fp;
        if (!(fp = fopen(settings.pid_file,"w"))) {
            fprintf(stderr,"Could not open the pid file %s for writing\n",settings.pid_file);
        } else {
            fprintf(fp,"%ld\n",(long) getpid());
            if (fclose(fp) < 0) {
                fprintf(stderr,"Could not close the pid file %s.\n",settings.pid_file);
            }
        }
    }

    /* enter the loop */
    aeMain(g_el);
    aeDeleteEventLoop(g_el);
    
    /* destory command service */
    command_service_destory(g_cmd_srv);

    /* remove the PID file if we're a daemon */
    if (settings.daemonize && NULL != settings.pid_file) {
        if (unlink(settings.pid_file) < 0) {
            fprintf(stderr,"Could not remove the pid file %s.\n",settings.pid_file);
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
movl %edi, %ebp
movq 0x5fe4(%rip), %rax      # 0xcff8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x2150
leaq -0x42(%rip), %rsi       # 0x6fe3
movl $0x2, %edi
callq 0x2240
leaq 0x6252(%rip), %rbx      # 0xd288
movq %rbx, %rdi
callq 0x777c
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x77c3
movq (%rbx), %rcx
movq 0x1312(%rip), %xmm0     # 0x8368
movl $0x30, %edi
movl $0x28, %esi
movl $0x100000, %edx         # imm = 0x100000
movl $0x1, %r8d
callq 0x4bfc
leaq 0x62c1(%rip), %rcx      # 0xd338
movq %rax, (%rcx)
movl $0x2800, %edi           # imm = 0x2800
callq 0x2aac
leaq 0x61ed(%rip), %r12      # 0xd278
movq %rax, (%r12)
cmpl $0x0, 0x44(%rbx)
je 0x70fc
movq %rsp, %rsi
movl $0x4, %edi
callq 0x2430
testl %eax, %eax
jne 0x70dc
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movdqa %xmm0, (%rsi)
movl $0x4, %edi
callq 0x2300
testl %eax, %eax
je 0x70dc
movq 0x8(%rsp), %rax
leaq 0x10(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq %rax, (%rsi)
movl $0x4, %edi
callq 0x2300
movq %rsp, %rsi
movl $0x4, %edi
callq 0x2430
testl %eax, %eax
jne 0x7727
cmpq $0x0, (%rsp)
je 0x7727
movq %rsp, %rsi
movl $0x7, %edi
callq 0x2430
testl %eax, %eax
jne 0x7718
movslq 0x8(%rbx), %rcx
movq (%rsp), %rax
cmpl %eax, %ecx
jle 0x7128
addq $0x3, %rcx
movq %rcx, (%rsp)
movq %rcx, %rax
cmpq %rax, 0x8(%rsp)
jae 0x7134
movq %rax, 0x8(%rsp)
movq %rsp, %rsi
movl $0x7, %edi
callq 0x2300
testl %eax, %eax
jne 0x771d
cmpq $0x0, 0x28(%rbx)
jne 0x7196
movl 0xc(%rbx), %edi
xorl %esi, %esi
callq 0x6c0c
leaq 0x610f(%rip), %rcx      # 0xd270
movl %eax, (%rcx)
cmpl $-0x1, %eax
je 0x7736
movl 0x10(%rbx), %edi
testl %edi, %edi
jle 0x7196
cmpq $0x0, 0x28(%rbx)
jne 0x7196
movl $0x1, %esi
callq 0x6c0c
leaq 0x60bd(%rip), %rcx      # 0xd248
movl %eax, (%rcx)
cmpl $-0x1, %eax
je 0x773b
callq 0x2140
testl %eax, %eax
je 0x71a8
callq 0x21a0
testl %eax, %eax
jne 0x71f0
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x76ba
cmpb $0x0, (%rdi)
je 0x76ba
callq 0x2250
testq %rax, %rax
je 0x76d2
movq %rax, %r14
movl 0x14(%rax), %edi
callq 0x2370
testl %eax, %eax
js 0x76c1
movl 0x10(%r14), %edi
callq 0x2440
testl %eax, %eax
js 0x76c1
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x7210
callq 0x6db6
leaq 0x606b(%rip), %rcx      # 0xd270
movl %eax, (%rcx)
cmpl $-0x1, %eax
je 0x772c
cmpl $0x0, 0x40(%rbx)
je 0x722a
movl 0x18(%rbx), %esi
movl 0x44(%rbx), %edi
callq 0x4434
cmpl $-0x1, %eax
je 0x76d9
callq 0x44d8
leaq 0x60aa(%rip), %rdi      # 0xd2e0
callq 0x7c0c
callq 0x3814
callq 0x3b6c
cmpl $0x0, 0x20(%rbx)
je 0x726a
movl $0x1, %edi
movl $0x20000, %esi          # imm = 0x20000
callq 0x2220
movq %rax, 0x74ef(%rip)      # 0xe750
testq %rax, %rax
je 0x7731
cmpl $0x0, 0x3c(%rbx)
jne 0x76c8
leaq 0x18(%rsp), %rdi
movq $0x1, -0x8(%rdi)
movl $0x0, 0x80(%rdi)
callq 0x2270
cmpl $-0x1, %eax
je 0x7713
leaq 0x10(%rsp), %rsi
movl $0xd, %edi
xorl %edx, %edx
callq 0x20c0
cmpl $-0x1, %eax
je 0x7713
leaq 0x5fb6(%rip), %rax      # 0xd270
movl (%rax), %edi
xorl %esi, %esi
movl $0x1, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x3b94
testq %rax, %rax
je 0x7722
movq (%r12), %rdi
leaq -0x396(%rip), %rdx      # 0x6f4e
xorl %r14d, %r14d
movl $0x3e8, %esi            # imm = 0x3E8
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3077
movl $0xc8, 0x745c(%rip)     # 0xe75c
movl $0x0, 0x744e(%rip)      # 0xe758
movl $0x640, %edi            # imm = 0x640
callq 0x22c0
movq %rax, 0x7445(%rip)      # 0xe760
movq (%r12), %rdi
leaq -0x3c4(%rip), %rdx      # 0x6f62
movl $0x3e8, %esi            # imm = 0x3E8
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3077
xorl %eax, %eax
callq 0x395c
leaq 0x5f3d(%rip), %r13      # 0xd280
movq %rax, (%r13)
leaq 0x1916(%rip), %rsi      # 0x8c64
leaq -0x1f0d(%rip), %r8      # 0x5448
movl $0x4, %edx
movq %rax, %rdi
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x18f1(%rip), %rsi      # 0x8c63
leaq -0x1f48(%rip), %r8      # 0x5431
movl $0x5, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x18d6(%rip), %rsi      # 0x8c69
leaq -0x1ddf(%rip), %r8      # 0x55bb
movl $0x4, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x18ba(%rip), %rsi      # 0x8c6e
leaq -0x1dee(%rip), %r8      # 0x55cd
movl $0x4, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x189e(%rip), %rsi      # 0x8c73
leaq -0x1e33(%rip), %r8      # 0x55a9
movl $0x8, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x1886(%rip), %rsi      # 0x8c7c
leaq -0x1c1b(%rip), %r8      # 0x57e2
movl $0x5, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x186b(%rip), %rsi      # 0x8c82
leaq -0x1c2a(%rip), %r8      # 0x57f4
movl $0x5, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x1850(%rip), %rsi      # 0x8c88
leaq -0x1c3c(%rip), %r8      # 0x5803
movl $0x7, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x183a(%rip), %rsi      # 0x8c93
leaq -0x1af5(%rip), %r8      # 0x596b
movl $0x4, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x1816(%rip), %rsi      # 0x8c90
leaq -0x1a94(%rip), %r8      # 0x59ed
movl $0x7, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x17fd(%rip), %rsi      # 0x8c98
leaq -0x1a38(%rip), %r8      # 0x5a6a
movl $0x5, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x17e5(%rip), %rsi      # 0x8c9e
leaq -0x17b8(%rip), %r8      # 0x5d08
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x17d3(%rip), %rsi      # 0x8caa
leaq -0x17b3(%rip), %r8      # 0x5d2b
movl $0xc, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x17c2(%rip), %rsi      # 0x8cb7
leaq -0x164a(%rip), %r8      # 0x5eb2
movl $0xa, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x17af(%rip), %rsi      # 0x8cc2
leaq -0x158b(%rip), %r8      # 0x5f8f
movl $0xf, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x17a1(%rip), %rsi      # 0x8cd2
leaq -0x151f(%rip), %r8      # 0x6019
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x178f(%rip), %rsi      # 0x8cde
leaq -0x153a(%rip), %r8      # 0x601c
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x177d(%rip), %rsi      # 0x8cea
leaq -0x1523(%rip), %r8      # 0x6051
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x176b(%rip), %rsi      # 0x8cf6
leaq -0x14e8(%rip), %r8      # 0x60aa
movl $0x7, %edx
xorl %ecx, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x1755(%rip), %rsi      # 0x8cfe
leaq -0x14f2(%rip), %r15     # 0x60be
movl $0x4, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x397d
movq (%r13), %rdi
leaq 0x1739(%rip), %rsi      # 0x8d03
movl $0xf, %edx
movl $0x1, %ecx
movq %r15, %r8
callq 0x397d
movq (%r13), %rdi
leaq 0x128c(%rip), %rsi      # 0x8873
leaq -0x151b(%rip), %r8      # 0x60d3
movl $0x9, %edx
movl $0x1, %ecx
callq 0x397d
movq (%r13), %rdi
leaq 0x170b(%rip), %rsi      # 0x8d13
leaq -0x14ab(%rip), %r8      # 0x6164
movl $0x3, %edx
movl $0x1, %ecx
callq 0x397d
cmpl $0x0, 0x40(%rbx)
setne %al
movq 0x50(%rbx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7677
leaq 0x11e2(%rip), %rsi      # 0x881f
callq 0x2390
testq %rax, %rax
je 0x76f9
movq %rax, %r15
callq 0x2100
movslq %eax, %rdx
leaq 0x16e6(%rip), %rsi      # 0x8d43
movq %r15, %rdi
xorl %eax, %eax
callq 0x2260
movq %r15, %rdi
callq 0x2110
testl %eax, %eax
js 0x7702
movq (%r12), %rdi
callq 0x35ee
movq (%r12), %rdi
callq 0x2e79
movq (%r13), %rdi
callq 0x3b35
cmpl $0x0, 0x40(%rbx)
setne %al
movq 0x50(%rbx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x76e4
callq 0x2050
testl %eax, %eax
jns 0x76e4
callq 0x2851
jmp 0x76e4
callq 0x2906
jmp 0x76de
callq 0x28b4
jmp 0x76de
callq 0x2802
jmp 0x7274
callq 0x28dd
jmp 0x76de
callq 0x27bb
movl $0x1, %r14d
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1617(%rip), %rdi      # 0x8d17
jmp 0x7709
leaq 0x163f(%rip), %rdi      # 0x8d48
callq 0x282c
jmp 0x7677
callq 0x289f
callq 0x2721
callq 0x2746
callq 0x287a
callq 0x26fc
callq 0x2796
callq 0x27dd
callq 0x2928
callq 0x276b