// File: /workspace/github_c_makefile_star2_O0/panux[P]linit/src/linit-supd.c
// Function: main
int main(int argc, char **argv) {
    if(argc != 1) {
        perror("[FATAL] Should have no arguments\n");
        return 1;
    }
    //create socket
    int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    {
        if(sockfd == -1) {
            fprintf(stderr, "[FATAL] Failed to create unix socket: %s\n", strerror(errno));
            return 65;
        }
        struct sockaddr_un addr;
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, "/tmp/linit-supd.sock", sizeof(addr.sun_path)-1);
        if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
            fprintf(stderr, "[FATAL] Failed to bind unix domain socket: %s\n", strerror(errno));
            return 65;
        }
    }
    //connect to linitd
    FILE* linitdconn;
    {
        char *sp = getenv("LINITSOCK");
        if(sp == NULL) {
            fprintf(stderr, "[FATAL] LINITSOCK not set\n");
            return 65;
        }
        int fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if(fd == -1) {
            fprintf(stderr, "[FATAL] 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, "[FATAL] 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, "[FATAL] Failed to connect: %s\n", strerror(errno));
            return 65;
        }
        linitdconn = fdopen(fd, "r+");
        if(linitdconn == NULL) {
            fprintf(stderr, "[FATAL] Failed to convert fd to stream\n");
            return 65;
        }
    }
    //chain (starts empty)
    struct chain *ch = NULL;
    //listen
    if(listen(sockfd, 16) == -1) {
        fprintf(stderr, "[FATAL] Failed to listen: %s\n", strerror(errno));
        return 65;
    }
    //handle connections
    printf("Starting linit-supd\n");
    for(;;) {
        printf("Accepting\n");
        int fd = accept(sockfd, NULL, NULL);
        printf("Accepted\n");
        if(fd == -1) {  //something went wrong - notify linitd and exit
            fprintf(stderr, "[FATAL] Failed to accept connection: %s\n", strerror(errno));
            if(fprintf(linitdconn, "state linit-supd failed\n") < 0) {  //failed to notify linitd
                fprintf(stderr, "[FATAL] Failed to notify linitd of failure: %s\n", strerror(errno));
            } else {
                fclose(linitdconn);
            }
            return 65;
        }
        FILE *stream = fdopen(fd, "r+");
        if(stream == NULL) {
            fprintf(stderr, "[ERROR] Failed to open stream: %s\n", strerror(errno));
            close(fd);
            continue;
        }
        char buf[1024];
        if(fgets(buf, 1024, stream) == NULL) {
            fprintf(stderr, "[ERROR] Failed to read from stream: %s\n", strerror(errno));
            fclose(stream);
            continue;
        }
        //strip trailing newline
        if(buf[strlen(buf) - 1] == '\n') {
            buf[strlen(buf) - 1] = '\0';
        } else {
            perror("[ERROR] Oversized command\n");
            fclose(stream);
            continue;
        }
        if(strlen(buf) < 2) {
            perror("[ERROR] Short command\n");
            fclose(stream);
            continue;
        }
        char cmd = buf[0];
        char *arg = buf + 1;
        printf("Recieved %s\n", buf);
        switch(cmd) {
        case '*':   //new supervision
            {
                char *pidnum = arg;
                char *name = arg;
                while(isdigit(*name)) { name++; }
                if(name == arg) {
                    perror("[ERROR] Command missing process ID\n");
                    fclose(stream);
                    continue;
                }
                struct chain **chainend = &ch;
                while(*chainend != NULL) { chainend = &((*chainend)->next); }
                struct chain *link = malloc(sizeof(struct chain));
                if(link == NULL) {
                    perror("[ERROR] OOM\n");
                    fclose(stream);
                    continue;
                }
                link->name = strdup(name);
                if(link->name == NULL) {
                    perror("[ERROR] OOM\n");
                    free(link);
                    fclose(stream);
                    continue;
                }
                *name = '\0';
                link->pid = atoi(pidnum);
                printf("Name: %s PID: %d\n", link->name, (int)link->pid);
                *chainend = link;
            }
            break;
        case '^':   //terminate & delete supervision
            {
                char *signum = arg;
                char *name = arg;
                while(isdigit(*name)) { name++; }
                if(name == arg) {
                    perror("[ERROR] Command missing signal number\n");
                    fclose(stream);
                    continue;
                }
                //find in chain
                struct chain **chainpos = &ch;
                while((*chainpos != NULL) && !streq(name, (*chainpos)->name)) {
                    chainpos = &((*chainpos)->next);
                }
                if(*chainpos == NULL) { //not found
                    perror("[ERROR] process not found");
                    fclose(stream);
                    continue;
                }
                //send signal
                pid_t pid = (**chainpos).pid;
                if(kill(pid, atoi(signum)) == -1) {
                    fprintf(stderr, "[ERROR] Failed to send signal: %s\n", strerror(errno));
                    fclose(stream);
                    continue;
                }
                //wait for exit
                waitpid(pid, NULL, 0);  //note: error ignored because race condition
                //remove from chain
                struct chain *c = *chainpos;
                *chainpos = (**chainpos).next;
                free(c);
            }
            break;
        }
        fprintf(stream, "ok\n");
        fclose(stream);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5f0, %rsp            # imm = 0x5F0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
je 0x1317
leaq 0xcfe(%rip), %rdi       # 0x2004
callq 0x1160
movl $0x1, -0x4(%rbp)
jmp 0x1c38
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x11d0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x136f
movq 0x2cc3(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x590(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x590(%rbp), %rdi
movq %rax, %rdx
leaq 0xcca(%rip), %rsi       # 0x2026
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
leaq -0x82(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x10b0
movw $0x1, -0x82(%rbp)
leaq -0x82(%rbp), %rdi
addq $0x2, %rdi
leaq 0xcb3(%rip), %rsi       # 0x2050
movl $0x6b, %edx
callq 0x1060
movl -0x14(%rbp), %edi
leaq -0x82(%rbp), %rsi
movl $0x6e, %edx
callq 0x1140
cmpl $-0x1, %eax
jne 0x1401
movq 0x2c31(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x598(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x598(%rbp), %rdi
movq %rax, %rdx
leaq 0xc77(%rip), %rsi       # 0x2065
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
leaq 0xc8c(%rip), %rdi       # 0x2094
callq 0x1030
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x1442
movq 0x2bd3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc6f(%rip), %rsi       # 0x209e
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x11d0
movl %eax, -0x9c(%rbp)
cmpl $-0x1, -0x9c(%rbp)
jne 0x14a0
movq 0x2b92(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5a0(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5a0(%rbp), %rdi
movq %rax, %rdx
leaq 0xc2c(%rip), %rsi       # 0x20b9
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
leaq -0x10a(%rbp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x10b0
movw $0x1, -0x10a(%rbp)
movq -0x98(%rbp), %rdi
callq 0x1090
addq $0x1, %rax
cmpq $0x6c, %rax
jbe 0x14f6
movq 0x2b1f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbfb(%rip), %rsi       # 0x20de
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
leaq -0x10a(%rbp), %rdi
addq $0x2, %rdi
movq -0x98(%rbp), %rsi
callq 0x1070
movl -0x9c(%rbp), %edi
leaq -0x10a(%rbp), %rsi
movl $0x6e, %edx
callq 0x1190
cmpl $-0x1, %eax
jne 0x156a
movq 0x2ac8(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5a8(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5a8(%rbp), %rdi
movq %rax, %rdx
leaq 0xba8(%rip), %rsi       # 0x20ff
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
movl -0x9c(%rbp), %edi
leaq 0xba7(%rip), %rsi       # 0x211e
callq 0x1130
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jne 0x15b1
movq 0x2a64(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xb83(%rip), %rsi       # 0x2121
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
movq $0x0, -0x118(%rbp)
movl -0x14(%rbp), %edi
movl $0x10, %esi
callq 0x1120
cmpl $-0x1, %eax
jne 0x160f
movq 0x2a23(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5b0(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5b0(%rbp), %rdi
movq %rax, %rdx
leaq 0xb4d(%rip), %rsi       # 0x2149
movb $0x0, %al
callq 0x10f0
movl $0x41, -0x4(%rbp)
jmp 0x1c38
leaq 0xb51(%rip), %rdi       # 0x2167
movb $0x0, %al
callq 0x10a0
leaq 0xb58(%rip), %rdi       # 0x217c
movb $0x0, %al
callq 0x10a0
movl -0x14(%rbp), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x1170
movl %eax, -0x11c(%rbp)
leaq 0xb40(%rip), %rdi       # 0x2187
movb $0x0, %al
callq 0x10a0
cmpl $-0x1, -0x11c(%rbp)
jne 0x16f9
movq 0x2996(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5b8(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5b8(%rbp), %rdi
movq %rax, %rdx
leaq 0xb08(%rip), %rsi       # 0x2191
movb $0x0, %al
callq 0x10f0
movq -0x90(%rbp), %rdi
leaq 0xb1c(%rip), %rsi       # 0x21ba
movb $0x0, %al
callq 0x10f0
cmpl $0x0, %eax
jge 0x16e1
movq 0x2947(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5c0(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5c0(%rbp), %rdi
movq %rax, %rdx
leaq 0xafb(%rip), %rsi       # 0x21d3
movb $0x0, %al
callq 0x10f0
jmp 0x16ed
movq -0x90(%rbp), %rdi
callq 0x1080
movl $0x41, -0x4(%rbp)
jmp 0x1c38
movl -0x11c(%rbp), %edi
leaq 0xa18(%rip), %rsi       # 0x211e
callq 0x1130
movq %rax, -0x128(%rbp)
cmpq $0x0, -0x128(%rbp)
jne 0x1761
movq 0x28d5(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5c8(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5c8(%rbp), %rdi
movq %rax, %rdx
leaq 0xab9(%rip), %rsi       # 0x2203
movb $0x0, %al
callq 0x10f0
movl -0x11c(%rbp), %edi
callq 0x10c0
jmp 0x161d
leaq -0x530(%rbp), %rdi
movq -0x128(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x10d0
cmpq $0x0, %rax
jne 0x17c5
movq 0x2872(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5d0(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5d0(%rbp), %rdi
movq %rax, %rdx
leaq 0xa79(%rip), %rsi       # 0x2226
movb $0x0, %al
callq 0x10f0
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
leaq -0x530(%rbp), %rdi
callq 0x1090
subq $0x1, %rax
movsbl -0x530(%rbp,%rax), %eax
cmpl $0xa, %eax
jne 0x17fc
leaq -0x530(%rbp), %rdi
callq 0x1090
subq $0x1, %rax
movb $0x0, -0x530(%rbp,%rax)
jmp 0x1819
leaq 0xa4b(%rip), %rdi       # 0x224e
callq 0x1160
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
leaq -0x530(%rbp), %rdi
callq 0x1090
cmpq $0x2, %rax
jae 0x1848
leaq 0xa37(%rip), %rdi       # 0x2269
callq 0x1160
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
movb -0x530(%rbp), %al
movb %al, -0x531(%rbp)
leaq -0x52f(%rbp), %rax
movq %rax, -0x540(%rbp)
leaq 0xa17(%rip), %rdi       # 0x2280
xorl %eax, %eax
leaq -0x530(%rbp), %rsi
callq 0x10a0
movsbl -0x531(%rbp), %eax
movl %eax, -0x5d4(%rbp)
subl $0x2a, %eax
je 0x189f
jmp 0x188b
movl -0x5d4(%rbp), %eax
subl $0x5e, %eax
je 0x1a32
jmp 0x1c12
movq -0x540(%rbp), %rax
movq %rax, -0x548(%rbp)
movq -0x540(%rbp), %rax
movq %rax, -0x550(%rbp)
callq 0x11c0
movq (%rax), %rax
movq -0x550(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x18f2
movq -0x550(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x550(%rbp)
jmp 0x18bb
movq -0x550(%rbp), %rax
cmpq -0x540(%rbp), %rax
jne 0x191f
leaq 0x984(%rip), %rdi       # 0x228d
callq 0x1160
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
leaq -0x118(%rbp), %rax
movq %rax, -0x558(%rbp)
movq -0x558(%rbp), %rax
cmpq $0x0, (%rax)
je 0x1951
movq -0x558(%rbp), %rax
movq (%rax), %rax
addq $0x10, %rax
movq %rax, -0x558(%rbp)
jmp 0x192d
movl $0x18, %edi
callq 0x1110
movq %rax, -0x560(%rbp)
cmpq $0x0, -0x560(%rbp)
jne 0x1989
leaq 0x93e(%rip), %rdi       # 0x22b1
callq 0x1160
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
movq -0x550(%rbp), %rdi
callq 0x11a0
movq %rax, %rcx
movq -0x560(%rbp), %rax
movq %rcx, (%rax)
movq -0x560(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x19d8
leaq 0x8fb(%rip), %rdi       # 0x22b1
callq 0x1160
movq -0x560(%rbp), %rdi
callq 0x1040
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
movq -0x550(%rbp), %rax
movb $0x0, (%rax)
movq -0x548(%rbp), %rdi
callq 0x1180
movl %eax, %ecx
movq -0x560(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x560(%rbp), %rax
movq (%rax), %rsi
movq -0x560(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x8a9(%rip), %rdi       # 0x22be
movb $0x0, %al
callq 0x10a0
movq -0x560(%rbp), %rcx
movq -0x558(%rbp), %rax
movq %rcx, (%rax)
jmp 0x1c12
movq -0x540(%rbp), %rax
movq %rax, -0x568(%rbp)
movq -0x540(%rbp), %rax
movq %rax, -0x570(%rbp)
callq 0x11c0
movq (%rax), %rax
movq -0x570(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x1a85
movq -0x570(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x570(%rbp)
jmp 0x1a4e
movq -0x570(%rbp), %rax
cmpq -0x540(%rbp), %rax
jne 0x1ab2
leaq 0x834(%rip), %rdi       # 0x22d0
callq 0x1160
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
leaq -0x118(%rbp), %rax
movq %rax, -0x578(%rbp)
movq -0x578(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0x5d5(%rbp)
je 0x1afc
movq -0x570(%rbp), %rdi
movq -0x578(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rsi
callq 0x10e0
cmpl $0x0, %eax
sete %al
xorb $-0x1, %al
movb %al, -0x5d5(%rbp)
movb -0x5d5(%rbp), %al
testb $0x1, %al
jne 0x1b08
jmp 0x1b1f
movq -0x578(%rbp), %rax
movq (%rax), %rax
addq $0x10, %rax
movq %rax, -0x578(%rbp)
jmp 0x1ac0
movq -0x578(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x1b49
leaq 0x7c4(%rip), %rdi       # 0x22f7
callq 0x1160
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
movq -0x578(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x57c(%rbp)
movl -0x57c(%rbp), %eax
movl %eax, -0x5dc(%rbp)
movq -0x568(%rbp), %rdi
callq 0x1180
movl -0x5dc(%rbp), %edi
movl %eax, %esi
callq 0x1100
cmpl $-0x1, %eax
jne 0x1bcc
movq 0x246b(%rip), %rax      # 0x3ff8
movq (%rax), %rax
movq %rax, -0x5e8(%rbp)
callq 0x1050
movl (%rax), %edi
callq 0x11b0
movq -0x5e8(%rbp), %rdi
movq %rax, %rdx
leaq 0x75d(%rip), %rsi       # 0x2311
movb $0x0, %al
callq 0x10f0
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
movl -0x57c(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1150
movq -0x578(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x588(%rbp)
movq -0x578(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rcx
movq -0x578(%rbp), %rax
movq %rcx, (%rax)
movq -0x588(%rbp), %rdi
callq 0x1040
movq -0x128(%rbp), %rdi
leaq 0x714(%rip), %rsi       # 0x2334
movb $0x0, %al
callq 0x10f0
movq -0x128(%rbp), %rdi
callq 0x1080
jmp 0x161d
movl -0x4(%rbp), %eax
addq $0x5f0, %rsp            # imm = 0x5F0
popq %rbp
retq