// File: /workspace/github_c_makefile_star2_O2/panux[P]linit/src/linitd.c
// Function: cmd_start
void cmd_start(struct conn *c, char *args) {
    if(c->closed) {
        return;
    }
    ref(c);
    if(hasSpace(args)) {    //starting multiple - break into multiple commands
        char *arg, *a;
        arg = a = args;
        for(;;) {
            switch(*a) {
            case '\0':
                cmd_start(c, arg);
                goto end;
            case ' ':
                *a = '\0';
                cmd_start(c, arg);
                a = arg = a + 1;
                break;
            default:
                a++;
            }
        }
    } else {
        struct service *svc = findService(args);
        if(svc != NULL) {   //service already started
            if(!setNotify(svc, c)) {
                fprintf(stderr, "Failed to set conn %d to be notified about %s\n", c->fd, args);
                close_conn(c);
                goto end;
            }
            //format: "notify %s %s", args, status
            char not[] = "notify ";
            char *status = statestr(svc->state);
            char ntxt[strlen(not) + strlen(args) + strlen(status) + 2];
            char *nt = ntxt;
            strcpy(nt, not);
            nt += strlen(not);
            strcpy(nt, args);
            nt += strlen(args);
            *nt = ' ';
            nt++;
            strcpy(nt, status);
            struct buf dat = { .dat = ntxt, .len = strlen(ntxt) + 1 };
            if(notify(c, svc, dat) != notify_success) {
                fprintf(stderr, "Failed to notify conn %d about the state of %s\n", c->fd, args);
                close_conn(c);
                goto end;
            }
        } else {            //start service and mark to be notified
            struct service *ns = newService();
            if(ns == NULL) {
                fprintf(stderr, "[ERROR] Failed to start service %s: out of memory\n", args);
                close_conn(c);  //we cant notify the service because we are out of memory
                goto end;
            }
            ns->name = strdup(args);
            ns->state = service_status_starting;
            setNotify(ns, c);
            if(!runStart(args)) {
                fprintf(stderr, "[ERROR] Failed to start service %s: fork/exec error\n", args);
                ns->state = service_status_fail;
            }
            if(!notifyAll(ns)) {    //notify client of start
                fprintf(stderr, "[ERROR] Failed to send notification about %s to conn %d\n", args, c->fd);
                close_conn(c);
                goto end;
            }
            if(ns->state == service_status_fail) {  //delete the service if it failed
                rmService(ns->name);
            }
        }
    }
end:
    deref(c);
}

// Assembly:
cmpb $0x0, 0x18(%rdi)
jne 0x21ff
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
incl 0x10(%rdi)
movq %rsi, %rdi
callq 0x20aa
testb %al, %al
je 0x2119
leaq 0x1(%r14), %r15
movzbl -0x1(%r15), %eax
cmpl $0x20, %eax
je 0x2104
testl %eax, %eax
je 0x21d8
incq %r15
jmp 0x20ed
movb $0x0, -0x1(%r15)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20bc
movq %r15, %r14
jmp 0x20e9
movq %r14, %rdi
callq 0x16c1
testq %rax, %rax
je 0x2200
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1375
testb %al, %al
je 0x2260
movl 0x8(%r15), %edi
callq 0x1357
movq %rax, %r15
movq %r14, %rdi
callq 0x10a0
movq %rax, %r12
movq %r15, %rdi
callq 0x10a0
movq %rsp, %r13
addq %r12, %rax
addq $0x18, %rax
andq $-0x10, %rax
movq %rsp, %rcx
movq %rcx, %r12
subq %rax, %r12
negq %rax
movq %r12, %rsp
movabsq $0x20796669746f6e, %rdx # imm = 0x20796669746F6E
movq %rdx, (%rcx,%rax)
leaq 0x7(%r12), %rdi
movq %r14, %rsi
callq 0x1060
movq %r14, %rdi
callq 0x10a0
leaq (%rax,%r12), %rdi
addq $0x8, %rdi
movb $0x20, -0x1(%rdi)
movq %r15, %rsi
callq 0x1060
movq %r12, %rdi
callq 0x10a0
leaq 0x1(%rax), %rcx
movq %rbx, %rdi
movq %r12, %rdx
callq 0x13dc
cmpl $0x1, %eax
jne 0x2283
movq %r13, %rsp
jmp 0x21e3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20bc
subl $0x1, 0x10(%rbx)
jae 0x21f1
movq %rbx, %rdi
callq 0x1030
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x16f7
testq %rax, %rax
je 0x22ae
movq %rax, %r15
movq %r14, %rdi
callq 0x1210
movq %rax, (%r15)
movl $0x1, 0x8(%r15)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1375
movq %r14, %rdi
callq 0x2066
testb %al, %al
je 0x22cb
movq %r15, %rdi
callq 0x158c
testb %al, %al
je 0x22f3
cmpl $0x3, 0x8(%r15)
jne 0x21e3
movq (%r15), %rdi
callq 0x1734
jmp 0x21e3
movq 0x3d91(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl 0x14(%rbx), %edx
leaq 0x1187(%rip), %rsi      # 0x33fb
movq %r14, %rcx
xorl %eax, %eax
callq 0x1130
jmp 0x2311
movq 0x3d6e(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl 0x14(%rbx), %edx
leaq 0x1193(%rip), %rsi      # 0x342a
movq %r14, %rcx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x14b8
jmp 0x21d3
movq 0x3d43(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x119b(%rip), %rsi      # 0x345a
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
jmp 0x2311
movq 0x3d26(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x11b1(%rip), %rsi      # 0x348d
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
movl $0x3, 0x8(%r15)
jmp 0x223f
movq 0x3cfe(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movl 0x14(%rbx), %ecx
leaq 0x11bb(%rip), %rsi      # 0x34c2
movq %r14, %rdx
xorl %eax, %eax
callq 0x1130
movq %rbx, %rdi
callq 0x14b8
jmp 0x21e3