#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 int /*<<< orphan*/ path;

/* Variables and functions */
int MAXPATHLEN;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, int);
int /*<<< orphan*/ perror(char *);
int snprintf(char *, int, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
char *strdup(char const *);
int waitpid(int, int *, int /*<<< orphan*/);
int run_with_arg(const char *p1, const char *p2, const char *arg) {
  char path[MAXPATHLEN];
  int rv;
  char *argv[3];
  int status;
  rv = snprintf(path, sizeof(path), "%s/%s", p1, p2);
  assert(rv < sizeof(path));
  argv[0] = path;
  argv[1] = strdup(arg);
  argv[2] = NULL;
  rv = fork();
  if (rv == -1) {
    perror("fork");
    abort();
  }
  if (rv == 0) {
    execvp(argv[0], argv);
    perror("execvp");
    abort();
  } else {
    rv = waitpid(rv, &status, 0);
    if (rv == -1) {
      perror("waitpid");
      abort();
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
      fprintf(stderr, "Process for \"%s\" exited with %d (%d)\n", path,
              WEXITSTATUS(status), status);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<run_with_arg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xdfb(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x68(%rbp),%rcx
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    %rcx,%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1269 <run_with_arg+0xb9>
lea    0xda9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
cmpl   $0x0,-0x34(%rbp)
jne    1293 <run_with_arg+0xe3>
mov    -0x50(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   10a0 <execvp@plt>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x34(%rbp),%edi
lea    -0x54(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12c1 <run_with_arg+0x111>
lea    0xd5d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x54(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12e3 <run_with_arg+0x133>
mov    -0x54(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1323 <run_with_arg+0x173>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x54(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x6c(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
mov    -0x54(%rbp),%r8d
lea    0xd0f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
jmp    1336 <run_with_arg+0x186>
jmp    1328 <run_with_arg+0x178>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_with_arg>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2e1b(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r15
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe19(%rip),%rdx
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <snprintf@plt>
cltq
xor    %edi,%edi
cmp    %rbx,%rax
setb   %dil
call   1050 <assert@plt>
mov    %r15,-0x40(%rbp)
mov    %r14,%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     128f <run_with_arg+0xdf>
cmp    $0xffffffff,%eax
je     12a4 <run_with_arg+0xf4>
lea    -0x1c(%rbp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
je     12ad <run_with_arg+0xfd>
mov    -0x1c(%rbp),%edi
call   10b0 <WIFEXITED@plt>
test   %eax,%eax
je     1253 <run_with_arg+0xa3>
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
test   %eax,%eax
je     1282 <run_with_arg+0xd2>
mov    0x2d7e(%rip),%rax
mov    (%rax),%ebx
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x1c(%rbp),%r8d
lea    0xdab(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %eax,%ecx
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1284 <run_with_arg+0xd4>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    -0x40(%rbp),%rsi
mov    %r15,%rdi
call   10a0 <execvp@plt>
lea    0xd69(%rip),%rdi
jmp    12b4 <run_with_arg+0x104>
lea    0xd5b(%rip),%rdi
jmp    12b4 <run_with_arg+0x104>
lea    0xd5e(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"execvp\00"
@.str.3 = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.4 = [38 x i8] c"Process for \22%s\22 exited with %d (%d)\0A\00"
define i32 @run_with_arg(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca [3 x i8*]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = load i32, i32* @MAXPATHLEN
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = trunc i64 %15 to i32
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = call i32 @snprintf(i8* %17, i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, %15
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
store i8* %17, i8** %27
%28 = load i8*, i8** %7
%29 = call i8* @strdup(i8* %28)
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 1
store i8* %29, i8** %30
%31 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 2
store i8* null, i8** %31
%32 = call i32 (...) @fork()
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %3
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %3
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%43 = load i8*, i8** %42
%44 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%45 = call i32 @execvp(i8* %43, i8** %44)
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 (...) @abort()
unreachable
48: ; preds = %38
%49 = load i32, i32* %10
%50 = call i32 @waitpid(i32 %49, i32* %12, i32 0)
store i32 %50, i32* %10
%51 = load i32, i32* %10
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %48
%54 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 (...) @abort()
unreachable
56: ; preds = %48
%57 = load i32, i32* %12
%58 = call i32 @WIFEXITED(i32 %57)
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* %12
%62 = call i32 @WEXITSTATUS(i32 %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %60, %56
%65 = load i32, i32* @stderr
%66 = load i32, i32* %12
%67 = call i32 @WEXITSTATUS(i32 %66)
%68 = load i32, i32* %12
%69 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %17, i32 %67, i32 %68)
store i32 -1, i32* %4
store i32 1, i32* %13
br label %72
70: ; preds = %60
br label %71
71: ; preds = %70
store i32 0, i32* %4
store i32 1, i32* %13
br label %72
72: ; preds = %71, %64
%73 = load i8*, i8** %8
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"execvp\00"
@.str.3 = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.4 = [38 x i8] c"Process for \22%s\22 exited with %d (%d)\0A\00"
define i32 @run_with_arg(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca [3 x i8*]
%5 = alloca i32
%6 = load i32, i32* @MAXPATHLEN
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast [3 x i8*]* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = call i32 @snprintf(i8* %8, i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%12 = sext i32 %11 to i64
%13 = icmp ult i64 %12, %7
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
store i8* %8, i8** %16
%17 = call i8* @strdup(i8* %2)
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 1
store i8* %17, i8** %18
%19 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 2
store i8* null, i8** %19
%20 = call i32 (...) @fork()
switch i32 %20, label %28 [
i32 -1, label %21
i32 0, label %24
]
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %3
%25 = call i32 @execvp(i8* %8, i8** %16)
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 (...) @abort()
unreachable
28: ; preds = %3
%29 = call i32 @waitpid(i32 %20, i32* %5, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %28
%35 = load i32, i32* %5
%36 = call i32 @WIFEXITED(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i32, i32* %5
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %38, %34
%43 = load i32, i32* @stderr
%44 = load i32, i32* %5
%45 = call i32 @WEXITSTATUS(i32 %44)
%46 = load i32, i32* %5
%47 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %8, i32 %45, i32 %46)
br label %48
48: ; preds = %38, %42
%49 = phi i32 [ -1, %42 ], [ 0, %38 ]
ret i32 %49
}