// File: /workspace/github_c_makefile_star2_O3/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 0x2911
cmpl $0x20, %ecx
je 0x2a85
incq %rax
jmp 0x28fc
movq %r14, %rdi
callq 0x1e3e
testq %rax, %rax
je 0x2970
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1b24
leaq 0x1abd(%rip), %rax      # 0x43f0
leaq -0x50(%rbp), %rsi
movq %rax, (%rsi)
movq %r14, 0x8(%rsi)
movl $0x2, %edi
callq 0x258b
testb %al, %al
je 0x2a92
movl $0x4, 0x8(%r15)
movq %r15, %rdi
callq 0x1cf0
testb %al, %al
jne 0x2a68
movq %r14, %rdi
callq 0x1721
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 %rax, %r13
movq %r12, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1140
movl $0x6c756e20, 0x7(%r13,%r15) # imm = 0x6C756E20
movw $0x6c, 0xb(%r13,%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 0x1160
movq %rax, %r13
addq %rax, %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x1140
testq %r13, %r13
je 0x2ab7
movq %r13, 0x30(%r12)
movq -0x38(%rbp), %rax
movq %rax, 0x38(%r12)
testq %rax, %rax
movq %r12, %rbx
movq -0x30(%rbp), %r14
je 0x2a65
cmpb $0x0, 0x19(%rbx)
je 0x2a65
leaq -0x50(%rbp), %rcx
movl $0x5, (%rcx)
movq %rbx, 0x4(%rcx)
movl 0x46f9(%rip), %edi      # 0x7148
movl 0x14(%rbx), %edx
movl $0x3, %esi
callq 0x10e0
cmpl $-0x1, %eax
je 0x2acb
movb $0x0, 0x19(%rbx)
movq %r14, %rsp
subl $0x1, 0x10(%rbx)
jae 0x2a76
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 0x16f6
jmp 0x2a68
movq 0x455f(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x1ad9(%rip), %rsi      # 0x457c
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x1c5b
jmp 0x2a68
movq %r12, %rdi
movq %r14, %rsi
callq 0x1770
movq %r12, %rbx
movq -0x30(%rbp), %r14
jmp 0x2a65
leaq 0x14(%rbx), %rdi
movq %rbx, %rsi
callq 0x1745
jmp 0x2a65