#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 pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ cmd_error(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int call_proc(char **argv, char *errmsg) {
  pid_t pid;
  int status;
  pid = fork();
  switch (pid) {
  case 0:
    execvp(argv[0], argv);
    cmd_error("%s: %s : %s\n", errmsg, argv[0], strerror(errno));
    break;
  case -1:
    cmd_error("%s: %s : %s\n", errmsg, argv[0], strerror(errno));
    break;
  }
WAIT:
  if (waitpid(pid, &status, 0) < 0) {
    if (errno == EINTR)
      goto WAIT;
    cmd_error("unexpected response from waitpid : %s\n", strerror(errno));
  }
  if (!WIFEXITED(status)) {
    cmd_error(NULL);
  }
  return WEXITSTATUS(status);
}


x86 assembly (clang, -O0):
<call_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11fd <call_proc+0x8d>
jmp    119e <call_proc+0x2e>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
jne    1237 <call_proc+0xc7>
jmp    11ae <call_proc+0x3e>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <execvp@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1060 <cmd_error@plt>
jmp    1237 <call_proc+0xc7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1060 <cmd_error@plt>
jmp    123c <call_proc+0xcc>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jge    1292 <call_proc+0x122>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <call_proc+0x103>
jmp    123c <call_proc+0xcc>
mov    0x2d66(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1060 <cmd_error@plt>
mov    -0x18(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    12ae <call_proc+0x13e>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x0,%al
call   1060 <cmd_error@plt>
mov    -0x18(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<call_proc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     119b <call_proc+0x2b>
test   %ebx,%ebx
jne    11c3 <call_proc+0x53>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1050 <execvp@plt>
mov    (%r15),%r15
mov    0x2e3b(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xe4c(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <cmd_error@plt>
lea    0x4(%rsp),%r14
mov    0x2e11(%rip),%r15
mov    0x2e02(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
jns    120f <call_proc+0x9f>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     11e0 <call_proc+0x70>
call   1080 <strerror@plt>
lea    0xe07(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <cmd_error@plt>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
jne    1225 <call_proc+0xb5>
xor    %edi,%edi
xor    %eax,%eax
call   1060 <cmd_error@plt>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%s: %s : %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [39 x i8] c"unexpected response from waitpid : %s\0A\00"
define i32 @call_proc(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i8* %1, i8** %4
%7 = call i32 (...) @fork()
store i32 %7, i32* %5
%8 = load i32, i32* %5
switch i32 %8, label %30 [
i32 0, label %9
i32 -1, label %22
]
9: ; preds = %2
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = load i8**, i8*** %3
%14 = call i32 @execvp(i8* %12, i8** %13)
%15 = load i8*, i8** %4
%16 = load i8**, i8*** %3
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = load i64, i64* @errno
%20 = call i32 @strerror(i64 %19)
%21 = call i32 (i8*, ...) @cmd_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %18, i32 %20)
br label %30
22: ; preds = %2
%23 = load i8*, i8** %4
%24 = load i8**, i8*** %3
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = load i64, i64* @errno
%28 = call i32 @strerror(i64 %27)
%29 = call i32 (i8*, ...) @cmd_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %26, i32 %28)
br label %30
30: ; preds = %2, %22, %9
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %5
%33 = call i64 @waitpid(i32 %32, i32* %6, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %31
40: ; preds = %35
%41 = load i64, i64* @errno
%42 = call i32 @strerror(i64 %41)
%43 = call i32 (i8*, ...) @cmd_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %40, %31
%45 = load i32, i32* %6
%46 = call i32 @WIFEXITED(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = call i32 (i8*, ...) @cmd_error(i8* null)
br label %50
50: ; preds = %48, %44
%51 = load i32, i32* %6
%52 = call i32 @WEXITSTATUS(i32 %51)
ret i32 %52
}

LLVM IR (-O2):
@.str = [13 x i8] c"%s: %s : %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [39 x i8] c"unexpected response from waitpid : %s\0A\00"
define i32 @call_proc(i8** %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %14 [
i32 0, label %6
i32 -1, label %9
]
6: ; preds = %2
%7 = load i8*, i8** %0
%8 = tail call i32 @execvp(i8* %7, i8** %0)
br label %9
9: ; preds = %2, %6
%10 = load i8*, i8** %0
%11 = load i64, i64* @errno
%12 = tail call i32 @strerror(i64 %11)
%13 = tail call i32 (i8*, ...) @cmd_error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %10, i32 %12)
br label %14
14: ; preds = %9, %2
br label %15
15: ; preds = %14, %18
%16 = call i64 @waitpid(i32 %5, i32* %3, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %15, label %22
22: ; preds = %18
%23 = call i32 @strerror(i64 %19)
%24 = call i32 (i8*, ...) @cmd_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %15, %22
%26 = load i32, i32* %3
%27 = call i32 @WIFEXITED(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @cmd_error(i8* null)
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %3
%33 = call i32 @WEXITSTATUS(i32 %32)
ret i32 %33
}