// File: /workspace/github_c_makefile_star2_O2/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, %rdi
callq 0x20aa
testb %al, %al
jne 0x24a5
movq %r14, %rdi
callq 0x16c1
testq %rax, %rax
je 0x23ae
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1375
movq %r14, %rdi
callq 0x2088
testb %al, %al
je 0x24c5
movl $0x4, 0x8(%r15)
movq %r15, %rdi
callq 0x158c
testb %al, %al
jne 0x24e8
movq 0x3c66(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1214(%rip), %rsi      # 0x35b0
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
pushq $0x41
popq %rdi
callq 0x11f0
movq %r14, %rdi
callq 0x10a0
movq %rsp, -0x38(%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, -0x30(%rbp)
movq 0x38(%rbx), %rbx
movq %r15, %rdi
callq 0x10a0
leaq (%rax,%rbx), %r12
incq %r12
incq %rax
movq %rax, -0x40(%rbp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1160
movq %rax, %r13
addq %rax, %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x1140
testq %r13, %r13
je 0x2505
movq -0x30(%rbp), %rbx
movq %r13, 0x30(%rbx)
movq %r12, 0x38(%rbx)
testq %r12, %r12
movq -0x38(%rbp), %r14
je 0x24a0
cmpb $0x0, 0x19(%rbx)
je 0x24a0
leaq -0x4c(%rbp), %rcx
movl $0x5, (%rcx)
movq %rbx, 0x4(%rcx)
movl 0x3cc0(%rip), %edi      # 0x6148
movl 0x14(%rbx), %edx
pushq $0x3
popq %rsi
callq 0x10e0
cmpl $-0x1, %eax
je 0x2538
movb $0x0, 0x19(%rbx)
movq %r14, %rsp
jmp 0x24e8
movq 0x3b4c(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl 0x14(%rbx), %edx
leaq 0x1042(%rip), %rsi      # 0x34fb
movq %r14, %rcx
xorl %eax, %eax
callq 0x1130
jmp 0x24e0
movq 0x3b2c(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x10a6(%rip), %rsi      # 0x357c
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x14b8
subl $0x1, 0x10(%rbx)
jae 0x24f6
movq %rbx, %rdi
callq 0x1030
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3aec(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq -0x30(%rbp), %rbx
movl 0x14(%rbx), %edx
leaq 0x1027(%rip), %rsi      # 0x3544
movq %r14, %rcx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x14b8
movq -0x38(%rbp), %r14
jmp 0x24a0
movq 0x3ab9(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl 0x14(%rbx), %edx
leaq 0xade(%rip), %rsi       # 0x302a
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x14b8
jmp 0x24a0