#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 */

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char const *,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int waitpid(int, int *, int /*<<< orphan*/);
int mysh6_system(const char *cmd) {
  int pid, status;
  pid = fork();
  switch (pid) {
  case -1:
    return EXIT_FAILURE;
    break;
  case 0:
    if (execlp("/bin/sh", "sh", "-c", cmd, NULL)) {
      exit(127);
    }
  default:
    while (waitpid(pid, &status, 0) == -1) {
      if (errno != EINTR) {
        status = -1;
      }
    }
    break;
  }
  return status;
}


x86 assembly (clang, -O0):
<mysh6_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     117a <mysh6_system+0x3a>
jmp    116a <mysh6_system+0x2a>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     118b <mysh6_system+0x4b>
jmp    11c6 <mysh6_system+0x86>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1211 <mysh6_system+0xd1>
mov    -0x10(%rbp),%rcx
lea    0xe6a(%rip),%rdi
lea    0xe68(%rip),%rsi
lea    0xe64(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <execlp@plt>
cmp    $0x0,%eax
je     11c1 <mysh6_system+0x81>
mov    $0x7f,%edi
call   1040 <exit@plt>
jmp    11c6 <mysh6_system+0x86>
jmp    11cb <mysh6_system+0x8b>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1206 <mysh6_system+0xc6>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%eax
je     1201 <mysh6_system+0xc1>
movl   $0xffffffff,-0x18(%rbp)
jmp    11cb <mysh6_system+0x8b>
jmp    120b <mysh6_system+0xcb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysh6_system>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
cmp    $0xffffffff,%eax
je     11d3 <mysh6_system+0x93>
mov    %eax,%ebp
test   %eax,%eax
jne    1180 <mysh6_system+0x40>
lea    0xe9d(%rip),%rdi
lea    0xe9b(%rip),%rsi
lea    0xe97(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1050 <execlp@plt>
test   %eax,%eax
jne    11e7 <mysh6_system+0xa7>
lea    0x4(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11da <mysh6_system+0x9a>
mov    0x2e43(%rip),%r14
mov    0x2e2c(%rip),%r15
lea    0x4(%rsp),%rbx
jmp    11c1 <mysh6_system+0x81>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11da <mysh6_system+0x9a>
mov    (%r14),%eax
cmp    (%r15),%eax
je     11b0 <mysh6_system+0x70>
movl   $0xffffffff,0x4(%rsp)
ff
jmp    11b0 <mysh6_system+0x70>
mov    0x2dfe(%rip),%rbx
mov    (%rbx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x7f,%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@errno = global i32 0
@EINTR = global i32 0
define i32 @mysh6_system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %4
%7 = load i32, i32* %4
switch i32 %7, label %17 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %1
%9 = load i32, i32* @EXIT_FAILURE
store i32 %9, i32* %2
br label %31
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %11, i32* null)
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @exit(i32 127)
unreachable
16: ; preds = %10
br label %17
17: ; preds = %1, %16
br label %18
18: ; preds = %27, %17
%19 = load i32, i32* %4
%20 = call i32 @waitpid(i32 %19, i32* %5, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINTR
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %5
br label %27
27: ; preds = %26, %22
br label %18
28: ; preds = %18
br label %29
29: ; preds = %28
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %8
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
@errno = global i32 0
@EINTR = global i32 0
define i32 @mysh6_system(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %10 [
i32 -1, label %21
i32 0, label %5
]
5: ; preds = %1
%6 = tail call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32* null)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @exit(i32 127)
unreachable
10: ; preds = %5, %1
%11 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %21
13: ; preds = %10, %18
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINTR
%16 = icmp eq i32 %14, %15
br i1 %16, label %18, label %17
17: ; preds = %13
store i32 -1, i32* %2
br label %18
18: ; preds = %17, %13
%19 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %13, label %21
21: ; preds = %18, %10, %1
%22 = phi i32* [ @EXIT_FAILURE, %1 ], [ %2, %10 ], [ %2, %18 ]
%23 = load i32, i32* %22
ret i32 %23
}