#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*/ sigset_t;
typedef int pid_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SIG_SETMASK;
scalar_t__ errno;
int /*<<< orphan*/ execl(char const *, char const *, char const *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ const *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int waitpid(int, int *, int /*<<< orphan*/);
void do_exec(const char *path, const char *arg, const sigset_t *mask) {
  pid_t pid;
  int status;
  pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(1);
  }
  if (pid == 0) {
    sigprocmask(SIG_SETMASK, mask, NULL);
    execl(path, path, arg, NULL);
    fprintf(stderr, "FAILED: execl failed with %s\n", strerror(errno));
  } else {
    int ret;
    do
      ret = waitpid(pid, &status, 0);
    while (ret == -1 && errno == EINTR);
    if (ret != pid) {
      perror("waitpid");
      exit(1);
    }
    if (status != 0) {
      fprintf(stderr, "child exec failed\n");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<do_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11be <do_exec+0x3e>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1227 <do_exec+0xa7>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <execl@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12c3 <do_exec+0x143>
jmp    122c <do_exec+0xac>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x29(%rbp)
jne    1266 <do_exec+0xe6>
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    122c <do_exec+0xac>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1293 <do_exec+0x113>
lea    0xd9f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
je     12be <do_exec+0x13e>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    12c3 <do_exec+0x143>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_exec>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11f5 <do_exec+0x75>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     124e <do_exec+0xce>
lea    0x4(%rsp),%r14
mov    0x2e1d(%rip),%r15
mov    0x2e0e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11da <do_exec+0x5a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     11c0 <do_exec+0x40>
cmp    %ebx,%eax
jne    1257 <do_exec+0xd7>
cmpl   $0x0,0x4(%rsp)
jne    126d <do_exec+0xed>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %r15,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1090 <execl@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%ebx
mov    0x2da8(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdce(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fprintf@plt>
lea    0xdab(%rip),%rdi
jmp    125e <do_exec+0xde>
lea    0xdc5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@SIG_SETMASK = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"FAILED: execl failed with %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [8 x i8] c"waitpid\00"
@.str.3 = [19 x i8] c"child exec failed\0A\00"
define void @do_exec(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%10 = call i32 (...) @fork()
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = load i32, i32* @SIG_SETMASK
%21 = load i32*, i32** %6
%22 = call i32 @sigprocmask(i32 %20, i32* %21, i32* null)
%23 = load i8*, i8** %4
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = call i32 @execl(i8* %23, i8* %24, i8* %25, i32* null)
%27 = load i32, i32* @stderr
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %59
31: ; preds = %16
br label %32
32: ; preds = %42, %31
%33 = load i32, i32* %7
%34 = call i32 @waitpid(i32 %33, i32* %8, i32 0)
store i32 %34, i32* %9
br label %35
35: ; preds = %32
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i64, i64* @errno
%40 = load i64, i64* @EINTR
%41 = icmp eq i64 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %32, label %44
44: ; preds = %42
%45 = load i32, i32* %9
%46 = load i32, i32* %7
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %44
%52 = load i32, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %51
br label %59
59: ; preds = %58, %19
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@SIG_SETMASK = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"FAILED: execl failed with %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [8 x i8] c"waitpid\00"
@.str.3 = [19 x i8] c"child exec failed\0A\00"
define void @do_exec(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @fork()
switch i32 %6, label %18 [
i32 -1, label %7
i32 0, label %10
]
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = load i32, i32* @SIG_SETMASK
%12 = tail call i32 @sigprocmask(i32 %11, i32* %2, i32* null)
%13 = tail call i32 @execl(i8* %0, i8* %0, i8* %1, i32* null)
%14 = load i32, i32* @stderr
%15 = load i64, i64* @errno
%16 = tail call i8* @strerror(i64 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %37
18: ; preds = %3, %18
%19 = call i32 @waitpid(i32 %6, i32* %4, i32 0)
%20 = icmp eq i32 %19, -1
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %18, label %25
25: ; preds = %18
%26 = icmp eq i32 %19, %6
br i1 %26, label %30, label %27
27: ; preds = %25
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %25
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30, %10
ret void
}