#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ WIFSTOPPED(int);
int _redirect_fd(int, int);
int errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ util_waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int util_runcmd(char **argv, int *wstat, int ifd, int ofd, int efd) {
  pid_t pid;
  int res = 0;
  int s;
  switch ((pid = fork())) {
  case -1:
    res = 23;
    break;
  case 0:
    if ((res = _redirect_fd(ifd, 0)) != 0)
      exit(1);
    if ((res = _redirect_fd(ofd, 1)) != 0)
      exit(1);
    if ((res = _redirect_fd(efd, 2)) != 0)
      exit(1);
    if (execvp(argv[0], argv) < 0) {
      exit(1);
    }
    break;
  default:
    if (util_waitpid(pid, &s, 0) < 0) {
      res = 9;
      if (wstat)
        *wstat = errno;
    } else {
      if (WIFEXITED(s) && WEXITSTATUS(s) != 0)
        res = 6;
      else if (WIFSIGNALED(s))
        res = 7;
      else if (WIFSTOPPED(s))
        res = 8;
      if (wstat)
        *wstat = s;
    }
    break;
  }
  return res;
}


x86 assembly (clang, -O0):
<util_runcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,-0x20(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11de <util_runcmd+0x4e>
jmp    11ce <util_runcmd+0x3e>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
je     11ea <util_runcmd+0x5a>
jmp    1278 <util_runcmd+0xe8>
movl   $0x17,-0x24(%rbp)
jmp    1340 <util_runcmd+0x1b0>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <_redirect_fd@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     120a <util_runcmd+0x7a>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <_redirect_fd@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     122d <util_runcmd+0x9d>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1040 <_redirect_fd@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     1250 <util_runcmd+0xc0>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
cmp    $0x0,%eax
jge    1273 <util_runcmd+0xe3>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1340 <util_runcmd+0x1b0>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1030 <util_waitpid@plt>
cmp    $0x0,%eax
jge    12b5 <util_runcmd+0x125>
movl   $0x9,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12b0 <util_runcmd+0x120>
mov    0x2d38(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    133b <util_runcmd+0x1ab>
mov    -0x28(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12e3 <util_runcmd+0x153>
mov    -0x28(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     12e3 <util_runcmd+0x153>
movl   $0x6,-0x24(%rbp)
jmp    1322 <util_runcmd+0x192>
mov    -0x28(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%eax
je     1300 <util_runcmd+0x170>
movl   $0x7,-0x24(%rbp)
jmp    131d <util_runcmd+0x18d>
mov    -0x28(%rbp),%edi
call   1060 <WIFSTOPPED@plt>
cmp    $0x0,%eax
je     1318 <util_runcmd+0x188>
movl   $0x8,-0x24(%rbp)
jmp    131d <util_runcmd+0x18d>
jmp    1322 <util_runcmd+0x192>
cmpq   $0x0,-0x10(%rbp)
je     1336 <util_runcmd+0x1a6>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    133b <util_runcmd+0x1ab>
jmp    1340 <util_runcmd+0x1b0>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_runcmd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%r14d
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r12
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1209 <util_runcmd+0x79>
test   %eax,%eax
jne    1210 <util_runcmd+0x80>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <_redirect_fd@plt>
test   %eax,%eax
jne    11ff <util_runcmd+0x6f>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <_redirect_fd@plt>
test   %eax,%eax
jne    11ff <util_runcmd+0x6f>
mov    %r14d,%edi
mov    $0x2,%esi
call   1040 <_redirect_fd@plt>
test   %eax,%eax
jne    11ff <util_runcmd+0x6f>
mov    (%r12),%rdi
mov    %r12,%rsi
call   1080 <execvp@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    128d <util_runcmd+0xfd>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x17,%eax
jmp    128d <util_runcmd+0xfd>
lea    0xc(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <util_waitpid@plt>
test   %eax,%eax
js     1276 <util_runcmd+0xe6>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     1246 <util_runcmd+0xb6>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    $0x6,%eax
test   %ecx,%ecx
jne    126f <util_runcmd+0xdf>
mov    0xc(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
mov    %eax,%ecx
mov    $0x7,%eax
test   %ecx,%ecx
jne    126f <util_runcmd+0xdf>
mov    0xc(%rsp),%edi
call   1060 <WIFSTOPPED@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
shl    $0x3,%eax
test   %r15,%r15
jne    1287 <util_runcmd+0xf7>
jmp    128d <util_runcmd+0xfd>
mov    $0x9,%eax
test   %r15,%r15
je     128d <util_runcmd+0xfd>
mov    0x2d59(%rip),%r14
mov    (%r14),%ecx
mov    %ecx,(%r15)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @util_runcmd(i8** %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %12
%14 = call i32 (...) @fork()
store i32 %14, i32* %11
switch i32 %14, label %44 [
i32 -1, label %15
i32 0, label %16
]
15: ; preds = %5
store i32 23, i32* %12
br label %84
16: ; preds = %5
%17 = load i32, i32* %8
%18 = call i32 @_redirect_fd(i32 %17, i32 0)
store i32 %18, i32* %12
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = load i32, i32* %9
%24 = call i32 @_redirect_fd(i32 %23, i32 1)
store i32 %24, i32* %12
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = load i32, i32* %10
%30 = call i32 @_redirect_fd(i32 %29, i32 2)
store i32 %30, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28
%35 = load i8**, i8*** %6
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = load i8**, i8*** %6
%39 = call i32 @execvp(i8* %37, i8** %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %34
br label %84
44: ; preds = %5
%45 = load i32, i32* %11
%46 = call i32 @util_waitpid(i32 %45, i32* %13, i32 0)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %44
store i32 9, i32* %12
%49 = load i32*, i32** %7
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = load i32*, i32** %7
store i32 %52, i32* %53
br label %54
54: ; preds = %51, %48
br label %83
55: ; preds = %44
%56 = load i32, i32* %13
%57 = call i32 @WIFEXITED(i32 %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = load i32, i32* %13
%61 = call i32 @WEXITSTATUS(i32 %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %59
store i32 6, i32* %12
br label %76
64: ; preds = %59, %55
%65 = load i32, i32* %13
%66 = call i32 @WIFSIGNALED(i32 %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %64
store i32 7, i32* %12
br label %75
69: ; preds = %64
%70 = load i32, i32* %13
%71 = call i32 @WIFSTOPPED(i32 %70)
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69
store i32 8, i32* %12
br label %74
74: ; preds = %73, %69
br label %75
75: ; preds = %74, %68
br label %76
76: ; preds = %75, %63
%77 = load i32*, i32** %7
%78 = icmp ne i32* %77, null
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i32, i32* %13
%81 = load i32*, i32** %7
store i32 %80, i32* %81
br label %82
82: ; preds = %79, %76
br label %83
83: ; preds = %82, %54
br label %84
84: ; preds = %83, %43, %15
%85 = load i32, i32* %12
ret i32 %85
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @util_runcmd(i8** %0, i32* writeonly %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = tail call i32 (...) @fork()
switch i32 %8, label %30 [
i32 -1, label %59
i32 0, label %9
]
9: ; preds = %5
%10 = tail call i32 @_redirect_fd(i32 %2, i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %9
%15 = tail call i32 @_redirect_fd(i32 %3, i32 1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %14
%20 = tail call i32 @_redirect_fd(i32 %4, i32 2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %19
%25 = load i8*, i8** %0
%26 = tail call i32 @execvp(i8* %25, i8** %0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %59
28: ; preds = %24
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %5
%31 = call i32 @util_waitpid(i32 %8, i32* %6, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = icmp eq i32* %1, null
br i1 %34, label %59, label %55
35: ; preds = %30
%36 = load i32, i32* %6
%37 = call i32 @WIFEXITED(i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i32, i32* %6
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %39, %35
%44 = load i32, i32* %6
%45 = call i32 @WIFSIGNALED(i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i32, i32* %6
%49 = call i32 @WIFSTOPPED(i32 %48)
%50 = icmp eq i32 %49, 0
%51 = select i1 %50, i32 0, i32 8
br label %52
52: ; preds = %47, %43, %39
%53 = phi i32 [ 6, %39 ], [ 7, %43 ], [ %51, %47 ]
%54 = icmp eq i32* %1, null
br i1 %54, label %59, label %55
55: ; preds = %52, %33
%56 = phi i32* [ @errno, %33 ], [ %6, %52 ]
%57 = phi i32 [ 9, %33 ], [ %53, %52 ]
%58 = load i32, i32* %56
store i32 %58, i32* %1
br label %59
59: ; preds = %55, %5, %33, %52, %24
%60 = phi i32 [ 9, %33 ], [ %53, %52 ], [ 0, %24 ], [ 23, %5 ], [ %57, %55 ]
ret i32 %60
}