// File: /workspace/github_c_makefile_star2_O1/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
setge %al
jl 0x162b
movl %edi, %r15d
movl $0x1, %ebx
movq $0x0, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movb %al, -0x30(%rbp)
movl %ebx, %r13d
movq %rsi, %r14
movq (%rsi,%r13,8), %r12
movq %r12, %rdi
leaq 0xaa1(%rip), %rsi       # 0x2004
callq 0x10d0
testl %eax, %eax
je 0x15c8
movq %r12, %rdi
leaq 0xaab(%rip), %rsi       # 0x2021
callq 0x10d0
testl %eax, %eax
je 0x15e2
movq %r12, %rdi
leaq 0xab5(%rip), %rsi       # 0x203e
callq 0x10d0
testl %eax, %eax
je 0x15fc
movq %r12, %rdi
leaq 0xac1(%rip), %rsi       # 0x205d
callq 0x10d0
testl %eax, %eax
jne 0x1a5d
movq %r14, %rsi
leaq (%r14,%r13,8), %rax
addq $0x8, %rax
movq %rax, -0x58(%rbp)
notl %ebx
movl %ebx, %eax
addl %r15d, %eax
movq %rax, -0x50(%rbp)
movl %r15d, %ebx
jmp 0x1614
incl %ebx
cmpl %r15d, %ebx
je 0x1a54
movl %ebx, %eax
movq %r14, %rsi
movq (%r14,%rax,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x1614
incl %ebx
cmpl %r15d, %ebx
je 0x1a7a
movl %ebx, %eax
movq %r14, %rsi
movq (%r14,%rax,8), %rax
movq %rax, -0x48(%rbp)
jmp 0x1614
incl %ebx
cmpl %r15d, %ebx
je 0x1abb
movl %ebx, %eax
movq %r14, %rsi
movq (%r14,%rax,8), %rax
movq %rax, -0x40(%rbp)
incl %ebx
cmpl %r15d, %ebx
setl %al
jl 0x154c
xorl %r13d, %r13d
movq -0x40(%rbp), %r15
jmp 0x1651
movq $0x0, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
xorl %r13d, %r13d
testb $0x1, %al
jne 0x1a15
testq %r15, %r15
je 0x191f
leaq 0xa16(%rip), %rdi       # 0x207f
callq 0x1030
testq %rax, %rax
je 0x1926
movq %rax, %r14
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x193d
movl %eax, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movups %xmm0, -0x72(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movw $0x1, -0xd0(%rbp)
movq %r14, %rdi
callq 0x1090
addq $-0x6c, %rax
cmpq $-0x6e, %rax
jbe 0x1a27
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 0x1a2e
leaq 0x9d4(%rip), %rsi       # 0x20e9
movl %ebx, %edi
callq 0x1120
testq %rax, %rax
je 0x1a40
movq %rax, %rbx
movb $0x1, %al
movq -0x38(%rbp), %rdi
movl %r13d, %r14d
testb %al, %al
je 0x175b
xorl %r13d, %r13d
testq %rdi, %rdi
je 0x1763
leaq 0x9cc(%rip), %rsi       # 0x2110
callq 0x1140
movq %rax, %r12
testq %rax, %rax
jne 0x1766
callq 0x1262
jmp 0x1a0f
movl %r14d, %r13d
jmp 0x1a15
xorl %r12d, %r12d
callq 0x1070
callq 0x1190
testl %eax, %eax
je 0x178e
cmpl $-0x1, %eax
jne 0x1a15
leaq 0x9ab(%rip), %rdi       # 0x212f
callq 0x1150
jmp 0x1a0f
movq %rbx, -0x30(%rbp)
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11a0
cmpl $-0x1, %eax
je 0x1a83
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x10(%rsi)
movups %xmm0, 0x5e(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movw $0x1, (%rsi)
movups 0x960(%rip), %xmm0      # 0x213f
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 0x1a97
leaq 0x8dc(%rip), %rsi       # 0x20e9
movl %ebx, %edi
callq 0x1120
testq %rax, %rax
je 0x1aa0
movq %rax, %rbx
movb $0x1, %al
movl %r14d, %r13d
testb %al, %al
je 0x1a15
leaq 0x8dc(%rip), %rsi       # 0x2110
movq -0x48(%rbp), %rdi
callq 0x1140
movq %rax, %r13
callq 0x1190
testl %eax, %eax
je 0x1953
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x1ad9
leaq 0x922(%rip), %rsi       # 0x2182
movq %rbx, %rdi
movl %r14d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x10e0
cmpl $-0x1, %eax
je 0x1aed
movq %rbx, %rdi
callq 0x1110
testl %eax, %eax
jne 0x1b01
leaq -0xd0(%rbp), %rdi
movl $0x50, %esi
movq %rbx, %rdx
callq 0x10c0
testq %rax, %rax
je 0x1b0b
movq %rbx, %rdi
callq 0x1080
cmpl $0xa6b6f, -0xd0(%rbp)   # imm = 0xA6B6F
jne 0x1b15
leaq 0x966(%rip), %rsi       # 0x222b
movq %r13, %rbx
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10e0
movq %rbx, %rdi
callq 0x1080
leaq -0x5c(%rbp), %rbx
movl $0x41, (%rbx)
movl %r14d, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1130
cmpl $0x0, (%rbx)
leaq 0x92e(%rip), %rax       # 0x222f
leaq 0x858(%rip), %rsi       # 0x2160
cmoveq %rax, %rsi
movq -0x30(%rbp), %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1a15
callq 0x13e9
jmp 0x1932
leaq 0x75c(%rip), %rdi       # 0x2089
callq 0x1150
movl $0x1, %r13d
jmp 0x1a15
callq 0x1236
movl $0x41, %r14d
xorl %eax, %eax
movq -0x38(%rbp), %rdi
jmp 0x1731
leaq 0x8e7(%rip), %rdi       # 0x2241
callq 0x1060
movq -0x30(%rbp), %rdi
callq 0x1080
testq %r12, %r12
je 0x199d
movq %r12, %rdi
callq 0x1100
movl %eax, %edi
movl $0x2, %esi
callq 0x10a0
movq %r12, %rdi
callq 0x1100
movl %eax, %edi
movl $0x3, %esi
callq 0x10a0
movq %r12, %rdi
callq 0x1080
movq %rsp, %r14
movq -0x50(%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 -0x58(%rbp), %rsi
callq 0x10f0
movq $0x0, (%rbx,%r13,8)
leaq 0x861(%rip), %rdi       # 0x2249
callq 0x1060
movq (%r15,%r12), %rdi
movq 0x25e8(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %rbx, %rsi
callq 0x10b0
cmpl $-0x1, %eax
je 0x1af7
movq %r14, %rsp
movl $0x41, %r13d
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x121a
jmp 0x1a33
callq 0x11ed
movl $0x41, %r13d
xorl %eax, %eax
jmp 0x172a
callq 0x11c0
movl $0x41, %r13d
xorl %ebx, %ebx
xorl %eax, %eax
jmp 0x172a
leaq 0x5af(%rip), %rdi       # 0x200a
jmp 0x1ac2
movq 0x2594(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x5f2(%rip), %rsi       # 0x2060
movq %r12, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1ac7
leaq 0x5a6(%rip), %rdi       # 0x2027
jmp 0x1ac2
callq 0x12c4
movl $0x41, %r13d
xorl %eax, %eax
xorl %ebx, %ebx
jmp 0x1825
leaq 0x634(%rip), %rdi       # 0x20d2
jmp 0x1aa7
leaq 0x645(%rip), %rdi       # 0x20ec
callq 0x128f
movl $0x41, %r14d
xorl %eax, %eax
xorl %ebx, %ebx
jmp 0x1822
leaq 0x583(%rip), %rdi       # 0x2045
callq 0x1150
movl $0x1, %r13d
movq -0x40(%rbp), %r15
movb -0x30(%rbp), %al
jmp 0x1651
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x131e
jmp 0x1a0f
callq 0x13bc
jmp 0x1a0f
callq 0x12f1
jmp 0x1a0c
callq 0x1362
jmp 0x1a0f
callq 0x138f
jmp 0x1a0f
movq 0x24dc(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x6f3(%rip), %rsi       # 0x2219
leaq -0xd0(%rbp), %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1a0f