#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*/ chdir(char const *);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void execvp_fork(const char *cwd, char **argv) {
  fputs("cwd: ", stderr);
  fputs(cwd, stderr);
  fputs(": exec:", stderr);
  for (char **it = argv; *it; ++it) {
    putc(' ', stderr);
    fputs(*it, stderr);
  }
  putc('\n', stderr);
  switch (fork()) {
  case -1:
    perror("fork");
    return;
  case 0:
    chdir(cwd);
    execvp(argv[0], argv);
    perror("execv");
  }
}


x86 assembly (clang, -O0):
<execvp_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     11fb <execvp_fork+0x9b>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    $0x20,%edi
call   1050 <putc@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    11b4 <execvp_fork+0x54>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    $0xa,%edi
call   1050 <putc@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     1238 <execvp_fork+0xd8>
jmp    1228 <execvp_fork+0xc8>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     1249 <execvp_fork+0xe9>
jmp    126e <execvp_fork+0x10e>
lea    0xdcf(%rip),%rdi
call   1060 <perror@plt>
jmp    126e <execvp_fork+0x10e>
mov    -0x8(%rbp),%rdi
call   1030 <chdir@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xdaa(%rip),%rdi
call   1060 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execvp_fork>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e6b(%rip),%r12
mov    (%r12),%esi
lea    0xe80(%rip),%rdi
call   1040 <fputs@plt>
mov    (%r12),%esi
mov    %r14,%rdi
call   1040 <fputs@plt>
mov    (%r12),%esi
lea    0xe6a(%rip),%rdi
call   1040 <fputs@plt>
cmpq   $0x0,(%r15)
je     11d5 <execvp_fork+0x75>
lea    0x8(%r15),%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    $0x20,%edi
call   1050 <putc@plt>
mov    -0x8(%rbx),%rdi
mov    (%r12),%esi
call   1040 <fputs@plt>
cmpq   $0x0,(%rbx)
lea    0x8(%rbx),%rbx
jne    11b0 <execvp_fork+0x50>
mov    (%r12),%esi
mov    $0xa,%edi
call   1050 <putc@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11ff <execvp_fork+0x9f>
test   %eax,%eax
je     1208 <execvp_fork+0xa8>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe08(%rip),%rdi
jmp    1222 <execvp_fork+0xc2>
mov    %r14,%rdi
call   1030 <chdir@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1070 <execvp@plt>
lea    0xdf1(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <perror@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"cwd: \00"
@stderr = global i32 0
@.str.1 = [8 x i8] c": exec:\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"execv\00"
define void @execvp_fork(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8**
store i8* %0, i8** %3
store i8** %1, i8*** %4
%6 = load i32, i32* @stderr
%7 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i8*, i8** %3
%9 = load i32, i32* @stderr
%10 = call i32 @fputs(i8* %8, i32 %9)
%11 = load i32, i32* @stderr
%12 = call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i8**, i8*** %4
store i8** %13, i8*** %5
br label %14
14: ; preds = %25, %2
%15 = load i8**, i8*** %5
%16 = load i8*, i8** %15
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @putc(i8 signext 32, i32 %19)
%21 = load i8**, i8*** %5
%22 = load i8*, i8** %21
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* %22, i32 %23)
br label %25
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i32 1
store i8** %27, i8*** %5
br label %14
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = call i32 @putc(i8 signext 10, i32 %29)
%31 = call i32 (...) @fork()
switch i32 %31, label %43 [
i32 -1, label %32
i32 0, label %34
]
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %43
34: ; preds = %28
%35 = load i8*, i8** %3
%36 = call i32 @chdir(i8* %35)
%37 = load i8**, i8*** %4
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = load i8**, i8*** %4
%41 = call i32 @execvp(i8* %39, i8** %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %32, %34, %28
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"cwd: \00"
@stderr = global i32 0
@.str.1 = [8 x i8] c": exec:\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [6 x i8] c"execv\00"
define void @execvp_fork(i8* %0, i8** %1) {
%3 = load i32, i32* @stderr
%4 = tail call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @stderr
%6 = tail call i32 @fputs(i8* %0, i32 %5)
%7 = load i32, i32* @stderr
%8 = tail call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i8*, i8** %1
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %15, %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @putc(i8 signext 10, i32 %12)
%14 = tail call i32 (...) @fork()
switch i32 %14, label %32 [
i32 -1, label %29
i32 0, label %25
]
15: ; preds = %2, %15
%16 = phi i8** [ %22, %15 ], [ %1, %2 ]
%17 = load i32, i32* @stderr
%18 = tail call i32 @putc(i8 signext 32, i32 %17)
%19 = load i8*, i8** %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fputs(i8* %19, i32 %20)
%22 = getelementptr inbounds i8*, i8** %16, i64 1
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %11, label %15
25: ; preds = %11
%26 = tail call i32 @chdir(i8* %0)
%27 = load i8*, i8** %1
%28 = tail call i32 @execvp(i8* %27, i8** %1)
br label %29
29: ; preds = %11, %25
%30 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %25 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %11 ]
%31 = tail call i32 @perror(i8* %30)
br label %32
32: ; preds = %29, %11
ret void
}