#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 */
struct CoSimComm {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ createComm(struct CoSimComm **, struct CoSimComm **);
int /*<<< orphan*/ disposeComm(struct CoSimComm **);
int /*<<< orphan*/ disposeSharedComm();
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shareComm(struct CoSimComm **);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int startProcess(char **arguments, struct CoSimComm **comm, int stdOut,
                 int waitProcess) {
  int fdNull;
  int pid, returnStatus;
  struct CoSimComm *commExt = NULL;
  if (arguments == NULL || arguments[0] == NULL) {
    printf("Arguments for creating process is null\n");
    return -1;
  }
  if (!waitProcess) {
    if (createComm(comm, &commExt) < 0)
      return -1;
  }
  pid = fork();
  if (pid < 0) {
    printf("Unable to fork\n");
    if (!waitProcess) {
      disposeComm(comm);
      disposeComm(&commExt);
    }
    return -1;
  } else if (pid == 0) {
    if (!waitProcess) {
      disposeComm(comm);
      if (shareComm(&commExt) < 0) {
        disposeSharedComm();
        _exit(-1);
      }
    }
    if (!stdOut) {
      fdNull = open("/dev/null", O_RDWR);
      dup2(fdNull, 1);
      if (fdNull > 2)
        close(fdNull);
    }
    execvp(arguments[0], arguments);
    if (!waitProcess) {
      disposeSharedComm();
      _exit(-1);
    }
  } else {
    if (waitProcess) {
      waitpid(pid, &returnStatus, 0);
      if (returnStatus != 0) {
        printf("Error with creating process '%s'\n", arguments[0]);
        return -1;
      }
    } else {
      disposeComm(&commExt);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<startProcess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f7 <startProcess+0x37>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1211 <startProcess+0x51>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <startProcess+0x1eb>
cmpl   $0x0,-0x20(%rbp)
jne    1243 <startProcess+0x83>
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1030 <createComm@plt>
cmp    $0x0,%rax
jge    123e <startProcess+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <startProcess+0x1eb>
jmp    1243 <startProcess+0x83>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    128d <startProcess+0xcd>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1281 <startProcess+0xc1>
mov    -0x18(%rbp),%rdi
call   1060 <disposeComm@plt>
lea    -0x38(%rbp),%rdi
call   1060 <disposeComm@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <startProcess+0x1eb>
cmpl   $0x0,-0x28(%rbp)
jne    1349 <startProcess+0x189>
cmpl   $0x0,-0x20(%rbp)
jne    12d3 <startProcess+0x113>
mov    -0x18(%rbp),%rdi
call   1060 <disposeComm@plt>
lea    -0x38(%rbp),%rdi
call   1040 <shareComm@plt>
cmp    $0x0,%rax
jge    12ce <startProcess+0x10e>
mov    $0x0,%al
call   10a0 <disposeSharedComm@plt>
mov    $0xffffffff,%edi
call   1050 <_exit@plt>
jmp    12d3 <startProcess+0x113>
cmpl   $0x0,-0x1c(%rbp)
jne    1319 <startProcess+0x159>
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
lea    0xd4b(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
cmpl   $0x2,-0x24(%rbp)
jle    1314 <startProcess+0x154>
mov    -0x24(%rbp),%edi
call   1090 <close@plt>
jmp    1319 <startProcess+0x159>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <execvp@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1344 <startProcess+0x184>
mov    $0x0,%al
call   10a0 <disposeSharedComm@plt>
mov    $0xffffffff,%edi
call   1050 <_exit@plt>
jmp    139f <startProcess+0x1df>
cmpl   $0x0,-0x20(%rbp)
je     1391 <startProcess+0x1d1>
mov    -0x28(%rbp),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmpl   $0x0,-0x2c(%rbp)
je     138c <startProcess+0x1cc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xcc9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <startProcess+0x1eb>
jmp    139a <startProcess+0x1da>
lea    -0x38(%rbp),%rdi
call   1060 <disposeComm@plt>
jmp    139f <startProcess+0x1df>
jmp    13a4 <startProcess+0x1e4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startProcess>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
test   %rdi,%rdi
je     124f <startProcess+0x7f>
mov    %rdi,%rbx
cmpq   $0x0,(%rdi)
je     124f <startProcess+0x7f>
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%r12
test   %ecx,%ecx
jne    120f <startProcess+0x3f>
mov    %rsp,%rsi
mov    %r12,%rdi
call   1030 <createComm@plt>
test   %rax,%rax
js     125b <startProcess+0x8b>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
js     126f <startProcess+0x9f>
je     1297 <startProcess+0xc7>
test   %r15d,%r15d
je     130c <startProcess+0x13c>
lea    0xc(%rsp),%rsi
xor    %ebp,%ebp
mov    %eax,%edi
xor    %edx,%edx
call   10c0 <waitpid@plt>
cmpl   $0x0,0xc(%rsp)
je     1260 <startProcess+0x90>
mov    (%rbx),%rsi
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
jmp    125b <startProcess+0x8b>
lea    0xde5(%rip),%rdi
call   1060 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb6(%rip),%rdi
call   1060 <puts@plt>
mov    $0xffffffff,%ebp
test   %r15d,%r15d
jne    1260 <startProcess+0x90>
mov    %r12,%rdi
call   1070 <disposeComm@plt>
mov    %rsp,%rdi
call   1070 <disposeComm@plt>
jmp    1260 <startProcess+0x90>
test   %r15d,%r15d
jne    12b1 <startProcess+0xe1>
mov    %r12,%rdi
call   1070 <disposeComm@plt>
mov    %rsp,%rdi
call   1040 <shareComm@plt>
test   %rax,%rax
js     12fb <startProcess+0x12b>
test   %r14d,%r14d
jne    12e5 <startProcess+0x115>
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
lea    0xd3a(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <dup2@plt>
cmp    $0x3,%ebp
jl     12e5 <startProcess+0x115>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10e0 <execvp@plt>
xor    %ebp,%ebp
test   %r15d,%r15d
jne    1260 <startProcess+0x90>
xor    %eax,%eax
call   10b0 <disposeSharedComm@plt>
mov    $0xffffffff,%edi
call   1050 <_exit@plt>
mov    %rsp,%rdi
call   1070 <disposeComm@plt>
xor    %ebp,%ebp
jmp    1260 <startProcess+0x90>

LLVM IR (-O0):
%struct.CoSimComm = type { i32 }
@.str = [40 x i8] c"Arguments for creating process is null\0A\00"
@.str.1 = [16 x i8] c"Unable to fork\0A\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [34 x i8] c"Error with creating process '%s'\0A\00"
define i32 @startProcess(i8** %0, %struct.CoSimComm** %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca %struct.CoSimComm**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.CoSimComm*
store i8** %0, i8*** %6
store %struct.CoSimComm** %1, %struct.CoSimComm*** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store %struct.CoSimComm* null, %struct.CoSimComm** %13
%14 = load i8**, i8*** %6
%15 = icmp eq i8** %14, null
br i1 %15, label %21, label %16
16: ; preds = %4
%17 = load i8**, i8*** %6
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16, %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %105
23: ; preds = %16
%24 = load i32, i32* %9
%25 = icmp ne i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = load %struct.CoSimComm**, %struct.CoSimComm*** %7
%28 = call i64 @createComm(%struct.CoSimComm** %27, %struct.CoSimComm** %13)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -1, i32* %5
br label %105
31: ; preds = %26
br label %32
32: ; preds = %31, %23
%33 = call i32 (...) @fork()
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load %struct.CoSimComm**, %struct.CoSimComm*** %7
%42 = call i32 @disposeComm(%struct.CoSimComm** %41)
%43 = call i32 @disposeComm(%struct.CoSimComm** %13)
br label %44
44: ; preds = %40, %36
store i32 -1, i32* %5
br label %105
45: ; preds = %32
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %86
48: ; preds = %45
%49 = load i32, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %60, label %51
51: ; preds = %48
%52 = load %struct.CoSimComm**, %struct.CoSimComm*** %7
%53 = call i32 @disposeComm(%struct.CoSimComm** %52)
%54 = call i64 @shareComm(%struct.CoSimComm** %13)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = call i32 (...) @disposeSharedComm()
%58 = call i32 @_exit(i32 -1)
unreachable
59: ; preds = %51
br label %60
60: ; preds = %59, %48
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %74, label %63
63: ; preds = %60
%64 = load i32, i32* @O_RDWR
%65 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %64)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = call i32 @dup2(i32 %66, i32 1)
%68 = load i32, i32* %10
%69 = icmp sgt i32 %68, 2
br i1 %69, label %70, label %73
70: ; preds = %63
%71 = load i32, i32* %10
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %63
br label %74
74: ; preds = %73, %60
%75 = load i8**, i8*** %6
%76 = getelementptr inbounds i8*, i8** %75, i64 0
%77 = load i8*, i8** %76
%78 = load i8**, i8*** %6
%79 = call i32 @execvp(i8* %77, i8** %78)
%80 = load i32, i32* %9
%81 = icmp ne i32 %80, 0
br i1 %81, label %85, label %82
82: ; preds = %74
%83 = call i32 (...) @disposeSharedComm()
%84 = call i32 @_exit(i32 -1)
unreachable
85: ; preds = %74
br label %103
86: ; preds = %45
%87 = load i32, i32* %9
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %100
89: ; preds = %86
%90 = load i32, i32* %11
%91 = call i32 @waitpid(i32 %90, i32* %12, i32 0)
%92 = load i32, i32* %12
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %99
94: ; preds = %89
%95 = load i8**, i8*** %6
%96 = getelementptr inbounds i8*, i8** %95, i64 0
%97 = load i8*, i8** %96
%98 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %97)
store i32 -1, i32* %5
br label %105
99: ; preds = %89
br label %102
100: ; preds = %86
%101 = call i32 @disposeComm(%struct.CoSimComm** %13)
br label %102
102: ; preds = %100, %99
br label %103
103: ; preds = %102, %85
br label %104
104: ; preds = %103
store i32 0, i32* %5
br label %105
105: ; preds = %104, %94, %44, %30, %21
%106 = load i32, i32* %5
ret i32 %106
}

LLVM IR (-O2):
%struct.CoSimComm = type { i32 }
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [34 x i8] c"Error with creating process '%s'\0A\00"
@str = [15 x i8] c"Unable to fork\00"
@str.4 = [39 x i8] c"Arguments for creating process is null\00"
define i32 @startProcess(i8** %0, %struct.CoSimComm** %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.CoSimComm*
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.CoSimComm** %6 to i8*
store %struct.CoSimComm* null, %struct.CoSimComm** %6
%9 = icmp eq i8** %0, null
br i1 %9, label %13, label %10
10: ; preds = %4
%11 = load i8*, i8** %0
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10, %4
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.4, i64 0, i64 0))
br label %63
15: ; preds = %10
%16 = icmp eq i32 %3, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = call i64 @createComm(%struct.CoSimComm** %1, %struct.CoSimComm** %6)
%19 = icmp slt i64 %18, 0
br i1 %19, label %63, label %20
20: ; preds = %17, %15
%21 = call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br i1 %16, label %25, label %63
25: ; preds = %23
%26 = call i32 @disposeComm(%struct.CoSimComm** %1)
%27 = call i32 @disposeComm(%struct.CoSimComm** %6)
br label %63
28: ; preds = %20
%29 = icmp eq i32 %21, 0
br i1 %29, label %30, label %53
30: ; preds = %28
br i1 %16, label %31, label %38
31: ; preds = %30
%32 = call i32 @disposeComm(%struct.CoSimComm** %1)
%33 = call i64 @shareComm(%struct.CoSimComm** %6)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 (...) @disposeSharedComm()
%37 = call i32 @_exit(i32 -1)
unreachable
38: ; preds = %31, %30
%39 = icmp eq i32 %2, 0
br i1 %39, label %40, label %47
40: ; preds = %38
%41 = load i32, i32* @O_RDWR
%42 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = call i32 @dup2(i32 %42, i32 1)
%44 = icmp sgt i32 %42, 2
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @close(i32 %42)
br label %47
47: ; preds = %40, %45, %38
%48 = load i8*, i8** %0
%49 = call i32 @execvp(i8* %48, i8** %0)
br i1 %16, label %50, label %63
50: ; preds = %47
%51 = call i32 (...) @disposeSharedComm()
%52 = call i32 @_exit(i32 -1)
unreachable
53: ; preds = %28
br i1 %16, label %61, label %54
54: ; preds = %53
%55 = call i32 @waitpid(i32 %21, i32* %5, i32 0)
%56 = load i32, i32* %5
%57 = icmp eq i32 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %54
%59 = load i8*, i8** %0
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %59)
br label %63
61: ; preds = %53
%62 = call i32 @disposeComm(%struct.CoSimComm** %6)
br label %63
63: ; preds = %61, %54, %47, %23, %25, %17, %58, %13
%64 = phi i32 [ -1, %13 ], [ -1, %58 ], [ -1, %17 ], [ -1, %25 ], [ -1, %23 ], [ 0, %47 ], [ 0, %54 ], [ 0, %61 ]
ret i32 %64
}