#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 /*<<< orphan*/ _exit(int);
scalar_t__ errno;
int execl(char *, char *, char *, char const *, int /*<<< orphan*/ *);
int fork();
int waitpid(int, int *, int /*<<< orphan*/);
int my_system(const char *cmd) {
  int status = -1;
  pid_t pid;
  if (cmd == NULL)
    return -1;
  if ((pid = fork()) == -1)
    status = -1;
  if (pid == 0) {
    if (execl("/bin/sh", "sh", "-c", cmd, NULL) == -1)
      _exit(127);
  } else if (pid > 0) {
    while (waitpid(pid, &status, 0) == -1) {
      if (errno == EINTR)
        continue;
      status = -1;
      break;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<my_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116a <my_system+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <my_system+0xe5>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1184 <my_system+0x44>
movl   $0xffffffff,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11c9 <my_system+0x89>
mov    -0x10(%rbp),%rcx
lea    0xe67(%rip),%rdi
lea    0xe65(%rip),%rsi
lea    0xe61(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <execl@plt>
cmp    $0xffffffff,%eax
jne    11c4 <my_system+0x84>
mov    $0x7f,%edi
call   1030 <_exit@plt>
jmp    121f <my_system+0xdf>
cmpl   $0x0,-0x18(%rbp)
jle    121a <my_system+0xda>
jmp    11d8 <my_system+0x98>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1215 <my_system+0xd5>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
jne    120e <my_system+0xce>
jmp    11d8 <my_system+0x98>
movl   $0xffffffff,-0x14(%rbp)
jmp    121a <my_system+0xda>
jmp    121f <my_system+0xdf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_system>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0xffffffff,0xc(%rsp)
ff
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11f1 <my_system+0xb1>
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11a7 <my_system+0x67>
test   %ebp,%ebp
jne    11ac <my_system+0x6c>
lea    0xe81(%rip),%rdi
lea    0xe7f(%rip),%rsi
lea    0xe7b(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1050 <execl@plt>
cmp    $0xffffffff,%eax
jne    11f1 <my_system+0xb1>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    %ebp,%r14d
jmp    11f1 <my_system+0xb1>
jle    11f1 <my_system+0xb1>
lea    0xc(%rsp),%r15
mov    0x2e26(%rip),%r12
mov    0x2e17(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11ec <my_system+0xac>
mov    (%r12),%rax
cmp    (%rbx),%rax
je     11d0 <my_system+0x90>
jmp    11f1 <my_system+0xb1>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.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 i64 0
@EINTR = global i64 0
define i32 @my_system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %41
9: ; preds = %1
%10 = call i32 (...) @fork()
store i32 %10, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 -1, i32* %4
br label %13
13: ; preds = %12, %9
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = call i32 @execl(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* %17, i32* null)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @_exit(i32 127)
unreachable
22: ; preds = %16
br label %39
23: ; preds = %13
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %23
br label %27
27: ; preds = %35, %26
%28 = load i32, i32* %5
%29 = call i32 @waitpid(i32 %28, i32* %4, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %27
36: ; preds = %31
store i32 -1, i32* %4
br label %37
37: ; preds = %36, %27
br label %38
38: ; preds = %37, %23
br label %39
39: ; preds = %38, %22
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %8
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.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 i64 0
@EINTR = global i64 0
define i32 @my_system(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%4 = icmp eq i8* %0, null
br i1 %4, label %23, label %5
5: ; preds = %1
%6 = tail call i32 (...) @fork()
switch i32 %6, label %12 [
i32 -1, label %23
i32 0, label %7
]
7: ; preds = %5
%8 = tail call i32 @execl(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)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = tail call i32 @_exit(i32 127)
unreachable
12: ; preds = %5
%13 = icmp sgt i32 %6, 0
br i1 %13, label %14, label %23
14: ; preds = %12, %17
%15 = call i32 @waitpid(i32 %6, i32* %2, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %14, label %23
21: ; preds = %14
%22 = load i32, i32* %2
br label %23
23: ; preds = %17, %5, %7, %12, %21, %1
%24 = phi i32 [ -1, %1 ], [ %22, %21 ], [ -1, %12 ], [ -1, %7 ], [ %6, %5 ], [ -1, %17 ]
ret i32 %24
}