#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ errno;
int execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
void mrsh_exec(char **args) {
  pid_t pid, wpid;
  int status;
  pid = fork();
  if (pid == 0) {
    if (execvp(*args, args) == -1) {
      do {
        fprintf(stderr, "mrsh: ");
        fprintf(stderr, "couldn't execute %s: %s", *args, strerror(errno));
        fprintf(stderr, "\n");
      } while (0);
    }
  } else if (pid > 0) {
    int status;
    do {
      wpid = waitpid(pid, &status, WUNTRACED);
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  } else {
    do {
      do {
        fprintf(stderr, "mrsh: ");
        fprintf(stderr, "fork(): %s", strerror(errno));
        fprintf(stderr, "\n");
      } while (0);
      exit(EXIT_FAILURE);
    } while (0);
  }
}


x86 assembly (clang, -O0):
<mrsh_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1235 <mrsh_exec+0xb5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1230 <mrsh_exec+0xb0>
jmp    11c0 <mrsh_exec+0x40>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1230 <mrsh_exec+0xb0>
jmp    1313 <mrsh_exec+0x193>
cmpq   $0x0,-0x10(%rbp)
jle    129a <mrsh_exec+0x11a>
jmp    1245 <mrsh_exec+0xc5>
mov    -0x10(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <WIFEXITED@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x2d(%rbp)
jne    128a <mrsh_exec+0x10a>
mov    -0x20(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1245 <mrsh_exec+0xc5>
jmp    130e <mrsh_exec+0x18e>
jmp    129f <mrsh_exec+0x11f>
jmp    12a4 <mrsh_exec+0x124>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd26(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1313 <mrsh_exec+0x193>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mrsh_exec>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11da <mrsh_exec+0x5a>
jle    1250 <mrsh_exec+0xd0>
mov    %rax,%rbx
mov    0x2e38(%rip),%r15
lea    0xc(%rsp),%r14
nopl   (%rax)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
jne    11ea <mrsh_exec+0x6a>
mov    0xc(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %eax,%eax
je     11b0 <mrsh_exec+0x30>
jmp    11ea <mrsh_exec+0x6a>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
je     11f4 <mrsh_exec+0x74>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2ddd(%rip),%r15
mov    (%r15),%edi
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%ebx
mov    (%r14),%r14
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde0(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%edi
lea    0xddf(%rip),%rsi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fprintf@plt>
mov    0x2d81(%rip),%r14
mov    (%r14),%edi
lea    0xd9f(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%ebx
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xda1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r14),%edi
lea    0xd89(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [7 x i8] c"mrsh: \00"
@.str.1 = [24 x i8] c"couldn't execute %s: %s\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@WUNTRACED = global i32 0
@.str.3 = [11 x i8] c"fork(): %s\00"
@EXIT_FAILURE = global i32 0
define void @mrsh_exec(i8** %0) {
%2 = alloca i8**
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %2
%7 = call i64 (...) @fork()
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %1
%11 = load i8**, i8*** %2
%12 = load i8*, i8** %11
%13 = load i8**, i8*** %2
%14 = call i32 @execvp(i8* %12, i8** %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %29
16: ; preds = %10
br label %17
17: ; preds = %16
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %2
%22 = load i8*, i8** %21
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %24)
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %17
br label %29
29: ; preds = %28, %10
br label %66
30: ; preds = %1
%31 = load i64, i64* %3
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %30
br label %34
34: ; preds = %47, %33
%35 = load i64, i64* %3
%36 = load i32, i32* @WUNTRACED
%37 = call i64 @waitpid(i64 %35, i32* %6, i32 %36)
store i64 %37, i64* %4
br label %38
38: ; preds = %34
%39 = load i32, i32* %6
%40 = call i32 @WIFEXITED(i32 %39)
%41 = icmp ne i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %38
%43 = load i32, i32* %6
%44 = call i32 @WIFSIGNALED(i32 %43)
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
br label %47
47: ; preds = %42, %38
%48 = phi i1 [ false, %38 ], [ %46, %42 ]
br i1 %48, label %34, label %49
49: ; preds = %47
br label %65
50: ; preds = %30
br label %51
51: ; preds = %50
br label %52
52: ; preds = %51
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %61
61: ; preds = %52
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; No predecessors!
br label %65
65: ; preds = %64, %49
br label %66
66: ; preds = %65, %29
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [7 x i8] c"mrsh: \00"
@.str.1 = [24 x i8] c"couldn't execute %s: %s\00"
@errno = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@WUNTRACED = global i32 0
@.str.3 = [11 x i8] c"fork(): %s\00"
@EXIT_FAILURE = global i32 0
define void @mrsh_exec(i8** %0) {
%2 = alloca i32
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %19
5: ; preds = %1
%6 = load i8*, i8** %0
%7 = tail call i32 @execvp(i8* %6, i8** %0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %45
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %0
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %15)
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %45
19: ; preds = %1
%20 = icmp sgt i64 %3, 0
br i1 %20, label %21, label %34
21: ; preds = %19
%22 = bitcast i32* %2 to i8*
br label %23
23: ; preds = %29, %21
%24 = load i32, i32* @WUNTRACED
%25 = call i64 @waitpid(i64 %3, i32* %2, i32 %24)
%26 = load i32, i32* %2
%27 = call i32 @WIFEXITED(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* %2
%31 = call i32 @WIFSIGNALED(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %23, label %33
33: ; preds = %23, %29
br label %45
34: ; preds = %19
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = tail call i8* @strerror(i32 %38)
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %5, %9, %33
ret void
}