// File: /workspace/github_c_makefile_star2_O3/panux[P]linit/src/linitd.c
// Function: main
int main(int argc, char** argv) {
    int nservers = 0;
    if((epollfd = epoll_create(1)) == -1) {
        fprintf(stderr, "[FATAL] Failed to create an epoll file descriptor: %s\n", strerror(errno));
        return 65;
    }
    for(int i = 1; i < argc; i++) {
        if(streq(argv[i], "--unix")) {
            i++;
            if(i == argc) {
                fprintf(stderr, "[FATAL] Missing argument: unix socket path\n");
                return 65;
            }
            int fd = socket(AF_UNIX, SOCK_STREAM, 0);
            if(fd == -1) {
                fprintf(stderr, "[FATAL] Failed to create unix socket: %s\n", strerror(errno));
                return 65;
            }
            printf("[INFO] Starting unix domain socket server at %s with fd %d\n", argv[i], fd);
            struct sockaddr_un addr;
            memset(&addr, 0, sizeof(addr));
            addr.sun_family = AF_UNIX;
            if(strlen(argv[i]) > sizeof(addr.sun_path)) {
                fprintf(stderr, "[FATAL] Bad argument: unix domain socket path is too long\n");
                return 65;
            }
            strncpy(addr.sun_path, argv[i], sizeof(addr.sun_path)-1);
            if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
                fprintf(stderr, "[FATAL] Failed to bind unix domain socket: %s\n", argv[i]);
                return 65;
            }
            addSock(fd);
            //format: "LINITSOCK=%s", argv[i]
            char lsk[] = "LINITSOCK=";
            char envs[strlen(lsk) + strlen(argv[i]) + 1];
            memcpy(envs, lsk, strlen(lsk));
            memcpy(envs + strlen(lsk), argv[i], strlen(argv[i]) + 1);
            char *lsks = strdup(envs);
            if((lsks == NULL) || (putenv(lsks) != 0)) {
                fprintf(stderr, "[FATAL] Failed to set LINITSOCK variable: %s\n", strerror(errno));
                return 65;
            }
            nservers++;
        } else if(streq(argv[i], "--tcp")) {
            i++;
            if(i == argc) {
                fprintf(stderr, "[FATAL] Missing argument: tcp server port\n");
                return 65;
            }
            int port = atoi(argv[i]);
            int fd = socket(AF_INET, SOCK_STREAM, 0);
            if(fd == -1) {
                fprintf(stderr, "[FATAL] Failed to create tcp socket: %s\n", strerror(errno));
                return 65;
            }
            printf("[INFO] Starting tcp server on port %d with fd %d\n", port, fd);
            struct sockaddr_in addr;
            bzero((char *) &addr, sizeof(addr));
            addr.sin_family = AF_INET;
            addr.sin_addr.s_addr = htonl(INADDR_ANY);
            addr.sin_port = htons((unsigned short)port);
            if(bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
                fprintf(stderr, "[FATAL] Failed to bind tcp socket: %s\n", strerror(errno));
                return 65;
            }
            addSock(fd);
            nservers++;
        } else if(streq(argv[i], "--backlog")) {
            i++;
            if(i == argc) {
                fprintf(stderr, "[FATAL] Missing argument: backlog value\n");
                return 65;
            }
            backlog = atoi(argv[i]);
            if((backlog < 1) || (backlog > SOMAXCONN)) {
                fprintf(stderr, "[FATAL] backlog value %d out of range (should be between 1 and %d)\n", backlog, SOMAXCONN);
                return 65;
            }
        } else {
            fprintf(stderr, "[FATAL] Bad argument: %s\n", argv[i]);
            return 65;
        }
    }
    if(nservers == 0) {
        fprintf(stderr, "[FATAL] No servers specified\n");
        return 65;
    }
    {
        struct service *bs = newService();
        if(bs == NULL) {
            fprintf(stderr, "[FATAL] Failed to start boot service: out of memory\n");
            return 65;
        }
        if((bs->name = strdup("boot")) == NULL) {
            fprintf(stderr, "[FATAL] Failed to start boot service: out of memory\n");
            return 65;
        }
        bs->state = service_status_starting;
        if(!runStart("boot")) {
            fprintf(stderr, "[FATAL] Failed to start boot service: fork/exec error\n");
            return 65;
        }
    }
    printf("[INFO] Starting server\n");
    for(;;) {
        epoll_cycle();
        waitpid(-1, NULL, WNOHANG);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r15
movl %edi, %r14d
movl $0x1, %edi
callq 0x11e0
movl %eax, 0x44b5(%rip)      # 0x7148
cmpl $-0x1, %eax
je 0x2fb4
cmpl $0x2, %r14d
jl 0x2faa
movl $0x1, %ebx
movl $0x0, -0x2c(%rbp)
movq %r15, -0x40(%rbp)
movl %r14d, -0x30(%rbp)
movslq %ebx, %rax
movq (%r15,%rax,8), %r12
movq %r12, %rdi
leaq 0x1ac9(%rip), %rsi      # 0x4794
callq 0x1120
testl %eax, %eax
je 0x2d34
movq %r12, %rdi
leaq 0x1bf2(%rip), %rsi      # 0x48d0
callq 0x1120
testl %eax, %eax
je 0x2e8a
movq %r12, %rdi
leaq 0x1c8e(%rip), %rsi      # 0x4983
callq 0x1120
testl %eax, %eax
jne 0x2fed
incl %ebx
cmpl %r14d, %ebx
je 0x300a
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
callq 0x11d0
movl %eax, 0x4419(%rip)      # 0x7138
movl $0xffffefff, %edx       # imm = 0xFFFFEFFF
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
ja 0x2f27
jmp 0x3011
incl %ebx
cmpl %r14d, %ebx
je 0x3032
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1240
cmpl $-0x1, %eax
je 0x3039
movslq %ebx, %r14
movq (%r15,%r14,8), %rsi
leaq 0x1a8a(%rip), %rdi      # 0x47f1
movl %eax, -0x34(%rbp)
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
leaq -0xae(%rbp), %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x5c(%r13)
movups %xmm0, 0x50(%r13)
movups %xmm0, 0x40(%r13)
movups %xmm0, 0x30(%r13)
movups %xmm0, 0x20(%r13)
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
movw $0x1, -0xb0(%rbp)
movq %r15, %r12
movq (%r15,%r14,8), %r15
movq %r15, %rdi
callq 0x10a0
cmpq $0x6d, %rax
jae 0x3040
leaq (%r12,%r14,8), %r12
movl $0x6b, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x1050
movl -0x34(%rbp), %r14d
movl %r14d, %edi
leaq -0xb0(%rbp), %rsi
movl $0x6e, %edx
callq 0x1190
cmpl $-0x1, %eax
je 0x3047
movl %r14d, %edi
callq 0x206c
movq (%r12), %rdi
callq 0x10a0
movq %rsp, %r13
movq %rsp, %rcx
addq $0x1a, %rax
andq $-0x10, %rax
movq %rcx, %r15
subq %rax, %r15
movq %r15, %rsp
negq %rax
movabsq $0x434f5354494e494c, %rdx # imm = 0x434F5354494E494C
movq %rdx, (%rcx,%rax)
movw $0x3d4b, 0x8(%r15)      # imm = 0x3D4B
leaq 0xa(%r15), %r14
movq (%r12), %r12
movq %r12, %rdi
callq 0x10a0
leaq 0x1(%rax), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1140
movq %r15, %rdi
callq 0x1210
testq %rax, %rax
je 0x2fdc
movq %rax, %rdi
callq 0x1110
testl %eax, %eax
jne 0x2fdc
incl -0x2c(%rbp)
movq %r13, %rsp
movq -0x40(%rbp), %r15
movl -0x30(%rbp), %r14d
jmp 0x2f27
incl %ebx
cmpl %r14d, %ebx
je 0x3051
movslq %ebx, %rax
movq (%r15,%rax,8), %rdi
callq 0x11d0
movl %eax, %r13d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1240
cmpl $-0x1, %eax
je 0x3058
movl %eax, %r12d
leaq 0x1a62(%rip), %rdi      # 0x492a
movl %r13d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
movq $0x0, -0xa8(%rbp)
movw $0x2, -0xb0(%rbp)
movl $0x0, -0xac(%rbp)
rolw $0x8, %r13w
movw %r13w, -0xae(%rbp)
movl %r12d, %edi
leaq -0xb0(%rbp), %rsi
movl $0x10, %edx
callq 0x1190
cmpl $-0x1, %eax
je 0x305f
movl %r12d, %edi
callq 0x206c
incl -0x2c(%rbp)
incl %ebx
cmpl %r14d, %ebx
jl 0x2cba
cmpl $0x0, -0x2c(%rbp)
je 0x2faa
callq 0x1e76
testq %rax, %rax
je 0x2fbe
movq %rax, %rbx
leaq 0x1b1b(%rip), %r14      # 0x4a67
movq %r14, %rdi
callq 0x1210
movq %rax, (%rbx)
testq %rax, %rax
je 0x2fc8
movl $0x1, 0x8(%rbx)
leaq 0x147a(%rip), %rax      # 0x43e4
leaq -0xb0(%rbp), %rsi
movq %rax, (%rsi)
movq %r14, 0x8(%rsi)
movl $0x2, %edi
callq 0x258b
testb %al, %al
je 0x2fd2
leaq 0x1b16(%rip), %rdi      # 0x4aa3
callq 0x1070
callq 0x2331
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl $0x1, %edx
callq 0x11b0
jmp 0x2f92
callq 0x18f1
jmp 0x306b
callq 0x19e4
jmp 0x306b
callq 0x18cf
jmp 0x306b
callq 0x18ad
jmp 0x306b
callq 0x188b
jmp 0x306b
leaq -0xb0(%rbp), %rsi
movq %r13, %rdi
callq 0x1913
jmp 0x306b
movq 0x4004(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x19fc(%rip), %rsi      # 0x49fa
movq %r12, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x306b
callq 0x17ed
jmp 0x306b
movq 0x3fe0(%rip), %rcx      # 0x6ff8
movq (%rcx), %rdi
leaq 0x1994(%rip), %rsi      # 0x49b6
movl %eax, %edx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x1130
jmp 0x306b
callq 0x19c2
jmp 0x306b
callq 0x1995
jmp 0x306b
callq 0x1973
jmp 0x306b
movq %r12, %rdi
callq 0x194f
jmp 0x306b
callq 0x1869
jmp 0x306b
callq 0x183c
jmp 0x306b
leaq -0xb0(%rbp), %rdi
callq 0x180f
movl $0x41, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq