#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char **);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ realloc(char **, int);
int /*<<< orphan*/ stderr;
char *strtok(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void start_new_program(void *command) {
  pid_t pid;
  char *cmd = (char *)command;
  pid = fork();
  if (pid > 0) {
    int status;
    waitpid(pid, &status, 0);
    if (status) {
      fprintf(stderr, "Unexpected termination: pid: %d\n", pid);
    }
  } else if (pid == 0) {
    int ret_val = EXIT_SUCCESS;
    char **argv = NULL;
    char *arg = strtok(cmd, " ");
    int i = 0;
    while (arg != NULL) {
      if (!argv)
        argv = (char **)malloc(sizeof(char *));
      else
        argv = (char **)realloc(argv, sizeof(char *));
      argv[i++] = arg;
      arg = strtok(NULL, " ");
    }
    argv = (char **)realloc(argv, sizeof(char *));
    argv[i] = NULL;
    if (!execvp(*argv, argv)) {
      perror("shell:");
      ret_val = EXIT_FAILURE;
    }
    fprintf(stderr, "command not found: %s\n", *argv);
    free(argv);
    exit(ret_val);
  } else {
    perror("shell: start_new_program");
  }
}


x86 assembly (clang, -O0):
<start_new_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    1201 <start_new_program+0x61>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmpl   $0x0,-0x1c(%rbp)
je     11fc <start_new_program+0x5c>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe09(%rip),%rsi
call   1040 <fprintf@plt>
jmp    133a <start_new_program+0x19a>
cmpq   $0x0,-0x10(%rbp)
jne    1329 <start_new_program+0x189>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12a9 <start_new_program+0x109>
cmpq   $0x0,-0x28(%rbp)
jne    1264 <start_new_program+0xc4>
mov    $0x8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
jmp    1276 <start_new_program+0xd6>
mov    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd86(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    123b <start_new_program+0x9b>
mov    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <execvp@plt>
cmp    $0x0,%eax
jne    12fc <start_new_program+0x15c>
lea    0xd38(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
lea    0xd17(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%edi
call   10b0 <exit@plt>
lea    0xd11(%rip),%rdi
call   1080 <perror@plt>
jmp    133a <start_new_program+0x19a>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_new_program>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jle    11f9 <start_new_program+0x59>
mov    %rax,%rbx
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmpl   $0x0,0xc(%rsp)
je     11ec <start_new_program+0x4c>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
je     1213 <start_new_program+0x73>
lea    0xe3f(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%r14d
lea    0xdfd(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strtok@plt>
xor    %r12d,%r12d
mov    $0x0,%r15d
test   %rax,%rax
je     128e <start_new_program+0xee>
mov    %rax,%rbp
xor    %ebx,%ebx
lea    0xddb(%rip),%r15
xor    %r12d,%r12d
jmp    127a <start_new_program+0xda>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x8,%esi
call   1060 <realloc@plt>
mov    %rax,%r12
mov    %rbp,(%rax,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1090 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
je     128b <start_new_program+0xeb>
test   %r12,%r12
jne    1250 <start_new_program+0xb0>
mov    $0x8,%edi
call   1050 <malloc@plt>
jmp    125d <start_new_program+0xbd>
mov    %ebx,%r15d
mov    %r12,%rdi
mov    $0x8,%esi
call   1060 <realloc@plt>
mov    %rax,%rbx
movq   $0x0,(%rax,%r15,8)
mov    (%rax),%rdi
mov    %rax,%rsi
call   10a0 <execvp@plt>
test   %eax,%eax
jne    12cb <start_new_program+0x12b>
lea    0xd67(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%r14d
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd4c(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Unexpected termination: pid: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [7 x i8] c"shell:\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [23 x i8] c"command not found: %s\0A\00"
@.str.4 = [25 x i8] c"shell: start_new_program\00"
define void @start_new_program(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
store i8* %10, i8** %4
%11 = call i64 (...) @fork()
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %25
14: ; preds = %1
%15 = load i64, i64* %3
%16 = call i32 @waitpid(i64 %15, i32* %5, i32 0)
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i64, i64* %3
%22 = inttoptr i64 %21 to i8*
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %19, %14
br label %81
25: ; preds = %1
%26 = load i64, i64* %3
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %78
28: ; preds = %25
%29 = load i32, i32* @EXIT_SUCCESS
store i32 %29, i32* %6
store i8** null, i8*** %7
%30 = load i8*, i8** %4
%31 = call i8* @strtok(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %31, i8** %8
store i32 0, i32* %9
br label %32
32: ; preds = %45, %28
%33 = load i8*, i8** %8
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %53
35: ; preds = %32
%36 = load i8**, i8*** %7
%37 = icmp ne i8** %36, null
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = call i64 @malloc(i32 8)
%40 = inttoptr i64 %39 to i8**
store i8** %40, i8*** %7
br label %45
41: ; preds = %35
%42 = load i8**, i8*** %7
%43 = call i64 @realloc(i8** %42, i32 8)
%44 = inttoptr i64 %43 to i8**
store i8** %44, i8*** %7
br label %45
45: ; preds = %41, %38
%46 = load i8*, i8** %8
%47 = load i8**, i8*** %7
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8*, i8** %47, i64 %50
store i8* %46, i8** %51
%52 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %52, i8** %8
br label %32
53: ; preds = %32
%54 = load i8**, i8*** %7
%55 = call i64 @realloc(i8** %54, i32 8)
%56 = inttoptr i64 %55 to i8**
store i8** %56, i8*** %7
%57 = load i8**, i8*** %7
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
store i8* null, i8** %60
%61 = load i8**, i8*** %7
%62 = load i8*, i8** %61
%63 = load i8**, i8*** %7
%64 = call i32 @execvp(i8* %62, i8** %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %53
%67 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
store i32 %68, i32* %6
br label %69
69: ; preds = %66, %53
%70 = load i32, i32* @stderr
%71 = load i8**, i8*** %7
%72 = load i8*, i8** %71
%73 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %72)
%74 = load i8**, i8*** %7
%75 = call i32 @free(i8** %74)
%76 = load i32, i32* %6
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %25
%79 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78
br label %81
81: ; preds = %80, %24
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Unexpected termination: pid: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [7 x i8] c"shell:\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [23 x i8] c"command not found: %s\0A\00"
@.str.4 = [25 x i8] c"shell: start_new_program\00"
define void @start_new_program(i8* %0) {
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = icmp sgt i64 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = bitcast i32* %2 to i8*
%7 = call i32 @waitpid(i64 %3, i32* %2, i32 0)
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = inttoptr i64 %3 to i8*
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %10, %5
br label %61
15: ; preds = %1
%16 = icmp eq i64 %3, 0
br i1 %16, label %17, label %59
17: ; preds = %15
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %40, label %21
21: ; preds = %17, %30
%22 = phi i64 [ %33, %30 ], [ 0, %17 ]
%23 = phi i8* [ %35, %30 ], [ %19, %17 ]
%24 = phi i8** [ %32, %30 ], [ null, %17 ]
%25 = icmp eq i8** %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i64 @malloc(i32 8)
br label %30
28: ; preds = %21
%29 = tail call i64 @realloc(i8** %24, i32 8)
br label %30
30: ; preds = %28, %26
%31 = phi i64 [ %29, %28 ], [ %27, %26 ]
%32 = inttoptr i64 %31 to i8**
%33 = add nuw i64 %22, 1
%34 = getelementptr inbounds i8*, i8** %32, i64 %22
store i8* %23, i8** %34
%35 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %21
37: ; preds = %30
%38 = inttoptr i64 %31 to i8**
%39 = and i64 %33, 4294967295
br label %40
40: ; preds = %37, %17
%41 = phi i8** [ %38, %37 ], [ null, %17 ]
%42 = phi i64 [ %39, %37 ], [ 0, %17 ]
%43 = tail call i64 @realloc(i8** %41, i32 8)
%44 = inttoptr i64 %43 to i8**
%45 = getelementptr inbounds i8*, i8** %44, i64 %42
store i8* null, i8** %45
%46 = load i8*, i8** %44
%47 = tail call i32 @execvp(i8* %46, i8** %44)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %40
%50 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
br label %52
52: ; preds = %49, %40
%53 = phi i32 [ %18, %40 ], [ %51, %49 ]
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %44
%56 = tail call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = tail call i32 @free(i8** %44)
%58 = tail call i32 @exit(i32 %53)
unreachable
59: ; preds = %15
%60 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %14
ret void
}