// File: /workspace/github_c_makefile_star2_O1/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 0x5ef2(%rip), %rax      # 0xcff8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x2150
leaq -0x42(%rip), %rsi       # 0x70d5
movl $0x2, %edi
callq 0x2240
leaq 0x6160(%rip), %rbx      # 0xd288
movq %rbx, %rdi
callq 0x7890
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x78d7
movq (%rbx), %rcx
movq 0x1220(%rip), %xmm0     # 0x8368
movl $0x30, %edi
movl $0x28, %esi
movl $0x100000, %edx         # imm = 0x100000
movl $0x1, %r8d
callq 0x4c04
leaq 0x61cf(%rip), %rcx      # 0xd338
movq %rax, (%rcx)
movl $0x2800, %edi           # imm = 0x2800
callq 0x2aac
leaq 0x60fb(%rip), %r12      # 0xd278
movq %rax, (%r12)
cmpl $0x0, 0x44(%rbx)
je 0x71ee
movq %rsp, %rsi
movl $0x4, %edi
callq 0x2430
testl %eax, %eax
jne 0x71ce
pcmpeqd %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movdqa %xmm0, (%rsi)
movl $0x4, %edi
callq 0x2300
testl %eax, %eax
je 0x71ce
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 0x7838
cmpq $0x0, (%rsp)
je 0x7838
movq %rsp, %rsi
movl $0x7, %edi
callq 0x2430
testl %eax, %eax
jne 0x7829
movslq 0x8(%rbx), %rax
cmpl (%rsp), %eax
jle 0x7214
addq $0x3, %rax
movq %rax, (%rsp)
movq (%rsp), %rax
cmpq %rax, 0x8(%rsp)
jae 0x7224
movq %rax, 0x8(%rsp)
movq %rsp, %rsi
movl $0x7, %edi
callq 0x2300
testl %eax, %eax
jne 0x782e
cmpq $0x0, 0x28(%rbx)
jne 0x725c
movl 0xc(%rbx), %edi
xorl %esi, %esi
callq 0x6d09
leaq 0x601f(%rip), %rcx      # 0xd270
movl %eax, (%rcx)
cmpl $-0x1, %eax
je 0x784c
movl 0x10(%rbx), %edi
testl %edi, %edi
jle 0x7286
cmpq $0x0, 0x28(%rbx)
jne 0x7286
movl $0x1, %esi
callq 0x6d09
leaq 0x5fcd(%rip), %rcx      # 0xd248
movl %eax, (%rcx)
cmpl $-0x1, %eax
je 0x7842
callq 0x2140
testl %eax, %eax
je 0x7298
callq 0x21a0
testl %eax, %eax
jne 0x72e0
movq 0x48(%rbx), %rdi
testq %rdi, %rdi
je 0x77c2
cmpb $0x0, (%rdi)
je 0x77c2
callq 0x2250
testq %rax, %rax
je 0x77da
movq %rax, %r14
movl 0x14(%rax), %edi
callq 0x2370
testl %eax, %eax
js 0x77c9
movl 0x10(%r14), %edi
callq 0x2440
testl %eax, %eax
js 0x77c9
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x7300
callq 0x6eb1
leaq 0x5f7b(%rip), %rcx      # 0xd270
movl %eax, (%rcx)
cmpl $-0x1, %eax
je 0x783d
cmpl $0x0, 0x40(%rbx)
je 0x7328
movl 0x18(%rbx), %esi
movl 0x44(%rbx), %edi
callq 0x441c
movl %eax, %ebp
xorl %r14d, %r14d
cmpl $-0x1, %eax
je 0x77fa
cmpl $-0x1, %ebp
je 0x77e5
callq 0x44c0
leaq 0x5fac(%rip), %rdi      # 0xd2e0
callq 0x7d28
callq 0x37f4
callq 0x3b58
cmpl $0x0, 0x20(%rbx)
je 0x7372
movl $0x20000, %edi          # imm = 0x20000
callq 0x22c0
movq %rax, 0x73f6(%rip)      # 0xe750
testq %rax, %rax
je 0x7847
movl $0x20000, %edx          # imm = 0x20000
movq %rax, %rdi
xorl %esi, %esi
callq 0x2190
cmpl $0x0, 0x3c(%rbx)
jne 0x77d0
leaq 0x18(%rsp), %rdi
movq $0x1, -0x8(%rdi)
movl $0x0, 0x80(%rdi)
callq 0x2270
cmpl $-0x1, %eax
je 0x7824
leaq 0x10(%rsp), %rsi
movl $0xd, %edi
xorl %edx, %edx
callq 0x20c0
cmpl $-0x1, %eax
je 0x7824
leaq 0x5eae(%rip), %rax      # 0xd270
movl (%rax), %edi
xorl %esi, %esi
movl $0x1, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x3b80
testq %rax, %rax
je 0x7833
movq (%r12), %rdi
leaq -0x3a2(%rip), %rdx      # 0x704a
xorl %r14d, %r14d
movl $0x3e8, %esi            # imm = 0x3E8
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3050
movl $0xc8, 0x7354(%rip)     # 0xe75c
movl $0x0, 0x7346(%rip)      # 0xe758
movl $0x640, %edi            # imm = 0x640
callq 0x22c0
movq %rax, 0x733d(%rip)      # 0xe760
movq (%r12), %rdi
leaq -0x3d0(%rip), %rdx      # 0x705e
movl $0x3e8, %esi            # imm = 0x3E8
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3050
xorl %eax, %eax
callq 0x393c
leaq 0x5e35(%rip), %r13      # 0xd280
movq %rax, (%r13)
leaq 0x180e(%rip), %rsi      # 0x8c64
leaq -0x1fd3(%rip), %r8      # 0x548a
movl $0x4, %edx
movq %rax, %rdi
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x17e9(%rip), %rsi      # 0x8c63
leaq -0x200e(%rip), %r8      # 0x5473
movl $0x5, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x17ce(%rip), %rsi      # 0x8c69
leaq -0x1ea5(%rip), %r8      # 0x55fd
movl $0x4, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x17b2(%rip), %rsi      # 0x8c6e
leaq -0x1eb4(%rip), %r8      # 0x560f
movl $0x4, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1796(%rip), %rsi      # 0x8c73
leaq -0x1ef9(%rip), %r8      # 0x55eb
movl $0x8, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x177e(%rip), %rsi      # 0x8c7c
leaq -0x1cd4(%rip), %r8      # 0x5831
movl $0x5, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1763(%rip), %rsi      # 0x8c82
leaq -0x1ce3(%rip), %r8      # 0x5843
movl $0x5, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1748(%rip), %rsi      # 0x8c88
leaq -0x1cf5(%rip), %r8      # 0x5852
movl $0x7, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1732(%rip), %rsi      # 0x8c93
leaq -0x1bac(%rip), %r8      # 0x59bc
movl $0x4, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x170e(%rip), %rsi      # 0x8c90
leaq -0x1b4b(%rip), %r8      # 0x5a3e
movl $0x7, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x16f5(%rip), %rsi      # 0x8c98
leaq -0x1aef(%rip), %r8      # 0x5abb
movl $0x5, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x16dd(%rip), %rsi      # 0x8c9e
leaq -0x186f(%rip), %r8      # 0x5d59
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x16cb(%rip), %rsi      # 0x8caa
leaq -0x186a(%rip), %r8      # 0x5d7c
movl $0xc, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x16ba(%rip), %rsi      # 0x8cb7
leaq -0x1701(%rip), %r8      # 0x5f03
movl $0xa, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x16a7(%rip), %rsi      # 0x8cc2
leaq -0x1642(%rip), %r8      # 0x5fe0
movl $0xf, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1699(%rip), %rsi      # 0x8cd2
leaq -0x15d6(%rip), %r8      # 0x606a
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1687(%rip), %rsi      # 0x8cde
leaq -0x15f1(%rip), %r8      # 0x606d
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1675(%rip), %rsi      # 0x8cea
leaq -0x15da(%rip), %r8      # 0x60a2
movl $0xb, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1663(%rip), %rsi      # 0x8cf6
leaq -0x159f(%rip), %r8      # 0x60fb
movl $0x7, %edx
xorl %ecx, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x164d(%rip), %rsi      # 0x8cfe
leaq -0x15a9(%rip), %r15     # 0x610f
movl $0x4, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x395d
movq (%r13), %rdi
leaq 0x1631(%rip), %rsi      # 0x8d03
movl $0xf, %edx
movl $0x1, %ecx
movq %r15, %r8
callq 0x395d
movq (%r13), %rdi
leaq 0x1184(%rip), %rsi      # 0x8873
leaq -0x15d2(%rip), %r8      # 0x6124
movl $0x9, %edx
movl $0x1, %ecx
callq 0x395d
movq (%r13), %rdi
leaq 0x1603(%rip), %rsi      # 0x8d13
leaq -0x1562(%rip), %r8      # 0x61b5
movl $0x3, %edx
movl $0x1, %ecx
callq 0x395d
cmpl $0x0, 0x40(%rbx)
setne %al
movq 0x50(%rbx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x777f
leaq 0x10da(%rip), %rsi      # 0x881f
callq 0x2390
testq %rax, %rax
je 0x780a
movq %rax, %r15
callq 0x2100
movslq %eax, %rdx
leaq 0x15de(%rip), %rsi      # 0x8d43
movq %r15, %rdi
xorl %eax, %eax
callq 0x2260
movq %r15, %rdi
callq 0x2110
testl %eax, %eax
js 0x7813
movq (%r12), %rdi
callq 0x35d1
movq (%r12), %rdi
callq 0x2e6b
movq (%r13), %rdi
callq 0x3b20
cmpl $0x0, 0x40(%rbx)
setne %al
movq 0x50(%rbx), %rdi
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x77e5
callq 0x2050
testl %eax, %eax
jns 0x77e5
callq 0x2851
jmp 0x77e5
callq 0x2906
jmp 0x77df
callq 0x28b4
jmp 0x77df
callq 0x2802
jmp 0x737c
callq 0x28dd
movl $0x1, %r14d
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x27bb
movl $0x1, %r14d
jmp 0x731f
leaq 0x1506(%rip), %rdi      # 0x8d17
jmp 0x781a
leaq 0x152e(%rip), %rdi      # 0x8d48
callq 0x282c
jmp 0x777f
callq 0x289f
callq 0x2721
callq 0x2746
callq 0x287a
callq 0x26fc
callq 0x2796
callq 0x276b
callq 0x27dd
callq 0x2928
nopl (%rax)