// File: /workspace/github_c_makefile_star2_O0/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
subq $0x140, %rsp            # imm = 0x140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x1, %edi
callq 0x1200
movl %eax, 0x3d80(%rip)      # 0x7150
cmpl $-0x1, %eax
jne 0x3416
movq 0x3c1c(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1230
movq -0xf8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1372(%rip), %rsi      # 0x4775
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x3a1b
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1370(%rip), %rsi      # 0x47ac
callq 0x1130
cmpl $0x0, %eax
jne 0x377a
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x347f
movq 0x3b96(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1347(%rip), %rsi      # 0x47b3
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1250
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x34d7
movq 0x3b5b(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0x100(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1230
movq -0x100(%rbp), %rdi
movq %rax, %rdx
leaq 0x131b(%rip), %rsi      # 0x47df
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movl -0x1c(%rbp), %edx
leaq 0x131c(%rip), %rdi      # 0x4809
movb $0x0, %al
callq 0x10c0
leaq -0x8a(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x10e0
movw $0x1, -0x8a(%rbp)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1090
cmpq $0x6c, %rax
jbe 0x354b
movq 0x3aca(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x130d(%rip), %rsi      # 0x4845
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
leaq -0x8a(%rbp), %rdi
addq $0x2, %rdi
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movl $0x6b, %edx
callq 0x1050
movl -0x1c(%rbp), %edi
leaq -0x8a(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rsi
movl $0x6e, %edx
callq 0x11b0
cmpl $-0x1, %eax
jne 0x35c3
movq 0x3a5e(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x12d0(%rip), %rsi      # 0x4880
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movl -0x1c(%rbp), %edi
callq 0x1da0
movabsq $0x434f5354494e494c, %rax # imm = 0x434F5354494E494C
movq %rax, -0xa8(%rbp)
movl $0x3d4b43, -0xa1(%rbp)  # imm = 0x3D4B43
leaq -0xa8(%rbp), %rdi
callq 0x1090
movq %rax, -0x128(%rbp)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1090
movq -0x128(%rbp), %rcx
movq %rax, %rdx
leaq 0x1(%rcx,%rdx), %rax
movq %rsp, %rsi
movq %rsi, -0xb0(%rbp)
leaq 0x10(%rcx,%rdx), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x108(%rbp)
movq %rcx, %rsp
movq %rax, -0xb8(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1090
movq -0x120(%rbp), %rsi
movq -0x108(%rbp), %rdi
movq %rax, %rdx
callq 0x1150
leaq -0xa8(%rbp), %rdi
callq 0x1090
movq %rax, %rcx
movq -0x108(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x118(%rbp)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x110(%rbp)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1090
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x1150
movq -0x108(%rbp), %rdi
callq 0x1220
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x36ff
movq -0xc0(%rbp), %rdi
callq 0x1120
cmpl $0x0, %eax
je 0x3747
movq 0x38f2(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0x130(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1230
movq -0x130(%rbp), %rdi
movq %rax, %rdx
leaq 0x118d(%rip), %rsi      # 0x48ba
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
movl $0x1, -0xc4(%rbp)
jmp 0x375a
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0xc4(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, %rsp
movl -0xc4(%rbp), %eax
testl %eax, %eax
je 0x3775
jmp 0x3770
jmp 0x3b23
jmp 0x3a0b
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x115b(%rip), %rsi      # 0x48e8
callq 0x1130
cmpl $0x0, %eax
jne 0x3924
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x37d0
movq 0x3845(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1131(%rip), %rsi      # 0x48ee
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x11f0
movl %eax, -0xc8(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1250
movl %eax, -0xcc(%rbp)
cmpl $-0x1, -0xcc(%rbp)
jne 0x3848
movq 0x37ea(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0x138(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1230
movq -0x138(%rbp), %rdi
movq %rax, %rdx
leaq 0x10e4(%rip), %rsi      # 0x4919
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movl -0xc8(%rbp), %esi
movl -0xcc(%rbp), %edx
leaq 0x10e7(%rip), %rdi      # 0x4942
movb $0x0, %al
callq 0x10c0
leaq -0xdc(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10e0
movw $0x2, -0xdc(%rbp)
xorl %edi, %edi
callq 0x10d0
movl %eax, -0xd8(%rbp)
movl -0xc8(%rbp), %eax
movzwl %ax, %edi
callq 0x10a0
movw %ax, -0xda(%rbp)
movl -0xcc(%rbp), %edi
leaq -0xdc(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rsi
movl $0x10, %edx
callq 0x11b0
cmpl $-0x1, %eax
jne 0x390b
movq 0x3727(%rip), %rax      # 0x6ff8
movq (%rax), %rax
movq %rax, -0x140(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1230
movq -0x140(%rbp), %rdi
movq %rax, %rdx
leaq 0x107c(%rip), %rsi      # 0x4974
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movl -0xcc(%rbp), %edi
callq 0x1da0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3a09
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1064(%rip), %rsi      # 0x499b
callq 0x1130
cmpl $0x0, %eax
jne 0x39d7
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x397a
movq 0x369b(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x103e(%rip), %rsi      # 0x49a5
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x11f0
movl %eax, 0x37af(%rip)      # 0x7140
cmpl $0x1, 0x37a8(%rip)      # 0x7140
jl 0x39a6
cmpl $0x1000, 0x379c(%rip)   # imm = 0x1000
jle 0x39d5
movq 0x364b(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movl 0x378a(%rip), %edx      # 0x7140
leaq 0x1011(%rip), %rsi      # 0x49ce
movl $0x1000, %ecx           # imm = 0x1000
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
jmp 0x3a07
movq 0x361a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x101e(%rip), %rsi      # 0x4a12
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
jmp 0x3a09
jmp 0x3a0b
jmp 0x3a0d
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x341d
cmpl $0x0, -0x14(%rbp)
jne 0x3a45
movq 0x35d0(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xffa(%rip), %rsi       # 0x4a2c
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
callq 0x19f0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0x3a7f
movq 0x3596(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xfde(%rip), %rsi       # 0x4a4a
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
leaq 0xff9(%rip), %rdi       # 0x4a7f
callq 0x1220
movq -0xf0(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x3abc
movq 0x3556(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xf9e(%rip), %rsi       # 0x4a4a
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
movq -0xf0(%rbp), %rax
movl $0x1, 0x8(%rax)
leaq 0xfae(%rip), %rdi       # 0x4a7f
callq 0x2910
testb $0x1, %al
jne 0x3afb
movq 0x3517(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xf99(%rip), %rsi       # 0x4a84
movb $0x0, %al
callq 0x1140
movl $0x41, -0x4(%rbp)
jmp 0x3b23
leaq 0xfb9(%rip), %rdi       # 0x4abb
movb $0x0, %al
callq 0x10c0
callq 0x2340
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x11d0
jmp 0x3b09
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq