#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 WEXITSTATUS(int);
int /*<<< orphan*/ die(char const *);
int /*<<< orphan*/ execvp(char const *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int *);
void sys(const char *cmd, char **args) {
  if (!cmd)
    cmd = args[0];
  pid_t child_pid = fork();
  if (child_pid < 0)
    die("fork");
  if (child_pid == 0) {
    execvp(cmd, (char **)args);
    die(cmd);
  }
  int status;
  wait(&status);
  if (WEXITSTATUS(status) != 0) {
    fprintf(stderr, "%s exited with error %d.\n", cmd, WEXITSTATUS(status));
    exit(WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<sys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1196 <sys+0x26>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11b8 <sys+0x48>
lea    0xe4d(%rip),%rdi
call   1050 <die@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <sys+0x69>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
mov    -0x8(%rbp),%rdi
call   1050 <die@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1233 <sys+0xc3>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xde6(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%edi
call   1070 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rdi,%rdi
jne    1182 <sys+0x12>
mov    (%rbx),%r14
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11a0 <sys+0x30>
jne    11ac <sys+0x3c>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <execvp@plt>
mov    %r14,%rdi
jmp    11a7 <sys+0x37>
lea    0xe59(%rip),%rdi
call   1050 <die@plt>
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %eax,%eax
jne    11cb <sys+0x5b>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e0e(%rip),%rax
mov    (%rax),%ebx
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe21(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
call   1030 <fprintf@plt>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"%s exited with error %d.\0A\00"
define void @sys(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i8**, i8*** %4
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
store i8* %12, i8** %3
br label %13
13: ; preds = %9, %2
%14 = call i64 (...) @fork()
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i8*, i8** %3
%24 = load i8**, i8*** %4
%25 = call i32 @execvp(i8* %23, i8** %24)
%26 = load i8*, i8** %3
%27 = call i32 @die(i8* %26)
br label %28
28: ; preds = %22, %19
%29 = call i32 @wait(i32* %6)
%30 = load i32, i32* %6
%31 = call i32 @WEXITSTATUS(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %3
%36 = load i32, i32* %6
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %35, i32 %37)
%39 = load i32, i32* %6
%40 = call i32 @WEXITSTATUS(i32 %39)
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %28
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"%s exited with error %d.\0A\00"
define void @sys(i8* %0, i8** %1) {
%3 = alloca i32
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i8*, i8** %1
br label %7
7: ; preds = %5, %2
%8 = phi i8* [ %0, %2 ], [ %6, %5 ]
%9 = tail call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = icmp eq i64 %9, 0
br i1 %12, label %13, label %18
13: ; preds = %11
%14 = tail call i32 @execvp(i8* %8, i8** %1)
br label %15
15: ; preds = %7, %13
%16 = phi i8* [ %8, %13 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %7 ]
%17 = tail call i32 @die(i8* %16)
br label %18
18: ; preds = %15, %11
%19 = bitcast i32* %3 to i8*
%20 = call i32 @wait(i32* %3)
%21 = load i32, i32* %3
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i32, i32* %3
%27 = call i32 @WEXITSTATUS(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 %27)
%29 = load i32, i32* %3
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %18
ret void
}