#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 WEXITSTATUS(int);
int /*<<< orphan*/ debug(char *, ...);
int /*<<< orphan*/ debug_err(char *);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char const *, char const *, int /*<<< orphan*/ *);
int fork();
int waitpid(int, int *, int /*<<< orphan*/);
int launch_impose_descriptors(const char *filepath, int stdin_, int stdout_,
                              int stderr_, int do_wait) {
  int rc = -1;
  int pid;
  int status = -1;
  pid = fork();
  switch (pid) {
  case -1:
    debug_err("fork()\n");
    break;
  case 0:
    if (stdin_ >= 0)
      dup2(stdin_, 0);
    if (stdout_ >= 0)
      dup2(stdout_, 1);
    if (stderr_ >= 0)
      dup2(stderr_, 2);
    execl(filepath, filepath, NULL);
    debug_err("execl() returned");
    break;
  default:
    if (!do_wait)
      break;
    debug("calling waitpid()\n");
    if (waitpid(pid, &status, 0) == -1) {
      debug_err("waitpid()\n");
    } else {
      rc = WEXITSTATUS(status);
      debug("status:%d (child returned: %d)\n", status, rc);
    }
    break;
  }
  return rc;
}


x86 assembly (clang, -O0):
<launch_impose_descriptors>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0xffffffff,-0x24(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0xffffffff,%eax
je     11c5 <launch_impose_descriptors+0x55>
jmp    11b5 <launch_impose_descriptors+0x45>
mov    -0x28(%rbp),%eax
test   %eax,%eax
je     11d6 <launch_impose_descriptors+0x66>
jmp    123a <launch_impose_descriptors+0xca>
lea    0xe34(%rip),%rdi
call   1080 <debug_err@plt>
jmp    12a3 <launch_impose_descriptors+0x133>
cmpl   $0x0,-0xc(%rbp)
jl     11ea <launch_impose_descriptors+0x7a>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmpl   $0x0,-0x10(%rbp)
jl     1201 <launch_impose_descriptors+0x91>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmpl   $0x0,-0x14(%rbp)
jl     1218 <launch_impose_descriptors+0xa8>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execl@plt>
lea    0xdd8(%rip),%rdi
call   1080 <debug_err@plt>
jmp    12a3 <launch_impose_descriptors+0x133>
cmpl   $0x0,-0x18(%rbp)
jne    1249 <launch_impose_descriptors+0xd9>
jmp    12a3 <launch_impose_descriptors+0x133>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1030 <debug@plt>
mov    -0x20(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    127f <launch_impose_descriptors+0x10f>
lea    0xdac(%rip),%rdi
call   1080 <debug_err@plt>
jmp    129e <launch_impose_descriptors+0x12e>
mov    -0x24(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <debug@plt>
jmp    12a3 <launch_impose_descriptors+0x133>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launch_impose_descriptors>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %esi,%ebx
mov    %rdi,%r14
movl   $0xffffffff,0x4(%rsp)
ff
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11ac <launch_impose_descriptors+0x3c>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    120e <launch_impose_descriptors+0x9e>
lea    0xe56(%rip),%rdi
jmp    11f1 <launch_impose_descriptors+0x81>
test   %ebx,%ebx
js     11b9 <launch_impose_descriptors+0x49>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
test   %r12d,%r12d
js     11cb <launch_impose_descriptors+0x5b>
mov    %r12d,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
test   %r15d,%r15d
js     11dd <launch_impose_descriptors+0x6d>
mov    %r15d,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %r14,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <execl@plt>
lea    0xe17(%rip),%rdi
call   1080 <debug_err@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r14d
test   %r13d,%r13d
je     11fc <launch_impose_descriptors+0x8c>
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1030 <debug@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     125c <launch_impose_descriptors+0xec>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%r14d
mov    0x4(%rsp),%esi
lea    0xddb(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <debug@plt>
jmp    11fc <launch_impose_descriptors+0x8c>
lea    0xdbe(%rip),%rdi
call   1080 <debug_err@plt>
jmp    11fc <launch_impose_descriptors+0x8c>

LLVM IR (-O0):
@.str = [8 x i8] c"fork()\0A\00"
@.str.1 = [17 x i8] c"execl() returned\00"
@.str.2 = [19 x i8] c"calling waitpid()\0A\00"
@.str.3 = [11 x i8] c"waitpid()\0A\00"
@.str.4 = [32 x i8] c"status:%d (child returned: %d)\0A\00"
define i32 @launch_impose_descriptors(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 -1, i32* %11
store i32 -1, i32* %13
%14 = call i32 (...) @fork()
store i32 %14, i32* %12
%15 = load i32, i32* %12
switch i32 %15, label %41 [
i32 -1, label %16
i32 0, label %18
]
16: ; preds = %5
%17 = call i32 @debug_err(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %59
18: ; preds = %5
%19 = load i32, i32* %7
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32, i32* %7
%23 = call i32 @dup2(i32 %22, i32 0)
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* %8
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %8
%29 = call i32 @dup2(i32 %28, i32 1)
br label %30
30: ; preds = %27, %24
%31 = load i32, i32* %9
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %9
%35 = call i32 @dup2(i32 %34, i32 2)
br label %36
36: ; preds = %33, %30
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = call i32 @execl(i8* %37, i8* %38, i32* null)
%40 = call i32 @debug_err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %59
41: ; preds = %5
%42 = load i32, i32* %10
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %41
br label %59
45: ; preds = %41
%46 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %12
%48 = call i32 @waitpid(i32 %47, i32* %13, i32 0)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @debug_err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %58
52: ; preds = %45
%53 = load i32, i32* %13
%54 = call i32 @WEXITSTATUS(i32 %53)
store i32 %54, i32* %11
%55 = load i32, i32* %13
%56 = load i32, i32* %11
%57 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %55, i32 %56)
br label %58
58: ; preds = %52, %50
br label %59
59: ; preds = %58, %44, %36, %16
%60 = load i32, i32* %11
ret i32 %60
}

LLVM IR (-O2):
@.str = [8 x i8] c"fork()\0A\00"
@.str.1 = [17 x i8] c"execl() returned\00"
@.str.2 = [19 x i8] c"calling waitpid()\0A\00"
@.str.3 = [11 x i8] c"waitpid()\0A\00"
@.str.4 = [32 x i8] c"status:%d (child returned: %d)\0A\00"
define i32 @launch_impose_descriptors(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
store i32 -1, i32* %6
%8 = tail call i32 (...) @fork()
switch i32 %8, label %26 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %5
%10 = tail call i32 @debug_err(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %5
%12 = icmp sgt i32 %1, -1
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @dup2(i32 %1, i32 0)
br label %15
15: ; preds = %13, %11
%16 = icmp sgt i32 %2, -1
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @dup2(i32 %2, i32 1)
br label %19
19: ; preds = %17, %15
%20 = icmp sgt i32 %3, -1
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 @dup2(i32 %3, i32 2)
br label %23
23: ; preds = %21, %19
%24 = tail call i32 @execl(i8* %0, i8* %0, i32* null)
%25 = tail call i32 @debug_err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %39
26: ; preds = %5
%27 = icmp eq i32 %4, 0
br i1 %27, label %39, label %28
28: ; preds = %26
%29 = tail call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @waitpid(i32 %8, i32* %6, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @debug_err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %39
34: ; preds = %28
%35 = load i32, i32* %6
%36 = call i32 @WEXITSTATUS(i32 %35)
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %37, i32 %36)
br label %39
39: ; preds = %32, %34, %26, %23, %9
%40 = phi i32 [ -1, %32 ], [ %36, %34 ], [ -1, %26 ], [ -1, %23 ], [ -1, %9 ]
ret i32 %40
}