// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movl $0x1, %edi
callq 0x11f0
movl %eax, 0x446e(%rip)      # 0x7150
cmpl $-0x1, %eax
je 0x30c1
cmpl $0x2, %r12d
setge %r13b
jl 0x301c
movl $0x1, %r14d
movl $0x0, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movq %r15, -0x40(%rbp)
movl %r12d, -0x34(%rbp)
movslq %r14d, %rax
movq (%r15,%rax,8), %rbx
movq %rbx, %rdi
leaq 0x1a8a(%rip), %rsi      # 0x47b0
callq 0x1120
testl %eax, %eax
je 0x2d90
movq %rbx, %rdi
leaq 0x1bb3(%rip), %rsi      # 0x48ec
callq 0x1120
testl %eax, %eax
je 0x2ef1
movq %rbx, %rdi
leaq 0x1c4f(%rip), %rsi      # 0x499f
callq 0x1120
testl %eax, %eax
jne 0x30fd
incl %r14d
cmpl %r12d, %r14d
je 0x311a
movslq %r14d, %rax
movq (%r15,%rax,8), %rdi
callq 0x11e0
movl %eax, 0x43c5(%rip)      # 0x7140
movl $0xffffefff, %edx       # imm = 0xFFFFEFFF
leal (%rax,%rdx), %ecx
cmpl %edx, %ecx
ja 0x2fa0
jmp 0x3121
incl %r14d
cmpl %r12d, %r14d
je 0x3142
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1250
cmpl $-0x1, %eax
je 0x2fc5
movl %eax, %r12d
movb %r13b, -0x29(%rbp)
movslq %r14d, %rbx
movq (%r15,%rbx,8), %rsi
leaq 0x1a42(%rip), %rdi      # 0x480d
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movups %xmm0, -0x52(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movw $0x1, -0xb0(%rbp)
movq (%r15,%rbx,8), %r13
movq %r13, %rdi
callq 0x10a0
cmpq $0x6d, %rax
jae 0x2fd5
leaq (%r15,%rbx,8), %rbx
movl $0x6b, %edx
leaq -0xae(%rbp), %rdi
movq %r13, %rsi
callq 0x1050
movl %r12d, %edi
leaq -0xb0(%rbp), %rsi
movl $0x6e, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x2fdc
movl %r12d, %edi
callq 0x20e8
movq (%rbx), %rdi
callq 0x10a0
movq %rsp, %r15
movq %rsp, %rcx
addq $0x1a, %rax
andq $-0x10, %rax
movq %rcx, %r12
subq %rax, %r12
movq %r12, %rsp
negq %rax
movabsq $0x434f5354494e494c, %rdx # imm = 0x434F5354494E494C
movq %rdx, (%rcx,%rax)
movw $0x3d4b, 0x8(%r12)      # imm = 0x3D4B
leaq 0xa(%r12), %r13
movq (%rbx), %rbx
movq %rbx, %rdi
callq 0x10a0
leaq 0x1(%rax), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1140
movq %r12, %rdi
callq 0x1220
testq %rax, %rax
je 0x2fb2
movq %rax, %rdi
callq 0x1110
testl %eax, %eax
jne 0x2fb2
incl -0x38(%rbp)
movb $0x1, %al
movb -0x29(%rbp), %r13b
movl -0x34(%rbp), %r12d
movq %r15, %rsp
movq -0x40(%rbp), %r15
testb %al, %al
jne 0x2fa0
jmp 0x302a
incl %r14d
cmpl %r12d, %r14d
je 0x3149
movslq %r14d, %rax
movq (%r15,%rax,8), %rdi
callq 0x11e0
movl %eax, %r12d
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1250
cmpl $-0x1, %eax
je 0x2ffa
movl %eax, %ebx
leaq 0x1a17(%rip), %rdi      # 0x4946
movl %r12d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movw $0x2, -0xb0(%rbp)
movl $0x0, -0xac(%rbp)
rolw $0x8, %r12w
movw %r12w, -0xae(%rbp)
movl %ebx, %edi
leaq -0xb0(%rbp), %rsi
movl $0x10, %edx
callq 0x11a0
cmpl $-0x1, %eax
setne %r15b
je 0x300b
movl %ebx, %edi
callq 0x20e8
incl -0x38(%rbp)
movl -0x34(%rbp), %r12d
testb %r15b, %r15b
movq -0x40(%rbp), %r15
je 0x302a
incl %r14d
cmpl %r12d, %r14d
setl %r13b
jl 0x2d15
jmp 0x302a
callq 0x189b
movl $0x41, -0x30(%rbp)
xorl %eax, %eax
jmp 0x2ed5
callq 0x190e
movl $0x41, -0x30(%rbp)
xorl %eax, %eax
jmp 0x2ff1
callq 0x18ec
jmp 0x2fe4
movq %rbx, %rdi
callq 0x18c8
movl $0x41, -0x30(%rbp)
xorl %eax, %eax
movb -0x29(%rbp), %r13b
movl -0x34(%rbp), %r12d
jmp 0x2ee4
callq 0x184c
movl $0x41, -0x30(%rbp)
xorl %r15d, %r15d
jmp 0x2f8f
callq 0x181f
movl $0x41, -0x30(%rbp)
jmp 0x2f8f
movl $0x0, -0x38(%rbp)
movl $0x0, -0x30(%rbp)
testb $0x1, %r13b
je 0x3038
movl -0x30(%rbp), %eax
jmp 0x30d2
cmpl $0x0, -0x38(%rbp)
je 0x30c8
callq 0x1ec2
testq %rax, %rax
je 0x30e1
movq %rax, %rbx
leaq 0x1a29(%rip), %r14      # 0x4a83
movq %r14, %rdi
callq 0x1220
movq %rax, (%rbx)
testq %rax, %rax
je 0x30e8
movl $0x1, 0x8(%rbx)
leaq 0x1388(%rip), %rax      # 0x4400
leaq -0xb0(%rbp), %rsi
movq %rax, (%rsi)
movq %r14, 0x8(%rsi)
movl $0x2, %edi
callq 0x25da
movb $0x1, %cl
testb %al, %al
movl -0x30(%rbp), %eax
je 0x30ef
testb %cl, %cl
je 0x30d2
leaq 0x1a1b(%rip), %rdi      # 0x4abf
callq 0x1070
callq 0x236b
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl $0x1, %edx
callq 0x11c0
jmp 0x30a9
callq 0x19e5
jmp 0x30cd
callq 0x19c3
movl $0x41, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x19a1
jmp 0x30f4
callq 0x197f
jmp 0x30f4
callq 0x195d
movl $0x41, %eax
xorl %ecx, %ecx
jmp 0x3099
movq 0x3ef4(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1908(%rip), %rsi      # 0x4a16
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x314e
callq 0x17fd
jmp 0x314e
movq 0x3ed0(%rip), %rcx      # 0x6ff8
movq (%rcx), %rdi
leaq 0x18a0(%rip), %rsi      # 0x49d2
movl %eax, %edx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x1130
jmp 0x314e
callq 0x193b
jmp 0x314e
callq 0x1879
movl $0x41, -0x30(%rbp)
jmp 0x302a