#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 /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strtok(char *, char *);
int vfork();
int exec_run_fork(char *cmd, char *args) {
  char *argv[100];
  char *pch;
  int narg;
  narg = 0;
  argv[narg] = cmd;
  narg++;
  pch = args;
  do {
    printf("%d - %d (%s)\n", narg, pch - args, pch);
    pch = strtok(narg == 1 ? args : NULL, " ");
    if (pch != NULL) {
      *(pch - 1) = '\0';
    }
    argv[narg] = pch;
    narg++;
  } while (pch != NULL && narg < 100);
  printf("Running command with %d args: \n", narg);
  int i;
  for (i = 0; i < narg; i++) {
    printf("%s ", argv[i]);
  }
  printf("\n");
  int pid = vfork();
  if (pid == -1) {
    perror("fork");
  } else if (pid == 0) {
    execv(cmd, argv);
    perror("exec");
    exit(-1);
  }
  return pid;
}


x86 assembly (clang, -O0):
<exec_run_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x360,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x33c(%rbp)
mov    -0x8(%rbp),%rcx
movslq -0x33c(%rbp),%rax
mov    %rcx,-0x330(%rbp,%rax,8)
ff
mov    -0x33c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x33c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x338(%rbp)
mov    -0x33c(%rbp),%esi
mov    -0x338(%rbp),%rdx
mov    -0x10(%rbp),%rax
sub    %rax,%rdx
mov    -0x338(%rbp),%rcx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x33c(%rbp)
jne    11f0 <exec_run_fork+0x90>
mov    -0x10(%rbp),%rax
mov    %rax,-0x350(%rbp)
jmp    11fe <exec_run_fork+0x9e>
xor    %eax,%eax
mov    %rax,-0x350(%rbp)
jmp    11fe <exec_run_fork+0x9e>
mov    -0x350(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x338(%rbp)
cmpq   $0x0,-0x338(%rbp)
je     1231 <exec_run_fork+0xd1>
mov    -0x338(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x338(%rbp),%rcx
movslq -0x33c(%rbp),%rax
mov    %rcx,-0x330(%rbp,%rax,8)
ff
mov    -0x33c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x33c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x338(%rbp)
mov    %al,-0x351(%rbp)
je     127c <exec_run_fork+0x11c>
cmpl   $0x64,-0x33c(%rbp)
setl   %al
mov    %al,-0x351(%rbp)
mov    -0x351(%rbp),%al
test   $0x1,%al
jne    11aa <exec_run_fork+0x4a>
mov    -0x33c(%rbp),%esi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x340(%rbp)
mov    -0x340(%rbp),%eax
cmp    -0x33c(%rbp),%eax
jge    12eb <exec_run_fork+0x18b>
movslq -0x340(%rbp),%rax
mov    -0x330(%rbp,%rax,8),%rsi
ff
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x340(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x340(%rbp)
jmp    12a8 <exec_run_fork+0x148>
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
call   1060 <vfork@plt>
mov    %eax,-0x344(%rbp)
cmpl   $0xffffffff,-0x344(%rbp)
jne    1322 <exec_run_fork+0x1c2>
lea    0xd1a(%rip),%rdi
call   1040 <perror@plt>
jmp    135a <exec_run_fork+0x1fa>
cmpl   $0x0,-0x344(%rbp)
jne    1355 <exec_run_fork+0x1f5>
mov    -0x8(%rbp),%rdi
lea    -0x330(%rbp),%rsi
call   1080 <execv@plt>
lea    0xcf1(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    135a <exec_run_fork+0x1fa>
mov    -0x344(%rbp),%eax
add    $0x360,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_run_fork>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x328,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdi,(%rsp)
xor    %r13d,%r13d
mov    %r13d,%eax
lea    0xe68(%rip),%r15
lea    0xe91(%rip),%r12
mov    %rsi,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rbp
mov    %rcx,%rdx
sub    %rbx,%rdx
lea    0x1(%rax),%esi
mov    %r13d,%eax
mov    %r15,%rdi
call   1040 <printf@plt>
test   %rbp,%rbp
mov    %r13d,%edi
cmove  %rbx,%rdi
mov    %r12,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     1204 <exec_run_fork+0x94>
mov    %rax,%rcx
movb   $0x0,-0x1(%rax)
mov    %rax,0x8(%rsp,%rbp,8)
add    $0x1,%rbp
mov    %rbp,%rax
cmp    $0x63,%rbp
jb     11b0 <exec_run_fork+0x40>
mov    %ebp,%eax
mov    %ebp,%ecx
add    $0x1,%ecx
mov    %ecx,%eax
mov    %ecx,%esi
jmp    1214 <exec_run_fork+0xa4>
movq   $0x0,0x8(%rsp,%rbp,8)
mov    %ebp,%eax
add    $0x2,%eax
mov    %eax,%esi
mov    %eax,%ebx
lea    0xdf1(%rip),%rdi
xor    %r15d,%r15d
mov    %r15d,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
mov    %eax,%ebp
mov    %r15d,%eax
lea    0xdf8(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    (%rsp,%rax,8),%rsi
mov    %r15d,%eax
mov    %r12,%rdi
call   1040 <printf@plt>
add    $0x1,%rbx
mov    %rbx,%rax
cmp    %rbx,%rbp
jne    1240 <exec_run_fork+0xd0>
mov    $0xa,%edi
call   1030 <putchar@plt>
call   1070 <vfork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     128c <exec_run_fork+0x11c>
test   %ebx,%ebx
je     129a <exec_run_fork+0x12a>
mov    %ebx,%eax
add    $0x328,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
call   1050 <perror@plt>
jmp    1278 <exec_run_fork+0x108>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1090 <execv@plt>
lea    0xd8b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"%d - %d (%s)\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [32 x i8] c"Running command with %d args: \0A\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [5 x i8] c"exec\00"
define i32 @exec_run_fork(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [100 x i8*]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
%10 = load i8*, i8** %3
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [100 x i8*], [100 x i8*]* %5, i64 0, i64 %12
store i8* %10, i8** %13
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
%16 = load i8*, i8** %4
store i8* %16, i8** %6
br label %17
17: ; preds = %52, %2
%18 = load i32, i32* %7
%19 = load i8*, i8** %6
%20 = load i8*, i8** %4
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = load i8*, i8** %6
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %18, i64 %23, i8* %24)
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = load i8*, i8** %4
br label %31
30: ; preds = %17
br label %31
31: ; preds = %30, %28
%32 = phi i8* [ %29, %28 ], [ null, %30 ]
%33 = call i8* @strtok(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i8*, i8** %6
%38 = getelementptr inbounds i8, i8* %37, i64 -1
store i8 0, i8* %38
br label %39
39: ; preds = %36, %31
%40 = load i8*, i8** %6
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [100 x i8*], [100 x i8*]* %5, i64 0, i64 %42
store i8* %40, i8** %43
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %46
46: ; preds = %39
%47 = load i8*, i8** %6
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* %7
%51 = icmp slt i32 %50, 100
br label %52
52: ; preds = %49, %46
%53 = phi i1 [ false, %46 ], [ %51, %49 ]
br i1 %53, label %17, label %54
54: ; preds = %52
%55 = load i32, i32* %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %55)
store i32 0, i32* %8
br label %57
57: ; preds = %67, %54
%58 = load i32, i32* %8
%59 = load i32, i32* %7
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %70
61: ; preds = %57
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [100 x i8*], [100 x i8*]* %5, i64 0, i64 %63
%65 = load i8*, i8** %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %61
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 1
store i32 %69, i32* %8
br label %57
70: ; preds = %57
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%72 = call i32 @vfork()
store i32 %72, i32* %9
%73 = load i32, i32* %9
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %87
77: ; preds = %70
%78 = load i32, i32* %9
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %77
%81 = load i8*, i8** %3
%82 = getelementptr inbounds [100 x i8*], [100 x i8*]* %5, i64 0, i64 0
%83 = call i32 @execv(i8* %81, i8** %82)
%84 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%85 = call i32 @exit(i32 -1)
unreachable
86: ; preds = %77
br label %87
87: ; preds = %86, %75
%88 = load i32, i32* %9
ret i32 %88
}

LLVM IR (-O2):
@.str = [14 x i8] c"%d - %d (%s)\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [32 x i8] c"Running command with %d args: \0A\00"
@.str.3 = [4 x i8] c"%s \00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [5 x i8] c"exec\00"
define i32 @exec_run_fork(i8* %0, i8* %1) {
%3 = alloca [100 x i8*]
%4 = bitcast [100 x i8*]* %3 to i8*
%5 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 0
store i8* %0, i8** %5
%6 = ptrtoint i8* %1 to i64
br label %7
7: ; preds = %22, %2
%8 = phi i64 [ %25, %22 ], [ 1, %2 ]
%9 = phi i8* [ %16, %22 ], [ %1, %2 ]
%10 = ptrtoint i8* %9 to i64
%11 = sub i64 %10, %6
%12 = trunc i64 %8 to i32
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12, i64 %11, i8* %9)
%14 = icmp eq i64 %8, 1
%15 = select i1 %14, i8* %1, i8* null
%16 = call i8* @strtok(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %7
%19 = trunc i64 %8 to i32
%20 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 %8
store i8* null, i8** %20
%21 = add nuw nsw i32 %19, 1
br label %31
22: ; preds = %7
%23 = getelementptr inbounds i8, i8* %16, i64 -1
store i8 0, i8* %23
%24 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 %8
store i8* %16, i8** %24
%25 = add nuw nsw i64 %8, 1
%26 = icmp ult i64 %8, 99
br i1 %26, label %7, label %27
27: ; preds = %22
%28 = trunc i64 %8 to i32
%29 = trunc i64 %25 to i32
%30 = add nuw nsw i32 %28, 1
br label %31
31: ; preds = %27, %18
%32 = phi i32 [ %30, %27 ], [ %21, %18 ]
%33 = phi i32 [ %29, %27 ], [ %21, %18 ]
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = zext i32 %32 to i64
br label %36
36: ; preds = %31, %36
%37 = phi i64 [ 0, %31 ], [ %41, %36 ]
%38 = getelementptr inbounds [100 x i8*], [100 x i8*]* %3, i64 0, i64 %37
%39 = load i8*, i8** %38
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = add nuw nsw i64 %37, 1
%42 = icmp eq i64 %41, %35
br i1 %42, label %43, label %36
43: ; preds = %36
%44 = call i32 @putchar(i32 10)
%45 = call i32 @vfork()
switch i32 %45, label %52 [
i32 -1, label %46
i32 0, label %48
]
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %52
48: ; preds = %43
%49 = call i32 @execv(i8* %0, i8** %5)
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %43, %46
ret i32 %45
}