#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int);
int fork();
int waitpid(int, int *, int /*<<< orphan*/);
int my_system(const char *command, char **envp) {
  int pid, status;
  if (command == 0)
    return 1;
  pid = fork();
  if (pid == -1)
    return -1;
  if (pid == 0) {
    char *argv[4];
    argv[0] = "sh";
    argv[1] = "-c";
    argv[2] = command;
    argv[3] = 0;
    execve("/bin/sh", argv, envp);
    exit(127);
  }
  do {
    if (waitpid(pid, &status, 0) == -1) {
      if (errno != EINTR)
        return -1;
    } else
      return status;
  } while (1);
}


x86 assembly (clang, -O0):
<my_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <my_system+0x27>
movl   $0x1,-0x4(%rbp)
jmp    123b <my_system+0xfb>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1187 <my_system+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <my_system+0xfb>
cmpl   $0x0,-0x1c(%rbp)
jne    11d5 <my_system+0x95>
lea    0xe70(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe5d(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe3d(%rip),%rdi
call   1030 <execve@plt>
mov    $0x7f,%edi
call   1050 <exit@plt>
jmp    11da <my_system+0x9a>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    121c <my_system+0xdc>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <my_system+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <my_system+0xfb>
jmp    1227 <my_system+0xe7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123b <my_system+0xfb>
jmp    122c <my_system+0xec>
mov    $0x1,%al
test   $0x1,%al
jne    11da <my_system+0x9a>
jmp    123b <my_system+0xfb>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_system>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     11a1 <my_system+0x61>
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ab <my_system+0x6b>
test   %ebx,%ebx
je     11b9 <my_system+0x79>
mov    %rsp,%r14
mov    0x2e6e(%rip),%r15
mov    0x2e5f(%rip),%r12
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11a8 <my_system+0x68>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1180 <my_system+0x40>
mov    $0xffffffff,%ebx
jmp    11ab <my_system+0x6b>
mov    $0x1,%ebx
jmp    11ab <my_system+0x6b>
mov    (%rsp),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe48(%rip),%rax
mov    %rax,(%rsp)
lea    0xe35(%rip),%rax
mov    %rax,0x8(%rsp)
mov    %r15,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xe1e(%rip),%rdi
mov    %rsp,%rsi
mov    %r14,%rdx
call   1030 <execve@plt>
mov    $0x7f,%edi
call   1050 <exit@plt>

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

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