// File: /workspace/github_c_makefile_star2_O1/panux[P]linit/src/linitd.c
// Function: cmd_stop
void cmd_stop(struct conn *c, char *arg) {
    ref(c);
    if(hasSpace(arg)) {
        //bad command syntax
        fprintf(stderr, "[ERROR] Bad stop command syntax recieved on conn %d: \"%s\"\n", c->fd, arg);
        close_conn(c);
        goto end;
    }
    struct service *svc = findService(arg);
    if(svc == NULL) {   //already stopped
        //format: "notify %s null"
        char not[] = "notify ";
        char nul[] = " null";
        char ntxt[strlen(not) + strlen(arg) + strlen(nul) + 1];
        memcpy(ntxt, not, strlen(not));
        memcpy(ntxt + strlen(not), arg, strlen(arg));
        memcpy(ntxt + strlen(not) + strlen(arg), nul, strlen(nul) + 1);
        //send it
        struct buf otx = c->tx;
        struct buf ntx = {
            .dat = realloc(otx.dat, otx.len + strlen(ntxt) + 1),
            .len = otx.len + strlen(ntxt) + 1,
        };
        memcpy(ntx.dat + otx.len, ntxt, strlen(ntxt) + 1);
        if(ntx.dat == NULL) {
            fprintf(stderr, "[ERROR] failed to notify %d that %s is already stopped\n", c->fd, arg);
            close_conn(c);
            goto end;
        }
        c->tx = ntx;
        if(c->txoff && (c->tx.len > 0)) {  //tx buffer was just populated
            //enable write in epoll
            struct epoll_event ev = {.events = EPOLLIN | EPOLLOUT, .data = {.ptr = &c->e}};
            if(epoll_ctl(epollfd, EPOLL_CTL_MOD, c->fd, &ev) == -1) {   //failed to enable - close and forget
                fprintf(stderr, "[ERROR] Failed to enable writing on conn %d\n", c->fd);
                close_conn(c);
            } else {
                c->txoff = false;
            }
        }
        goto end;
    }
    setNotify(svc, c);
    //stop service
    if(!runStop(arg)) {
        fprintf(stderr, "[ERROR] Failed to stop service %s: fork/exec error\n", arg);
        close_conn(c);
        goto end;
    }
    svc->state = service_status_stopping;
    if(!notifyAll(svc)) {
        fprintf(stderr, "[FATAL] Failed to notify clients of service %s stopping\n", arg);
        exit(65);
    }
end:
    deref(c);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
incl 0x10(%rdi)
movq %rsi, %rax
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x2960
cmpl $0x20, %ecx
je 0x2ad9
incq %rax
jmp 0x294b
movq %r14, %rdi
callq 0x1e8c
testq %rax, %rax
je 0x29bf
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1b45
leaq 0x1a8a(%rip), %rax      # 0x440c
leaq -0x50(%rbp), %rsi
movq %rax, (%rsi)
movq %r14, 0x8(%rsi)
movl $0x2, %edi
callq 0x25da
testb %al, %al
je 0x2ae6
movl $0x4, 0x8(%r15)
movq %r15, %rdi
callq 0x1d28
testb %al, %al
jne 0x2abc
movq %r14, %rdi
callq 0x1731
movq %r14, %rdi
callq 0x10a0
movq %rsp, -0x30(%rbp)
movq %rsp, %rcx
addq $0x1c, %rax
andq $-0x10, %rax
movq %rcx, %r15
subq %rax, %r15
movq %r15, %rsp
negq %rax
movl $0x69746f6e, (%rcx,%rax) # imm = 0x69746F6E
movl $0x20796669, 0x3(%r15)  # imm = 0x20796669
leaq 0x7(%r15), %r12
movq %r14, %rdi
callq 0x10a0
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1140
movq %r14, %rdi
callq 0x10a0
movl $0x6c756e20, 0x7(%rax,%r15) # imm = 0x6C756E20
movw $0x6c, 0xb(%rax,%r15)
movq 0x30(%rbx), %r13
movq %rbx, %r12
movq 0x38(%rbx), %rbx
movq %r15, %rdi
callq 0x10a0
leaq (%rax,%rbx), %rsi
incq %rsi
incq %rax
movq %rax, -0x40(%rbp)
movq %r13, %rdi
movq %rsi, -0x38(%rbp)
callq 0x1170
movq %rax, %r13
addq %rax, %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x1140
testq %r13, %r13
je 0x2b0b
movq %r13, 0x30(%r12)
movq -0x38(%rbp), %rax
movq %rax, 0x38(%r12)
testq %rax, %rax
movq %r12, %rbx
movq -0x30(%rbp), %r14
je 0x2ab9
cmpb $0x0, 0x19(%rbx)
je 0x2ab9
leaq -0x50(%rbp), %rcx
movl $0x5, (%rcx)
movq %rbx, 0x4(%rcx)
movl 0x46ad(%rip), %edi      # 0x7150
movl 0x14(%rbx), %edx
movl $0x3, %esi
callq 0x10f0
cmpl $-0x1, %eax
je 0x2b1f
movb $0x0, 0x19(%rbx)
movq %r14, %rsp
subl $0x1, 0x10(%rbx)
jae 0x2aca
movq %rbx, %rdi
callq 0x1030
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1706
jmp 0x2abc
movq 0x450b(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1aa1(%rip), %rsi      # 0x4598
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x1c93
jmp 0x2abc
movq %r12, %rdi
movq %r14, %rsi
callq 0x1780
movq %r12, %rbx
movq -0x30(%rbp), %r14
jmp 0x2ab9
leaq 0x14(%rbx), %rdi
movq %rbx, %rsi
callq 0x1755
jmp 0x2ab9