// File: /workspace/github_c_makefile_star2_O2/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
movq %rsi, %r13
movl %edi, %r15d
movq $0x0, -0x50(%rbp)
pushq $0x1
popq %rbx
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
cmpl %r15d, %ebx
jge 0x13ca
movl %ebx, %r14d
movq (%r13,%r14,8), %r12
movq %r12, %rdi
leaq 0xcf8(%rip), %rsi       # 0x2004
callq 0x10d0
testl %eax, %eax
je 0x137d
movq %r12, %rdi
leaq 0xd02(%rip), %rsi       # 0x2021
callq 0x10d0
testl %eax, %eax
je 0x1395
movq %r12, %rdi
leaq 0xd0c(%rip), %rsi       # 0x203e
callq 0x10d0
testl %eax, %eax
je 0x13ad
cmpb $0x2d, (%r12)
jne 0x17c0
cmpb $0x2d, 0x1(%r12)
jne 0x17c0
cmpb $0x0, 0x2(%r12)
jne 0x17c0
leaq 0x8(,%r14,8), %rax
addq %r13, %rax
movq %rax, -0x50(%rbp)
notl %ebx
movl %ebx, %eax
addl %r15d, %eax
movq %rax, -0x48(%rbp)
movl %r15d, %ebx
jmp 0x13c3
incl %ebx
cmpl %r15d, %ebx
je 0x180b
movl %ebx, %eax
movq (%r13,%rax,8), %rax
movq %rax, -0x30(%rbp)
jmp 0x13c3
incl %ebx
cmpl %r15d, %ebx
je 0x1842
movl %ebx, %eax
movq (%r13,%rax,8), %rax
movq %rax, -0x40(%rbp)
jmp 0x13c3
incl %ebx
cmpl %r15d, %ebx
je 0x184b
movl %ebx, %eax
movq (%r13,%rax,8), %rax
movq %rax, -0x38(%rbp)
incl %ebx
jmp 0x12f1
movq -0x38(%rbp), %r15
testq %r15, %r15
je 0x1756
leaq 0xc9e(%rip), %rdi       # 0x207c
callq 0x1030
testq %rax, %rax
je 0x1777
movq %rax, %r14
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x1734
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 0x1783
leaq -0xce(%rbp), %rdi
movq %r14, %rsi
callq 0x1050
leaq -0xd0(%rbp), %rsi
pushq $0x6e
popq %rdx
movl %ebx, %edi
callq 0x1160
cmpl $-0x1, %eax
je 0x17a1
leaq 0xc63(%rip), %rsi       # 0x20e6
movl %ebx, %edi
callq 0x1120
testq %rax, %rax
je 0x17dd
movq %rax, %r13
xorl %r14d, %r14d
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x14d8
leaq 0xc64(%rip), %rsi       # 0x210d
callq 0x1140
movq %rax, %rbx
testq %rax, %rax
jne 0x14da
movq 0x2b3b(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0xc3c(%rip), %rsi       # 0x210f
jmp 0x17fa
xorl %ebx, %ebx
callq 0x1070
callq 0x1190
testl %eax, %eax
je 0x1502
cmpl $-0x1, %eax
jne 0x185b
leaq 0xc34(%rip), %rdi       # 0x212c
callq 0x1150
jmp 0x1807
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x1734
movl %eax, %r14d
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)
andq $0x0, 0x66(%rsi)
movw $0x1, (%rsi)
movups 0xbf3(%rip), %xmm0      # 0x213c
movups %xmm0, 0x2(%rsi)
movabsq $0x6b636f732e6470, %rax # imm = 0x6B636F732E6470
movq %rax, 0xf(%rsi)
pushq $0x6e
popq %rdx
movl %r14d, %edi
callq 0x1160
cmpl $-0x1, %eax
je 0x1814
leaq 0xb70(%rip), %rsi       # 0x20e6
movl %r14d, %edi
callq 0x1120
testq %rax, %rax
je 0x181d
movq %rax, %r14
leaq 0xb7c(%rip), %rsi       # 0x210d
movq -0x40(%rbp), %rdi
callq 0x1140
movq %rax, -0x30(%rbp)
callq 0x1190
testl %eax, %eax
je 0x167b
movl %eax, %r12d
cmpl $-0x1, %eax
je 0x186d
leaq 0xbc1(%rip), %rsi       # 0x217f
movq %r14, %rdi
movl %r12d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x10e0
cmpl $-0x1, %eax
je 0x18a2
movq %r14, %rdi
callq 0x1110
testl %eax, %eax
jne 0x18f3
leaq -0xd0(%rbp), %rdi
pushq $0x50
popq %rsi
movq %r14, %rdx
callq 0x10c0
testq %rax, %rax
je 0x1915
movq %r14, %rdi
callq 0x1080
cmpl $0xa6b6f, -0xd0(%rbp)   # imm = 0xA6B6F
jne 0x1937
leaq 0xc07(%rip), %rsi       # 0x2228
xorl %r14d, %r14d
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x10e0
movq %rbx, %rdi
callq 0x1080
leaq -0x54(%rbp), %rbx
movl $0x41, (%rbx)
movl %r12d, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1130
cmpl $0x0, (%rbx)
leaq 0xbce(%rip), %rax       # 0x222c
leaq 0xaf8(%rip), %rsi       # 0x215d
cmoveq %rax, %rsi
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x185b
leaq 0xbbc(%rip), %rdi       # 0x223e
callq 0x1060
movq %r13, %rdi
callq 0x1080
testq %rbx, %rbx
je 0x16c0
movq %rbx, %rdi
callq 0x1100
pushq $0x2
popq %rsi
movl %eax, %edi
callq 0x10a0
movq %rbx, %rdi
callq 0x1100
pushq $0x3
popq %rsi
movl %eax, %edi
callq 0x10a0
movq %rbx, %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
andq $0x0, (%rbx,%r13,8)
leaq 0xb3e(%rip), %rdi       # 0x2246
callq 0x1060
movq (%r15,%r12), %rdi
movq 0x28c8(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %rbx, %rsi
callq 0x10b0
cmpl $-0x1, %eax
je 0x18c4
movq %r14, %rsp
jmp 0x1807
movq 0x28bd(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x948(%rip), %rsi       # 0x2099
jmp 0x17fa
movq 0x289b(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x907(%rip), %rdi       # 0x206e
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x1170
jmp 0x1857
leaq 0x908(%rip), %rdi       # 0x2086
jmp 0x1852
movq 0x286e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x922(%rip), %rdi       # 0x20b6
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x1170
jmp 0x1807
movq 0x2850(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x911(%rip), %rsi       # 0x20cf
jmp 0x17fa
movq 0x2831(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x88c(%rip), %rsi       # 0x205d
movq %r12, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1857
movq 0x2814(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x8ef(%rip), %rsi       # 0x20e9
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x41
jmp 0x1859
leaq 0x7f8(%rip), %rdi       # 0x200a
jmp 0x1852
leaq 0x8b4(%rip), %rbx       # 0x20cf
jmp 0x1824
leaq 0x8c5(%rip), %rbx       # 0x20e9
movq 0x27cd(%rip), %rax      # 0x3ff8
movq (%rax), %r14
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq %r14, %rdi
movq %rbx, %rsi
jmp 0x17fd
leaq 0x7de(%rip), %rdi       # 0x2027
jmp 0x1852
leaq 0x7f3(%rip), %rdi       # 0x2045
callq 0x1150
pushq $0x1
popq %r14
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8dd(%rip), %rdi       # 0x2151
callq 0x1150
leaq 0x8dd(%rip), %rsi       # 0x215d
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
movq %r13, %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1080
jmp 0x1807
movq 0x274f(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x8c7(%rip), %rsi       # 0x2186
jmp 0x17fa
movq 0x272d(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x88d(%rip), %rsi       # 0x216e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x172c
movq 0x26fe(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x8a4(%rip), %rsi       # 0x21b4
jmp 0x17fa
movq 0x26dc(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
callq 0x1040
movl (%rax), %edi
callq 0x1180
leaq 0x8ad(%rip), %rsi       # 0x21df
jmp 0x17fa
movq 0x26ba(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x8ce(%rip), %rsi       # 0x2216
leaq -0xd0(%rbp), %rdx
jmp 0x1800