#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ child(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ daemonize();
int /*<<< orphan*/ dup2(int, int);
int fork();
int parent(int, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
scalar_t__ prepare_new_job(char const *, char const *);
int /*<<< orphan*/ put_file(char *, char const *);
int cmd_execute(const char *queue, const char *jobid, const char *cmd,
                int wait) {
  int fd_pipe_stdout[2];
  int fd_pipe_stderr[2];
  int rc = 1;
  if (prepare_new_job(queue, jobid) == 0) {
    put_file("command", cmd);
    if (pipe(fd_pipe_stdout) == 0 && pipe(fd_pipe_stderr) == 0) {
      if (!wait)
        daemonize();
      int pid = fork();
      switch (pid) {
      case -1:
        perror("fork()");
        return 1;
        break;
      case 0:
        close(0);
        close(fd_pipe_stdout[0]);
        close(fd_pipe_stderr[0]);
        dup2(fd_pipe_stdout[1], 1);
        dup2(fd_pipe_stderr[1], 2);
        child(cmd);
        break;
      default: {
        close(fd_pipe_stdout[1]);
        close(fd_pipe_stderr[1]);
        rc = parent(pid, fd_pipe_stdout[0], fd_pipe_stderr[0]);
      }
      }
    } else {
      perror("pipe");
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<cmd_execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <prepare_new_job@plt>
cmp    $0x0,%rax
jne    12d8 <cmd_execute+0x138>
mov    -0x20(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1090 <put_file@plt>
lea    -0x2c(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jne    12c7 <cmd_execute+0x127>
lea    -0x34(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jne    12c7 <cmd_execute+0x127>
cmpl   $0x0,-0x24(%rbp)
jne    121c <cmd_execute+0x7c>
mov    $0x0,%al
call   1050 <daemonize@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0xffffffff,%eax
je     124a <cmd_execute+0xaa>
jmp    123a <cmd_execute+0x9a>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     1262 <cmd_execute+0xc2>
jmp    12a1 <cmd_execute+0x101>
lea    0xdb7(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <cmd_execute+0x13e>
xor    %edi,%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0x30(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    -0x20(%rbp),%rdi
call   1030 <child@plt>
jmp    12c2 <cmd_execute+0x122>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
mov    -0x3c(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1040 <parent@plt>
mov    %eax,-0x38(%rbp)
jmp    12d3 <cmd_execute+0x133>
lea    0xd41(%rip),%rdi
call   10b0 <perror@plt>
jmp    12d8 <cmd_execute+0x138>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_execute>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
call   10a0 <prepare_new_job@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    128c <cmd_execute+0xec>
lea    0xe39(%rip),%rdi
mov    %r14,%rsi
call   1090 <put_file@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
jne    1277 <cmd_execute+0xd7>
mov    %rsp,%rdi
call   1080 <pipe@plt>
test   %rax,%rax
jne    1277 <cmd_execute+0xd7>
test   %ebp,%ebp
jne    11fe <cmd_execute+0x5e>
xor    %eax,%eax
call   1050 <daemonize@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1234 <cmd_execute+0x94>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1280 <cmd_execute+0xe0>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%esi
mov    (%rsp),%edx
mov    %ebp,%edi
call   1040 <parent@plt>
mov    %eax,%ebx
jmp    128c <cmd_execute+0xec>
xor    %edi,%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%ebx
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    0x4(%rsp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    %r14,%rdi
call   1030 <child@plt>
jmp    128c <cmd_execute+0xec>
lea    0xd91(%rip),%rdi
jmp    1287 <cmd_execute+0xe7>
lea    0xd81(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"command\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [5 x i8] c"pipe\00"
define i32 @cmd_execute(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [2 x i32]
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 1, i32* %12
%14 = load i8*, i8** %6
%15 = load i8*, i8** %7
%16 = call i64 @prepare_new_job(i8* %14, i8* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %71
18: ; preds = %4
%19 = load i8*, i8** %8
%20 = call i32 @put_file(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%22 = call i64 @pipe(i32* %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %68
24: ; preds = %18
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%26 = call i64 @pipe(i32* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %68
28: ; preds = %24
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 (...) @daemonize()
br label %33
33: ; preds = %31, %28
%34 = call i32 (...) @fork()
store i32 %34, i32* %13
%35 = load i32, i32* %13
switch i32 %35, label %54 [
i32 -1, label %36
i32 0, label %38
]
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %73
38: ; preds = %33
%39 = call i32 @close(i32 0)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @dup2(i32 %47, i32 1)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%50 = load i32, i32* %49
%51 = call i32 @dup2(i32 %50, i32 2)
%52 = load i8*, i8** %8
%53 = call i32 @child(i8* %52)
br label %67
54: ; preds = %33
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %13
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%63 = load i32, i32* %62
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @parent(i32 %61, i32 %63, i32 %65)
store i32 %66, i32* %12
br label %67
67: ; preds = %54, %38
br label %70
68: ; preds = %24, %18
%69 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %70
70: ; preds = %68, %67
br label %71
71: ; preds = %70, %4
%72 = load i32, i32* %12
store i32 %72, i32* %5
br label %73
73: ; preds = %71, %36
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
@.str = [8 x i8] c"command\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [5 x i8] c"pipe\00"
define i32 @cmd_execute(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = bitcast [2 x i32]* %5 to i8*
%8 = bitcast [2 x i32]* %6 to i8*
%9 = tail call i64 @prepare_new_job(i8* %0, i8* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %53
11: ; preds = %4
%12 = tail call i32 @put_file(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %2)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%14 = call i64 @pipe(i32* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %51
16: ; preds = %11
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%18 = call i64 @pipe(i32* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %51
20: ; preds = %16
%21 = icmp eq i32 %3, 0
br i1 %21, label %22, label %24
22: ; preds = %20
%23 = call i32 (...) @daemonize()
br label %24
24: ; preds = %22, %20
%25 = call i32 (...) @fork()
switch i32 %25, label %39 [
i32 -1, label %49
i32 0, label %26
]
26: ; preds = %24
%27 = call i32 @close(i32 0)
%28 = load i32, i32* %13
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %17
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @dup2(i32 %33, i32 1)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @dup2(i32 %36, i32 2)
%38 = call i32 @child(i8* %2)
br label %53
39: ; preds = %24
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %13
%47 = load i32, i32* %17
%48 = call i32 @parent(i32 %25, i32 %46, i32 %47)
br label %53
49: ; preds = %24
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %53
51: ; preds = %16, %11
%52 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %26, %39, %4, %51, %49
%54 = phi i32 [ 1, %49 ], [ 1, %51 ], [ 1, %4 ], [ 1, %26 ], [ %48, %39 ]
ret i32 %54
}