// File: /workspace/github_c_makefile_star2_O0/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
subq $0x260, %rsp            # imm = 0x260
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x14a1
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xcee(%rip), %rsi       # 0x2004
callq 0x10e0
cmpl $0x0, %eax
jne 0x135e
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x1349
leaq 0xcd2(%rip), %rdi       # 0x200a
callq 0x1160
movl $0x1, -0x4(%rbp)
jmp 0x1c65
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
jmp 0x1491
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xcb0(%rip), %rsi       # 0x2021
callq 0x10e0
cmpl $0x0, %eax
jne 0x13b9
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x13a4
leaq 0xc94(%rip), %rdi       # 0x2027
callq 0x1160
movl $0x1, -0x4(%rbp)
jmp 0x1c65
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
jmp 0x148f
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xc72(%rip), %rsi       # 0x203e
callq 0x10e0
cmpl $0x0, %eax
jne 0x1411
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jne 0x13ff
leaq 0xc57(%rip), %rdi       # 0x2045
callq 0x1160
movl $0x1, -0x4(%rbp)
jmp 0x1c65
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x148d
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xc39(%rip), %rsi       # 0x205d
callq 0x10e0
cmpl $0x0, %eax
jne 0x145b
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x8(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x8(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x148b
movq 0x2b96(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0xbe8(%rip), %rsi       # 0x2060
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x1c65
jmp 0x148d
jmp 0x148f
jmp 0x1491
jmp 0x1493
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x12f7
cmpq $0x0, -0x28(%rbp)
jne 0x14cc
movq 0x2b49(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbb8(%rip), %rsi       # 0x2071
movb $0x0, %al
callq 0x10f0
movl $0x1, -0x4(%rbp)
jmp 0x1c65
leaq 0xbac(%rip), %rdi       # 0x207f
callq 0x1030
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x14fb
leaq 0xb9f(%rip), %rdi       # 0x2089
callq 0x1160
movl $0x1, -0x4(%rbp)
jmp 0x1c65
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x11a0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x1553
movq 0x2adf(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x1f0(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x1f0(%rbp), %rdi
movq %rax, %rdx
leaq 0xb5c(%rip), %rsi       # 0x209c
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
leaq -0xc2(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x10c0
movw $0x1, -0xc2(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1080
addq $0x1, %rax
cmpq $0x6c, %rax
jbe 0x15a6
movq 0x2a6f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb26(%rip), %rsi       # 0x20b9
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
leaq -0xc2(%rbp), %rdi
addq $0x2, %rdi
movq -0x48(%rbp), %rsi
callq 0x1050
movl -0x54(%rbp), %edi
leaq -0xc2(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rsi
movl $0x6e, %edx
callq 0x1170
cmpl $-0x1, %eax
jne 0x1622
movq 0x2a10(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x1f8(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x1f8(%rbp), %rdi
movq %rax, %rdx
leaq 0xac3(%rip), %rsi       # 0x20d2
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movl -0x54(%rbp), %edi
leaq 0xabd(%rip), %rsi       # 0x20e9
callq 0x1130
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x167d
movq 0x29b5(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x200(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x200(%rbp), %rdi
movq %rax, %rdx
leaq 0xa82(%rip), %rsi       # 0x20ec
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movq $0x0, -0xd8(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x16f3
movq -0x18(%rbp), %rdi
leaq 0xa76(%rip), %rsi       # 0x2110
callq 0x1150
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0x16f1
movq 0x2941(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x208(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x208(%rbp), %rdi
movq %rax, %rdx
leaq 0xa34(%rip), %rsi       # 0x2112
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
jmp 0x16f3
callq 0x1060
movl %eax, -0xdc(%rbp)
callq 0x1190
movl %eax, -0xe0(%rbp)
movl %eax, %ecx
movl %ecx, -0x20c(%rbp)
subl $-0x1, %eax
je 0x1727
jmp 0x1718
movl -0x20c(%rbp), %eax
testl %eax, %eax
je 0x173f
jmp 0x1c5e
leaq 0xa01(%rip), %rdi       # 0x212f
callq 0x1160
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movq $0x0, -0xe8(%rbp)
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x11a0
movl %eax, -0xec(%rbp)
cmpl $-0x1, -0xec(%rbp)
jne 0x17a8
movq 0x288a(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x218(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x218(%rbp), %rdi
movq %rax, %rdx
leaq 0x907(%rip), %rsi       # 0x209c
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
leaq -0x15a(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x10c0
movw $0x1, -0x15a(%rbp)
leaq -0x15a(%rbp), %rdi
addq $0x2, %rdi
leaq 0x969(%rip), %rsi       # 0x213f
callq 0x1050
movl -0xec(%rbp), %edi
leaq -0x15a(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rsi
movl $0x6e, %edx
callq 0x1170
cmpl $-0x1, %eax
jne 0x1846
movq 0x27ec(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x220(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x220(%rbp), %rdi
movq %rax, %rdx
leaq 0x89f(%rip), %rsi       # 0x20d2
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movl -0xec(%rbp), %edi
leaq 0x896(%rip), %rsi       # 0x20e9
callq 0x1130
movq %rax, -0xe8(%rbp)
cmpq $0x0, -0xe8(%rbp)
jne 0x18aa
movq 0x2788(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x228(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x228(%rbp), %rdi
movq %rax, %rdx
leaq 0x855(%rip), %rsi       # 0x20ec
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movq -0x20(%rbp), %rdi
leaq 0x85b(%rip), %rsi       # 0x2110
callq 0x1150
movq %rax, -0x170(%rbp)
callq 0x1190
movl %eax, -0xe0(%rbp)
movl %eax, %ecx
movl %ecx, -0x22c(%rbp)
subl $-0x1, %eax
je 0x18ea
jmp 0x18db
movl -0x22c(%rbp), %eax
testl %eax, %eax
je 0x192d
jmp 0x1a74
leaq 0x863(%rip), %rdi       # 0x2154
callq 0x1160
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x85b(%rip), %rsi       # 0x2160
movb $0x0, %al
callq 0x10f0
movq -0x50(%rbp), %rdi
callq 0x1070
movq -0xd8(%rbp), %rdi
callq 0x1070
movl $0x41, -0x4(%rbp)
jmp 0x1c65
leaq 0x83d(%rip), %rdi       # 0x2171
movb $0x0, %al
callq 0x10b0
movq -0x50(%rbp), %rdi
callq 0x1070
cmpq $0x0, -0xd8(%rbp)
je 0x198a
movq -0xd8(%rbp), %rdi
callq 0x1110
movl %eax, %edi
movl $0x2, %esi
callq 0x1090
movq -0xd8(%rbp), %rdi
callq 0x1110
movl %eax, %edi
movl $0x3, %esi
callq 0x1090
movq -0xd8(%rbp), %rdi
callq 0x1070
movl -0x2c(%rbp), %eax
incl %eax
movq %rsp, %rcx
movq %rcx, -0x178(%rbp)
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x238(%rbp)
movq %rdi, %rsp
movq %rax, -0x180(%rbp)
movq -0x38(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
shlq $0x3, %rdx
callq 0x1100
movq -0x238(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
leaq 0x793(%rip), %rdi       # 0x217a
movb $0x0, %al
callq 0x10b0
movq -0x238(%rbp), %rsi
movq (%rsi), %rdi
movq 0x25e1(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
callq 0x10a0
cmpl $-0x1, %eax
jne 0x1a54
movq 0x25e5(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x240(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x240(%rbp), %rdi
movq %rax, %rdx
leaq 0x74d(%rip), %rsi       # 0x2187
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
movl $0x1, -0x184(%rbp)
jmp 0x1a65
movl $0x41, -0x4(%rbp)
movl $0x1, -0x184(%rbp)
movq -0x178(%rbp), %rax
movq %rax, %rsp
jmp 0x1c65
movq -0xe8(%rbp), %rdi
movl -0xe0(%rbp), %edx
movq -0x28(%rbp), %rcx
leaq 0x70c(%rip), %rsi       # 0x2198
movb $0x0, %al
callq 0x10f0
cmpl $-0x1, %eax
jne 0x1ad9
movq 0x2559(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x248(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x248(%rbp), %rdi
movq %rax, %rdx
leaq 0x6d9(%rip), %rsi       # 0x219f
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movq -0xe8(%rbp), %rdi
callq 0x1120
cmpl $0x0, %eax
je 0x1b2b
movq 0x2507(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x250(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x250(%rbp), %rdi
movq %rax, %rdx
leaq 0x6b5(%rip), %rsi       # 0x21cd
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
leaq -0x1e0(%rbp), %rdi
movq -0xe8(%rbp), %rdx
movl $0x50, %esi
callq 0x10d0
cmpq $0x0, %rax
jne 0x1b8a
movq 0x24a8(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x258(%rbp)
callq 0x1040
movl (%rax), %edi
callq 0x1180
movq -0x258(%rbp), %rdi
movq %rax, %rdx
leaq 0x681(%rip), %rsi       # 0x21f8
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movq -0xe8(%rbp), %rdi
callq 0x1070
leaq -0x1e0(%rbp), %rdi
leaq 0x68b(%rip), %rsi       # 0x222f
callq 0x10e0
cmpl $0x0, %eax
je 0x1bd9
movq 0x2443(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq -0x1e0(%rbp), %rdx
leaq 0x66d(%rip), %rsi       # 0x2233
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c65
movq -0x170(%rbp), %rdi
movl -0xe0(%rbp), %edx
leaq 0x658(%rip), %rsi       # 0x2245
movb $0x0, %al
callq 0x10f0
movq -0x170(%rbp), %rdi
callq 0x1070
movl $0x41, -0x1e4(%rbp)
movl -0xe0(%rbp), %edi
leaq -0x1e4(%rbp), %rsi
xorl %edx, %edx
callq 0x1140
cmpl $0x0, -0x1e4(%rbp)
jne 0x1c3f
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x613(%rip), %rsi       # 0x2249
movb $0x0, %al
callq 0x10f0
jmp 0x1c55
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x512(%rip), %rsi       # 0x2160
movb $0x0, %al
callq 0x10f0
movl $0x0, -0x4(%rbp)
jmp 0x1c65
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq