#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 */
scalar_t__ EINTR;
int WEXITSTATUS(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug(char *);
int /*<<< orphan*/ debug_err(char *);
scalar_t__ errno;
scalar_t__ launch_ex(char *, char **, int *, int *, int *, int *);
int /*<<< orphan*/ read(int, char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
int launch(char *exec_name, char *argv[], int *ret_code, char *buf_stdout,
           int buf_stdout_sz, char *buf_stderr, int buf_stderr_sz,
           int do_wait) {
  int rc = -1;
  int pid, in = -1, out = -1, err = -1, stat;
  if (0 != launch_ex(exec_name, argv, &pid, &in, &out, &err)) {
    debug_err("launch_ex()\n");
    goto cleanup;
  }
  while (do_wait) {
    if (pid == waitpid(pid, &stat, 0)) {
      break;
    } else if (errno == EINTR) {
      debug("restarting waitpid()\n");
      continue;
    } else {
      debug_err("waitpid()\n");
      goto cleanup;
    }
  }
  if (buf_stdout)
    read(out, buf_stdout, buf_stdout_sz);
  if (buf_stderr)
    read(err, buf_stderr, buf_stderr_sz);
  if (ret_code) {
    *ret_code = WEXITSTATUS(stat);
  }
  rc = 0;
cleanup:
  if (in >= 0)
    close(in);
  if (out >= 0)
    close(out);
  if (err >= 0)
    close(err);
  return rc;
}


x86 assembly (clang, -O0):
<launch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
movl   $0xffffffff,-0x40(%rbp)
movl   $0xffffffff,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x40(%rbp),%r8
lea    -0x44(%rbp),%r9
call   1030 <launch_ex@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11ee <launch+0x7e>
lea    0xe1c(%rip),%rdi
call   1090 <debug_err@plt>
jmp    12b5 <launch+0x145>
jmp    11f3 <launch+0x83>
cmpl   $0x0,0x18(%rbp)
je     125f <launch+0xef>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
cmp    %ecx,%eax
jne    1223 <launch+0xb3>
jmp    125f <launch+0xef>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <launch+0xde>
lea    0xdc9(%rip),%rdi
call   1040 <debug@plt>
jmp    11f3 <launch+0x83>
lea    0xdc3(%rip),%rdi
call   1090 <debug_err@plt>
jmp    12b5 <launch+0x145>
cmpq   $0x0,-0x20(%rbp)
je     1279 <launch+0x109>
mov    -0x40(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
cmpq   $0x0,-0x30(%rbp)
je     1293 <launch+0x123>
mov    -0x44(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x10(%rbp),%edx
call   1060 <read@plt>
cmpq   $0x0,-0x18(%rbp)
je     12ae <launch+0x13e>
mov    -0x48(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     12c7 <launch+0x157>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x40(%rbp)
jl     12d9 <launch+0x169>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x44(%rbp)
jl     12eb <launch+0x17b>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r14
mov    %r8d,%ebp
mov    %rcx,%r13
mov    %rdx,%rbx
movl   $0xffffffff,0xc(%rsp)
ff
movl   $0xffffffff,0x4(%rsp)
ff
movl   $0xffffffff,(%rsp)
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x4(%rsp),%r8
mov    %rsp,%r9
call   1030 <launch_ex@plt>
test   %rax,%rax
je     11d3 <launch+0x63>
lea    0xe3c(%rip),%rdi
call   1090 <debug_err@plt>
mov    $0xffffffff,%ebx
jmp    128f <launch+0x11f>
mov    %ebp,0x14(%rsp)
mov    %r14,0x18(%rsp)
mov    %rbx,0x20(%rsp)
cmpl   $0x0,0x68(%rsp)
je     124c <launch+0xdc>
mov    0x10(%rsp),%ebx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    %eax,%ebx
je     124c <launch+0xdc>
mov    0x2ddb(%rip),%r14
mov    0x2dcc(%rip),%r12
lea    0xdfa(%rip),%rbx
lea    0x8(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    (%r14),%rax
cmp    (%r12),%rax
jne    12c6 <launch+0x156>
mov    %rbx,%rdi
call   1040 <debug@plt>
mov    0x10(%rsp),%r15d
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    %eax,%r15d
jne    1220 <launch+0xb0>
test   %r13,%r13
je     1261 <launch+0xf1>
mov    0x4(%rsp),%edi
mov    %r13,%rsi
mov    0x14(%rsp),%edx
call   1060 <read@plt>
mov    0x18(%rsp),%rsi
test   %rsi,%rsi
mov    0x20(%rsp),%rbp
je     127c <launch+0x10c>
mov    0x60(%rsp),%edx
mov    (%rsp),%edi
call   1060 <read@plt>
xor    %ebx,%ebx
test   %rbp,%rbp
je     128f <launch+0x11f>
mov    0x8(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0x0(%rbp)
mov    0xc(%rsp),%edi
test   %edi,%edi
js     129c <launch+0x12c>
call   1050 <close@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
js     12a9 <launch+0x139>
call   1050 <close@plt>
mov    (%rsp),%edi
test   %edi,%edi
js     12b5 <launch+0x145>
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4b(%rip),%rdi
jmp    11c4 <launch+0x54>

LLVM IR (-O0):
@.str = [13 x i8] c"launch_ex()\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [22 x i8] c"restarting waitpid()\0A\00"
@.str.2 = [11 x i8] c"waitpid()\0A\00"
define i32 @launch(i8* %0, i8** %1, i32* %2, i8* %3, i32 %4, i8* %5, i32 %6, i32 %7) {
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
store i8* %0, i8** %9
store i8** %1, i8*** %10
store i32* %2, i32** %11
store i8* %3, i8** %12
store i32 %4, i32* %13
store i8* %5, i8** %14
store i32 %6, i32* %15
store i32 %7, i32* %16
store i32 -1, i32* %17
store i32 -1, i32* %19
store i32 -1, i32* %20
store i32 -1, i32* %21
%23 = load i8*, i8** %9
%24 = load i8**, i8*** %10
%25 = call i64 @launch_ex(i8* %23, i8** %24, i32* %18, i32* %19, i32* %20, i32* %21)
%26 = icmp ne i64 0, %25
br i1 %26, label %27, label %29
27: ; preds = %8
%28 = call i32 @debug_err(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %71
29: ; preds = %8
br label %30
30: ; preds = %43, %29
%31 = load i32, i32* %16
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = load i32, i32* %18
%35 = load i32, i32* %18
%36 = call i32 @waitpid(i32 %35, i32* %22, i32 0)
%37 = icmp eq i32 %34, %36
br i1 %37, label %38, label %39
38: ; preds = %33
br label %47
39: ; preds = %33
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @debug(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %30
45: ; preds = %39
%46 = call i32 @debug_err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %71
47: ; preds = %38, %30
%48 = load i8*, i8** %12
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* %20
%52 = load i8*, i8** %12
%53 = load i32, i32* %13
%54 = call i32 @read(i32 %51, i8* %52, i32 %53)
br label %55
55: ; preds = %50, %47
%56 = load i8*, i8** %14
%57 = icmp ne i8* %56, null
br i1 %57, label %58, label %63
58: ; preds = %55
%59 = load i32, i32* %21
%60 = load i8*, i8** %14
%61 = load i32, i32* %15
%62 = call i32 @read(i32 %59, i8* %60, i32 %61)
br label %63
63: ; preds = %58, %55
%64 = load i32*, i32** %11
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i32, i32* %22
%68 = call i32 @WEXITSTATUS(i32 %67)
%69 = load i32*, i32** %11
store i32 %68, i32* %69
br label %70
70: ; preds = %66, %63
store i32 0, i32* %17
br label %71
71: ; preds = %70, %45, %27
%72 = load i32, i32* %19
%73 = icmp sge i32 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i32, i32* %19
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74, %71
%78 = load i32, i32* %20
%79 = icmp sge i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i32, i32* %20
%82 = call i32 @close(i32 %81)
br label %83
83: ; preds = %80, %77
%84 = load i32, i32* %21
%85 = icmp sge i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i32, i32* %21
%88 = call i32 @close(i32 %87)
br label %89
89: ; preds = %86, %83
%90 = load i32, i32* %17
ret i32 %90
}

LLVM IR (-O2):
@.str = [13 x i8] c"launch_ex()\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [22 x i8] c"restarting waitpid()\0A\00"
@.str.2 = [11 x i8] c"waitpid()\0A\00"
define i32 @launch(i8* %0, i8** %1, i32* writeonly %2, i8* %3, i32 %4, i8* %5, i32 %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = bitcast i32* %9 to i8*
%15 = bitcast i32* %10 to i8*
store i32 -1, i32* %10
%16 = bitcast i32* %11 to i8*
store i32 -1, i32* %11
%17 = bitcast i32* %12 to i8*
store i32 -1, i32* %12
%18 = bitcast i32* %13 to i8*
%19 = call i64 @launch_ex(i8* %0, i8** %1, i32* %9, i32* %10, i32* %11, i32* %12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %8
%22 = icmp eq i32 %7, 0
br i1 %22, label %40, label %23
23: ; preds = %21
%24 = load i32, i32* %9
%25 = call i32 @waitpid(i32 %24, i32* %13, i32 0)
%26 = icmp eq i32 %24, %25
br i1 %26, label %40, label %29
27: ; preds = %8
%28 = call i32 @debug_err(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %55
29: ; preds = %23, %33
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = call i32 @debug(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* %9
%36 = call i32 @waitpid(i32 %35, i32* %13, i32 0)
%37 = icmp eq i32 %35, %36
br i1 %37, label %40, label %29
38: ; preds = %29
%39 = call i32 @debug_err(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %55
40: ; preds = %33, %23, %21
%41 = icmp eq i8* %3, null
br i1 %41, label %45, label %42
42: ; preds = %40
%43 = load i32, i32* %11
%44 = call i32 @read(i32 %43, i8* %3, i32 %4)
br label %45
45: ; preds = %42, %40
%46 = icmp eq i8* %5, null
br i1 %46, label %50, label %47
47: ; preds = %45
%48 = load i32, i32* %12
%49 = call i32 @read(i32 %48, i8* %5, i32 %6)
br label %50
50: ; preds = %47, %45
%51 = icmp eq i32* %2, null
br i1 %51, label %55, label %52
52: ; preds = %50
%53 = load i32, i32* %13
%54 = call i32 @WEXITSTATUS(i32 %53)
store i32 %54, i32* %2
br label %55
55: ; preds = %50, %52, %38, %27
%56 = phi i32 [ -1, %27 ], [ -1, %38 ], [ 0, %52 ], [ 0, %50 ]
%57 = load i32, i32* %10
%58 = icmp sgt i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @close(i32 %57)
br label %61
61: ; preds = %59, %55
%62 = load i32, i32* %11
%63 = icmp sgt i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @close(i32 %62)
br label %66
66: ; preds = %64, %61
%67 = load i32, i32* %12
%68 = icmp sgt i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = call i32 @close(i32 %67)
br label %71
71: ; preds = %69, %66
ret i32 %56
}