// File: /workspace/github_c_makefile_star2_O2/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, -0x30(%rbp)
movl %edi, %r12d
pushq $0x1
popq %r14
movl %r14d, %edi
callq 0x11e0
movl %eax, 0x3a17(%rip)      # 0x6148
cmpl $-0x1, %eax
je 0x2a41
leaq -0xa6(%rbp), %rbx
xorl %r15d, %r15d
movl %r12d, -0x34(%rbp)
cmpl %r12d, %r14d
jge 0x29c2
movslq %r14d, %rax
movq -0x30(%rbp), %rcx
movq (%rcx,%rax,8), %r13
movq %r13, %rdi
leaq 0x102e(%rip), %rsi      # 0x3794
callq 0x1120
testl %eax, %eax
je 0x27d4
movq %r13, %rdi
leaq 0x1157(%rip), %rsi      # 0x38d0
callq 0x1120
testl %eax, %eax
je 0x2917
movq %r13, %rdi
leaq 0x11f3(%rip), %rsi      # 0x3983
callq 0x1120
testl %eax, %eax
jne 0x2ac5
incl %r14d
cmpl %r12d, %r14d
je 0x2ade
movslq %r14d, %rax
movq -0x30(%rbp), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x11d0
movl %eax, 0x3979(%rip)      # 0x6138
movl $0xffffefff, %edx       # imm = 0xFFFFEFFF
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
ja 0x29ba
jmp 0x2af6
incl %r14d
cmpl %r12d, %r14d
je 0x2b1a
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1240
cmpl $-0x1, %eax
je 0x2b2f
movl %eax, %r13d
movl %r15d, -0x38(%rbp)
movslq %r14d, %r12
movq -0x30(%rbp), %r15
movq (%r15,%r12,8), %rsi
leaq 0xfe3(%rip), %rdi       # 0x37f1
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
xorps %xmm0, %xmm0
movups %xmm0, 0x5c(%rbx)
movups %xmm0, 0x50(%rbx)
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
movw $0x1, -0xa8(%rbp)
movq (%r15,%r12,8), %r15
movq %r15, %rdi
callq 0x10a0
cmpq $0x6d, %rax
jae 0x2b51
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x6b
popq %rdx
callq 0x1050
movl %r13d, %edi
leaq -0xa8(%rbp), %rsi
pushq $0x6e
popq %rdx
callq 0x1190
cmpl $-0x1, %eax
je 0x2b66
movl %r13d, %edi
callq 0x1917
movq -0x30(%rbp), %rbx
movq (%rbx,%r12,8), %rdi
callq 0x10a0
movq %rbx, %rdx
movq %rsp, %rbx
addq $0x1a, %rax
andq $-0x10, %rax
movq %rsp, %rcx
movq %rcx, %r15
subq %rax, %r15
negq %rax
movq %r15, %rsp
movabsq $0x434f5354494e494c, %rsi # imm = 0x434F5354494E494C
movq %rsi, (%rcx,%rax)
movw $0x3d4b, 0x8(%r15)      # imm = 0x3D4B
leaq 0xa(%r15), %r13
movq (%rdx,%r12,8), %r12
movq %r12, %rdi
callq 0x10a0
leaq 0x1(%rax), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x1140
movq %r15, %rdi
callq 0x1210
testq %rax, %rax
je 0x2a93
movq %rax, %rdi
callq 0x1110
testl %eax, %eax
jne 0x2a93
movl -0x38(%rbp), %r15d
incl %r15d
movq %rbx, %rsp
movl -0x34(%rbp), %r12d
jmp 0x29b3
incl %r14d
cmpl %r12d, %r14d
je 0x2b81
movl %r15d, %ebx
movslq %r14d, %rax
movq -0x30(%rbp), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x11d0
movl %eax, %r15d
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1240
cmpl $-0x1, %eax
je 0x2b9f
movl %eax, %r13d
leaq 0xfd1(%rip), %rdi       # 0x392a
movl %r15d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
andq $0x0, -0xa0(%rbp)
movw $0x2, -0xa8(%rbp)
andl $0x0, -0xa4(%rbp)
rolw $0x8, %r15w
movw %r15w, -0xa6(%rbp)
movl %r13d, %edi
leaq -0xa8(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x1190
cmpl $-0x1, %eax
je 0x2bbe
movl %r13d, %edi
callq 0x1917
movl %ebx, %r15d
incl %r15d
leaq -0xa6(%rbp), %rbx
incl %r14d
jmp 0x2748
testl %r15d, %r15d
je 0x2a63
callq 0x16f7
testq %rax, %rax
je 0x2a29
movq %rax, %rbx
leaq 0x1088(%rip), %rdi      # 0x3a67
callq 0x1210
movq %rax, (%rbx)
testq %rax, %rax
je 0x2a29
movl $0x1, 0x8(%rbx)
leaq 0x106d(%rip), %rdi      # 0x3a67
callq 0x2066
testb %al, %al
je 0x2a7b
leaq 0x1099(%rip), %rdi      # 0x3aa3
callq 0x1070
pushq $-0x1
popq %rbx
pushq $0x1
popq %r14
callq 0x1cba
movl %ebx, %edi
xorl %esi, %esi
movl %r14d, %edx
callq 0x11b0
jmp 0x2a16
movq 0x35c8(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xff8(%rip), %rdi       # 0x3a32
pushq $0x34
jmp 0x2b94
movq 0x35b0(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1220
leaq 0xcff(%rip), %rsi       # 0x375d
jmp 0x2bdb
movq 0x358e(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xfa0(%rip), %rdi       # 0x3a14
pushq $0x1d
jmp 0x2b94
movq 0x3576(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xfe0(%rip), %rdi       # 0x3a6c
pushq $0x36
jmp 0x2b94
movq 0x355e(%rip), %rax      # 0x5ff8
movq (%rax), %r14
callq 0x1040
movl (%rax), %edi
callq 0x1220
leaq 0xdf2(%rip), %rsi       # 0x38a2
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rsp
jmp 0x2be8
movq 0x352c(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0xf24(%rip), %rsi       # 0x39fa
movq %r13, %rdx
jmp 0x2be1
movq 0x3513(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xe9e(%rip), %rdi       # 0x398d
pushq $0x28
jmp 0x2b94
movq 0x34fb(%rip), %rcx      # 0x5ff8
movq (%rcx), %rdi
leaq 0xeaf(%rip), %rsi       # 0x39b6
movl %eax, %edx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x1130
jmp 0x2be8
movq 0x34d7(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xc70(%rip), %rdi       # 0x379b
pushq $0x2b
jmp 0x2b94
movq 0x34c2(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1220
leaq 0xc7b(%rip), %rsi       # 0x37c7
jmp 0x2bdb
movq 0x34a0(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xccb(%rip), %rdi       # 0x382d
pushq $0x3a
jmp 0x2b94
movq 0x348b(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax,%r12,8), %rdx
leaq 0xce9(%rip), %rsi       # 0x3868
jmp 0x2be1
movq 0x3470(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0xd44(%rip), %rdi       # 0x38d6
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
jmp 0x2be8
movq 0x3452(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1220
leaq 0xd45(%rip), %rsi       # 0x3901
jmp 0x2bdb
movq 0x3433(%rip), %rax      # 0x5ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1220
leaq 0xd81(%rip), %rsi       # 0x395c
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1130
pushq $0x41
popq %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq