#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
scalar_t__ chdir(char const *);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ closeOtherFds(int const, int const, int const, int const *,
                                 int);
int /*<<< orphan*/ dup2(int const, int);
int /*<<< orphan*/ execve(char const *, char *const *, char *const *);
scalar_t__ fork();
pid_t launchProcess(const char *path, char *const argv[], char *const envp[],
                    const char *workdir, const int fd_in[2],
                    const int fd_out[2], const int fd_err[2],
                    const int dupFds[]) {
  pid_t pid;
  int mergeStderr = (fd_err ? 0 : 1);
  pid = fork();
  if (pid == 0) {
    int countFd = 0;
    int i;
    if (workdir) {
      if (chdir(workdir) < 0) {
        _exit(126);
      }
    }
    while (dupFds[countFd] > 0) {
      ++countFd;
    }
    closeOtherFds(fd_in[0], fd_out[1], fd_err ? fd_err[1] : 0, dupFds, countFd);
    close(fd_in[1]);
    close(fd_out[0]);
    if (!mergeStderr)
      close(fd_err[0]);
    close(0);
    dup2(fd_in[0], 0);
    close(1);
    dup2(fd_out[1], 1);
    close(2);
    dup2(mergeStderr ? fd_out[1] : fd_err[1], 2);
    for (i = 0; i < countFd; i++) {
      dup2(dupFds[i], 3 + i);
    }
    execve(path, argv, envp);
    _exit(1);
  }
  return pid;
}


x86 assembly (clang, -O0):
<launchProcess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    0x10(%rbp),%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1350 <launchProcess+0x1e0>
movl   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f7 <launchProcess+0x87>
mov    -0x20(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
jge    11f2 <launchProcess+0x82>
mov    $0x7e,%edi
call   1030 <_exit@plt>
jmp    11f7 <launchProcess+0x87>
jmp    11fc <launchProcess+0x8c>
mov    0x18(%rbp),%rax
movslq -0x40(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jle    121c <launchProcess+0xac>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11fc <launchProcess+0x8c>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x48(%rbp)
cmpq   $0x0,0x10(%rbp)
je     1249 <launchProcess+0xd9>
mov    0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x50(%rbp)
jmp    1253 <launchProcess+0xe3>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    1253 <launchProcess+0xe3>
mov    -0x48(%rbp),%esi
mov    -0x4c(%rbp),%edi
mov    -0x50(%rbp),%edx
mov    0x18(%rbp),%rcx
mov    -0x40(%rbp),%r8d
call   1040 <closeOtherFds@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    1295 <launchProcess+0x125>
mov    0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    $0x2,%edi
call   1070 <close@plt>
cmpl   $0x0,-0x3c(%rbp)
je     12e7 <launchProcess+0x177>
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x54(%rbp)
jmp    12f1 <launchProcess+0x181>
mov    0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    1335 <launchProcess+0x1c5>
mov    0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x44(%rbp),%esi
add    $0x3,%esi
call   1060 <dup2@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1305 <launchProcess+0x195>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <execve@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x38(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<launchProcess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8,%rbp
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11a8 <launchProcess+0x38>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %rbx,%rbx
je     11c4 <launchProcess+0x54>
mov    %rbx,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jns    11c4 <launchProcess+0x54>
mov    $0x7e,%edi
call   1030 <_exit@plt>
mov    0x48(%rsp),%rax
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
cmpl   $0x0,(%rax,%rbx,4)
lea    0x1(%rbx),%rbx
jg     11d0 <launchProcess+0x60>
lea    -0x1(%rbx),%r8
mov    0x0(%rbp),%edi
mov    0x4(%r12),%esi
cmpq   $0x0,0x40(%rsp)
jne    1204 <launchProcess+0x94>
xor    %edx,%edx
mov    0x48(%rsp),%rcx
call   1040 <closeOtherFds@plt>
lea    0x4(%rbp),%rax
mov    %r12,(%rsp)
jmp    122a <launchProcess+0xba>
mov    0x40(%rsp),%rax
mov    0x4(%rax),%edx
mov    0x48(%rsp),%rcx
call   1040 <closeOtherFds@plt>
mov    0x4(%rbp),%edi
call   1070 <close@plt>
mov    %r12,%rax
mov    0x40(%rsp),%rcx
mov    %rcx,(%rsp)
mov    (%rax),%edi
call   1070 <close@plt>
mov    (%rsp),%rax
mov    (%rax),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    0x0(%rbp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    $0x1,%edi
call   1070 <close@plt>
mov    0x4(%r12),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    0x40(%rsp),%rax
test   %rax,%rax
cmove  %r12,%rax
mov    0x4(%rax),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
cmp    $0x1,%ebx
mov    0x48(%rsp),%r12
je     12b5 <launchProcess+0x145>
add    $0xffffffff,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbp,4),%edi
lea    0x3(%rbp),%esi
call   1060 <dup2@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    12a0 <launchProcess+0x130>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <execve@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
define i64 @launchProcess(i8* %0, i8** %1, i8** %2, i8* %3, i32* %4, i32* %5, i32* %6, i32* %7) {
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i8**
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32*
%17 = alloca i64
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i8* %0, i8** %9
store i8** %1, i8*** %10
store i8** %2, i8*** %11
store i8* %3, i8** %12
store i32* %4, i32** %13
store i32* %5, i32** %14
store i32* %6, i32** %15
store i32* %7, i32** %16
%21 = load i32*, i32** %15
%22 = icmp ne i32* %21, null
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 0, i32 1
store i32 %24, i32* %18
%25 = call i64 (...) @fork()
store i64 %25, i64* %17
%26 = load i64, i64* %17
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %130
28: ; preds = %8
store i32 0, i32* %19
%29 = load i8*, i8** %12
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i8*, i8** %12
%33 = call i64 @chdir(i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @_exit(i32 126)
unreachable
37: ; preds = %31
br label %38
38: ; preds = %37, %28
br label %39
39: ; preds = %46, %38
%40 = load i32*, i32** %16
%41 = load i32, i32* %19
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* %19
%48 = add nsw i32 %47, 1
store i32 %48, i32* %19
br label %39
49: ; preds = %39
%50 = load i32*, i32** %13
%51 = getelementptr inbounds i32, i32* %50, i64 0
%52 = load i32, i32* %51
%53 = load i32*, i32** %14
%54 = getelementptr inbounds i32, i32* %53, i64 1
%55 = load i32, i32* %54
%56 = load i32*, i32** %15
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %62
58: ; preds = %49
%59 = load i32*, i32** %15
%60 = getelementptr inbounds i32, i32* %59, i64 1
%61 = load i32, i32* %60
br label %63
62: ; preds = %49
br label %63
63: ; preds = %62, %58
%64 = phi i32 [ %61, %58 ], [ 0, %62 ]
%65 = load i32*, i32** %16
%66 = load i32, i32* %19
%67 = call i32 @closeOtherFds(i32 %52, i32 %55, i32 %64, i32* %65, i32 %66)
%68 = load i32*, i32** %13
%69 = getelementptr inbounds i32, i32* %68, i64 1
%70 = load i32, i32* %69
%71 = call i32 @close(i32 %70)
%72 = load i32*, i32** %14
%73 = getelementptr inbounds i32, i32* %72, i64 0
%74 = load i32, i32* %73
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %18
%77 = icmp ne i32 %76, 0
br i1 %77, label %83, label %78
78: ; preds = %63
%79 = load i32*, i32** %15
%80 = getelementptr inbounds i32, i32* %79, i64 0
%81 = load i32, i32* %80
%82 = call i32 @close(i32 %81)
br label %83
83: ; preds = %78, %63
%84 = call i32 @close(i32 0)
%85 = load i32*, i32** %13
%86 = getelementptr inbounds i32, i32* %85, i64 0
%87 = load i32, i32* %86
%88 = call i32 @dup2(i32 %87, i32 0)
%89 = call i32 @close(i32 1)
%90 = load i32*, i32** %14
%91 = getelementptr inbounds i32, i32* %90, i64 1
%92 = load i32, i32* %91
%93 = call i32 @dup2(i32 %92, i32 1)
%94 = call i32 @close(i32 2)
%95 = load i32, i32* %18
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %101
97: ; preds = %83
%98 = load i32*, i32** %14
%99 = getelementptr inbounds i32, i32* %98, i64 1
%100 = load i32, i32* %99
br label %105
101: ; preds = %83
%102 = load i32*, i32** %15
%103 = getelementptr inbounds i32, i32* %102, i64 1
%104 = load i32, i32* %103
br label %105
105: ; preds = %101, %97
%106 = phi i32 [ %100, %97 ], [ %104, %101 ]
%107 = call i32 @dup2(i32 %106, i32 2)
store i32 0, i32* %20
br label %108
108: ; preds = %121, %105
%109 = load i32, i32* %20
%110 = load i32, i32* %19
%111 = icmp slt i32 %109, %110
br i1 %111, label %112, label %124
112: ; preds = %108
%113 = load i32*, i32** %16
%114 = load i32, i32* %20
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds i32, i32* %113, i64 %115
%117 = load i32, i32* %116
%118 = load i32, i32* %20
%119 = add nsw i32 3, %118
%120 = call i32 @dup2(i32 %117, i32 %119)
br label %121
121: ; preds = %112
%122 = load i32, i32* %20
%123 = add nsw i32 %122, 1
store i32 %123, i32* %20
br label %108
124: ; preds = %108
%125 = load i8*, i8** %9
%126 = load i8**, i8*** %10
%127 = load i8**, i8*** %11
%128 = call i32 @execve(i8* %125, i8** %126, i8** %127)
%129 = call i32 @_exit(i32 1)
unreachable
130: ; preds = %8
%131 = load i64, i64* %17
ret i64 %131
}

LLVM IR (-O2):
define i64 @launchProcess(i8* %0, i8** %1, i8** %2, i8* %3, i32* nocapture %4, i32* nocapture %5, i32* %6, i32* %7) {
%9 = icmp eq i32* %6, null
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %74
12: ; preds = %8
%13 = icmp eq i8* %3, null
br i1 %13, label %14, label %15
14: ; preds = %15, %12
br label %20
15: ; preds = %12
%16 = tail call i64 @chdir(i8* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %14
18: ; preds = %15
%19 = tail call i32 @_exit(i32 126)
unreachable
20: ; preds = %14, %20
%21 = phi i64 [ %25, %20 ], [ 0, %14 ]
%22 = getelementptr inbounds i32, i32* %7, i64 %21
%23 = load i32, i32* %22
%24 = icmp sgt i32 %23, 0
%25 = add nuw i64 %21, 1
br i1 %24, label %20, label %26
26: ; preds = %20
%27 = trunc i64 %21 to i32
%28 = load i32, i32* %4
%29 = getelementptr inbounds i32, i32* %5, i64 1
%30 = load i32, i32* %29
br i1 %9, label %38, label %31
31: ; preds = %26
%32 = getelementptr inbounds i32, i32* %6, i64 1
%33 = load i32, i32* %32
%34 = tail call i32 @closeOtherFds(i32 %28, i32 %30, i32 %33, i32* %7, i32 %27)
%35 = getelementptr inbounds i32, i32* %4, i64 1
%36 = load i32, i32* %35
%37 = tail call i32 @close(i32 %36)
br label %41
38: ; preds = %26
%39 = tail call i32 @closeOtherFds(i32 %28, i32 %30, i32 0, i32* %7, i32 %27)
%40 = getelementptr inbounds i32, i32* %4, i64 1
br label %41
41: ; preds = %38, %31
%42 = phi i32* [ %40, %38 ], [ %5, %31 ]
%43 = phi i32* [ %5, %38 ], [ %6, %31 ]
%44 = load i32, i32* %42
%45 = tail call i32 @close(i32 %44)
%46 = load i32, i32* %43
%47 = tail call i32 @close(i32 %46)
%48 = tail call i32 @close(i32 0)
%49 = load i32, i32* %4
%50 = tail call i32 @dup2(i32 %49, i32 0)
%51 = tail call i32 @close(i32 1)
%52 = load i32, i32* %29
%53 = tail call i32 @dup2(i32 %52, i32 1)
%54 = tail call i32 @close(i32 2)
%55 = getelementptr inbounds i32, i32* %6, i64 1
%56 = select i1 %9, i32* %29, i32* %55
%57 = load i32, i32* %56
%58 = tail call i32 @dup2(i32 %57, i32 2)
%59 = icmp eq i32 %27, 0
br i1 %59, label %71, label %60
60: ; preds = %41
%61 = and i64 %21, 4294967295
br label %62
62: ; preds = %60, %62
%63 = phi i64 [ 0, %60 ], [ %69, %62 ]
%64 = getelementptr inbounds i32, i32* %7, i64 %63
%65 = load i32, i32* %64
%66 = trunc i64 %63 to i32
%67 = add i32 %66, 3
%68 = tail call i32 @dup2(i32 %65, i32 %67)
%69 = add nuw nsw i64 %63, 1
%70 = icmp eq i64 %69, %61
br i1 %70, label %71, label %62
71: ; preds = %62, %41
%72 = tail call i32 @execve(i8* %0, i8** %1, i8** %2)
%73 = tail call i32 @_exit(i32 1)
unreachable
74: ; preds = %8
ret i64 %10
}