// File: /workspace/github_c_makefile_star2_O3/panux[P]linit/src/linit-sup.c
// Function: main
int main(int argc, char **argv) {
    //parse args
    char *logfile = NULL;
    char *pidf = NULL;
    char *name = NULL;
    int cmd_argc = 0;
    char **cmd_argv = NULL;
    for(int i = 1; i < argc; i++) {
        if(streq(argv[i], "--log")) {
            i++;
            if(i == argc) {
                perror("Missing arg for --log\n");
                return 1;
            }
            logfile = argv[i];
        } else if(streq(argv[i], "--pid")) {
            i++;
            if(i == argc) {
                perror("Missing arg for --pid\n");
                return 1;
            }
            pidf = argv[i];
        } else if(streq(argv[i], "--name")) {
            i++;
            if(i == argc) {
                perror("Missing arg for --name\n");
                return 1;
            }
            name = argv[i];
        } else if(streq(argv[i], "--")) {
            i++;
            cmd_argv = argv + i;
            cmd_argc = argc - i;
            i = argc;
        } else {
            fprintf(stderr, "Invalid arg: %s\n", argv[i]);
            return 1;
        }
    }
    if(name == NULL) {
        fprintf(stderr, "Name not set\n");
        return 1;
    }
    //get environment var
    char *sp = getenv("LINITSOCK");
    if(sp == NULL) {
        perror("LINITSOCK not set\n");
        return 1;
    }
    //connect to linitd
    FILE* linitdconn;
    {
        int fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if(fd == -1) {
            fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
            return 65;
        }
        struct sockaddr_un addr = { .sun_family = AF_UNIX };
        if((strlen(sp) + 1) > sizeof(addr.sun_path)) {
            fprintf(stderr, "Socket path is too long\n");
            return 65;
        }
        strcpy(addr.sun_path, sp);
        if(connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
            fprintf(stderr, "Failed to connect: %s\n", strerror(errno));
            return 65;
        }
        linitdconn = fdopen(fd, "r+");
        if(linitdconn == NULL) {
            fprintf(stderr, "Failed to convert fd to stream: %s\n", strerror(errno));
            return 65;
        }
    }
    //open log file
    FILE* log_file = NULL;
    if(logfile != NULL) {
        log_file = fopen(logfile, "w");
        if(log_file == NULL) {
            fprintf(stderr, "Failed to open log file: %s\n", strerror(errno));
            return 65;
        }
    }
    pid_t parentpid = getpid();
    pid_t childpid;
    switch(childpid = fork()) {
    case -1:
        perror("Failed to fork\n");
        return 65;
    case 0:
        {
            //open connection to supervisor daemon
            FILE* supconn = NULL;
            {
                int fd = socket(AF_UNIX, SOCK_STREAM, 0);
                if(fd == -1) {
                    fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
                    return 65;
                }
                struct sockaddr_un addr = { .sun_family = AF_UNIX };
                strcpy(addr.sun_path, "/tmp/linit-supd.sock");
                if(connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) {
                    fprintf(stderr, "Failed to connect: %s\n", strerror(errno));
                    return 65;
                }
                supconn = fdopen(fd, "r+");
                if(supconn == NULL) {
                    fprintf(stderr, "Failed to convert fd to stream: %s\n", strerror(errno));
                    return 65;
                }
            }
            FILE* pid_file = fopen(pidf, "w");
            switch(childpid = fork()) {
            case -1:
                perror("Fork failed");
                fprintf(linitdconn, "state %s failed\n", name);
                fclose(linitdconn);
                fclose(log_file);
                return 65;
            case 0:
                //set log_file as stdout and stderr
                printf("Execing\n");
                fclose(linitdconn);
                if(log_file != NULL) {
                    dup2(fileno(log_file), 2);
                    dup2(fileno(log_file), 3);
                    fclose(log_file);
                }
                char *exec_argv[cmd_argc + 1];
                memcpy(exec_argv, cmd_argv, sizeof(*cmd_argv) * cmd_argc);
                exec_argv[cmd_argc] = NULL;
                printf("Here we go!\n");
                if(execvpe(exec_argv[0], exec_argv, environ) == -1) {
                    fprintf(stderr, "exec failed: %s\n", strerror(errno));
                    return 65;
                }
                return 65;  //should never be run
            }
            if(fprintf(supconn, "*%d%s\n", (int)childpid, name) == -1) {
                fprintf(stderr, "Failed to write to linit-supd connection: %s\n", strerror(errno));
                return 65;
            }
            if(fflush(supconn) != 0) {
                fprintf(stderr, "Failed to flush linit-supd connection: %s\n", strerror(errno));
                return 65;
            }
            char resp[80];
            if(fgets(resp, 80, supconn) == NULL) {
                fprintf(stderr, "Failed to get response from linit-supd connection: %s\n", strerror(errno));
                return 65;
            }
            fclose(supconn);
            if(!streq(resp, "ok\n")) {
                fprintf(stderr, "Bad response: %s\n", resp);
                return 65;
            }
            fprintf(pid_file, "%d\n", childpid);
            fclose(pid_file);
            int code = 65;
            waitpid(childpid, &code, 0);
            if(code == 0) {
                fprintf(linitdconn, "state %s stopped\n", name);
            } else {
                fprintf(linitdconn, "state %s failed\n", name);
            }
            return 0;
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
cmpl $0x2, %edi
jl 0x198e
movl %edi, %r13d
movl $0x1, %ebx
xorl %r12d, %r12d
movq $0x0, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq %r12, -0x30(%rbp)
movl %ebx, %r14d
movq %rsi, %r15
movq (%rsi,%r14,8), %r12
movq %r12, %rdi
leaq 0xaa8(%rip), %rsi       # 0x2004
callq 0x10d0
testl %eax, %eax
je 0x15cd
movq %r12, %rdi
leaq 0xab2(%rip), %rsi       # 0x2021
callq 0x10d0
testl %eax, %eax
je 0x15e3
movq %r12, %rdi
leaq 0xabc(%rip), %rsi       # 0x203e
callq 0x10d0
testl %eax, %eax
je 0x1601
cmpb $0x2d, (%r12)
jne 0x19af
cmpb $0x2d, 0x1(%r12)
jne 0x19af
cmpb $0x0, 0x2(%r12)
jne 0x19af
movq %r15, %rsi
leaq (%r15,%r14,8), %rax
addq $0x8, %rax
movq %rax, -0x50(%rbp)
notl %ebx
movl %ebx, %eax
addl %r13d, %eax
movq %rax, -0x48(%rbp)
movl %r13d, %ebx
jmp 0x15fb
incl %ebx
cmpl %r13d, %ebx
je 0x19da
movl %ebx, %eax
movq %r15, %rsi
movq (%r15,%rax,8), %r12
jmp 0x161d
incl %ebx
cmpl %r13d, %ebx
je 0x1a0d
movl %ebx, %eax
movq %r15, %rsi
movq (%r15,%rax,8), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %r12
jmp 0x161d
incl %ebx
cmpl %r13d, %ebx
movq -0x30(%rbp), %r12
je 0x1a16
movq %r15, %rsi
movl %ebx, %eax
movq (%r15,%rax,8), %rax
movq %rax, -0x38(%rbp)
incl %ebx
cmpl %r13d, %ebx
jl 0x1544
movq -0x38(%rbp), %r15
testq %r15, %r15
je 0x198e
leaq 0xa40(%rip), %rdi       # 0x207c
callq 0x1030
testq %rax, %rax
je 0x1998
movq %rax, %r14
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x19a1
movl %eax, %ebx
xorps %xmm0, %xmm0
movups %xmm0, -0x72(%rbp)
movups %xmm0, -0x7e(%rbp)
movups %xmm0, -0x8e(%rbp)
movups %xmm0, -0x9e(%rbp)
movups %xmm0, -0xae(%rbp)
movups %xmm0, -0xbe(%rbp)
movups %xmm0, -0xce(%rbp)
movw $0x1, -0xd0(%rbp)
movq %r14, %rdi
callq 0x1090
addq $-0x6c, %rax
cmpq $-0x6e, %rax
jbe 0x19a8
leaq -0xce(%rbp), %rdi
movq %r14, %rsi
callq 0x1050
leaq -0xd0(%rbp), %rsi
movl %ebx, %edi
movl $0x6e, %edx
callq 0x1160
cmpl $-0x1, %eax
je 0x19cc
leaq 0x9fe(%rip), %rsi       # 0x20e6
movl %ebx, %edi
callq 0x1120
testq %rax, %rax
je 0x19d3
movq %rax, %r13
xorl %ebx, %ebx
testq %r12, %r12
je 0x1723
leaq 0xa04(%rip), %rsi       # 0x210d
movq %r12, %rdi
callq 0x1140
movq %rax, %r14
testq %rax, %rax
jne 0x1726
callq 0x11c0
jmp 0x1a06
xorl %r14d, %r14d
callq 0x1070
callq 0x1190
testl %eax, %eax
je 0x174e
cmpl $-0x1, %eax
jne 0x1a27
leaq 0x9e8(%rip), %rdi       # 0x212c
callq 0x1150
jmp 0x1a06
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x19e3
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %rsi
movups %xmm0, 0x16(%rsi)
movups %xmm0, 0x56(%rsi)
movups %xmm0, 0x46(%rsi)
movups %xmm0, 0x36(%rsi)
movups %xmm0, 0x26(%rsi)
movq $0x0, 0x66(%rsi)
movw $0x1, (%rsi)
movups 0x9a0(%rip), %xmm0      # 0x213c
movups %xmm0, 0x2(%rsi)
movabsq $0x6b636f732e6470, %rax # imm = 0x6B636F732E6470
movq %rax, 0xf(%rsi)
movl %ebx, %edi
movl $0x6e, %edx
callq 0x1160
cmpl $-0x1, %eax
je 0x19ea
leaq 0x91c(%rip), %rsi       # 0x20e6
movl %ebx, %edi
callq 0x1120
testq %rax, %rax
je 0x19f3
movq %rax, %rbx
leaq 0x929(%rip), %rsi       # 0x210d
movq -0x40(%rbp), %rdi
callq 0x1140
movq %rax, -0x30(%rbp)
callq 0x1190
testl %eax, %eax
je 0x18d1
movl %eax, %r12d
cmpl $-0x1, %eax
je 0x1a38
leaq 0x96e(%rip), %rsi       # 0x217f
movq %rbx, %rdi
movl %r12d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x10e0
cmpl $-0x1, %eax
je 0x1a48
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1a59
leaq -0xd0(%rbp), %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x10c0
testq %rax, %rax
je 0x1a60
movq %rbx, %rdi
callq 0x1080
cmpl $0xa6b6f, -0xd0(%rbp)   # imm = 0xA6B6F
jne 0x1a67
leaq 0x9b2(%rip), %rsi       # 0x2228
xorl %ebx, %ebx
movq -0x30(%rbp), %r14
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x10e0
movq %r14, %rdi
callq 0x1080
leaq -0x54(%rbp), %r14
movl $0x41, (%r14)
movl %r12d, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1130
cmpl $0x0, (%r14)
leaq 0x978(%rip), %rax       # 0x222c
leaq 0x8a2(%rip), %rsi       # 0x215d
cmoveq %rax, %rsi
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1a27
leaq 0x966(%rip), %rdi       # 0x223e
callq 0x1060
movq %r13, %rdi
callq 0x1080
testq %r14, %r14
je 0x191a
movq %r14, %rdi
callq 0x1100
movl %eax, %edi
movl $0x2, %esi
callq 0x10a0
movq %r14, %rdi
callq 0x1100
movl %eax, %edi
movl $0x3, %esi
callq 0x10a0
movq %r14, %rdi
callq 0x1080
movq %rsp, %r14
movq -0x48(%rbp), %rcx
leal 0x1(%rcx), %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %r12
andq $-0x10, %r12
movq %r15, %rbx
subq %r12, %rbx
movq %rbx, %rsp
negq %r12
movslq %ecx, %r13
leaq (,%r13,8), %rdx
movq %rbx, %rdi
movq -0x50(%rbp), %rsi
callq 0x10f0
movq $0x0, (%rbx,%r13,8)
leaq 0x8e1(%rip), %rdi       # 0x2246
callq 0x1060
movq (%r15,%r12), %rdi
movq 0x266b(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %rbx, %rsi
callq 0x10b0
cmpl $-0x1, %eax
je 0x1a4f
movq %r14, %rsp
jmp 0x1a06
callq 0x13e9
jmp 0x1a22
leaq 0x6e7(%rip), %rdi       # 0x2086
jmp 0x1a1d
callq 0x13bd
jmp 0x1a06
callq 0x13a1
jmp 0x1a06
movq 0x2642(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x69d(%rip), %rsi       # 0x205d
movq %r12, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1a22
callq 0x1374
jmp 0x1a06
callq 0x1347
jmp 0x1a06
leaq 0x629(%rip), %rdi       # 0x200a
jmp 0x1a1d
callq 0x131a
jmp 0x1a06
leaq 0x6de(%rip), %rdi       # 0x20cf
jmp 0x19fa
leaq 0x6ef(%rip), %rdi       # 0x20e9
leaq -0xd0(%rbp), %rsi
callq 0x12e5
movl $0x41, %ebx
jmp 0x1a27
leaq 0x613(%rip), %rdi       # 0x2027
jmp 0x1a1d
leaq 0x628(%rip), %rdi       # 0x2045
callq 0x1150
movl $0x1, %ebx
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x121a
jmp 0x1a06
callq 0x12b8
jmp 0x1a06
callq 0x11ed
jmp 0x1989
callq 0x125e
jmp 0x1a06
callq 0x128b
jmp 0x1a06
movq 0x258a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x79e(%rip), %rsi       # 0x2216
leaq -0xd0(%rbp), %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1a06