#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 EINTR;
int /*<<< orphan*/ _exit(int);
int errno;
int /*<<< orphan*/ execl(char *, char *, char *, char const *, char *);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int my_system(const char *cmdstring) {
  pid_t pid;
  int status;
  if (cmdstring == NULL)
    return 1;
  if ((pid = fork()) < 0) {
    status = -1;
  } else if (pid == 0) {
    printf("before calling shell: %s\n", cmdstring);
    execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
    _exit(127);
  } else {
    while (waitpid(pid, &status, 0) < 0) {
      if (errno != EINTR) {
        printf("wait cmd failed, errno = %d\n", errno);
        status = -1;
        break;
      }
    }
    printf("wait cmd, status = %d\n", status);
  }
  return (status);
}


x86 assembly (clang, -O0):
<my_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <my_system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1258 <my_system+0x108>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1194 <my_system+0x44>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1252 <my_system+0x102>
cmpq   $0x0,-0x18(%rbp)
jne    11de <my_system+0x8e>
mov    -0x10(%rbp),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rcx
lea    0xe5e(%rip),%rdi
lea    0xe5c(%rip),%rsi
lea    0xe58(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
jmp    11e3 <my_system+0x93>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    123c <my_system+0xec>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%eax
je     1237 <my_system+0xe7>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    123c <my_system+0xec>
jmp    11e3 <my_system+0x93>
mov    -0x1c(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1252 <my_system+0x102>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_system>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11c8 <my_system+0x78>
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11cf <my_system+0x7f>
je     11f8 <my_system+0xa8>
mov    %rax,%rbx
lea    0x4(%rsp),%r14
mov    0x2e5f(%rip),%r15
mov    0x2e50(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jns    11d6 <my_system+0x86>
mov    (%r15),%esi
cmp    (%r12),%esi
je     1190 <my_system+0x40>
lea    0xe73(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0xffffffff,0x4(%rsp)
ff
mov    $0xffffffff,%esi
jmp    11da <my_system+0x8a>
mov    $0x1,%eax
jmp    11ec <my_system+0x9c>
mov    $0xffffffff,%eax
jmp    11ec <my_system+0x9c>
mov    0x4(%rsp),%esi
lea    0xe61(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe01(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe0a(%rip),%rdi
lea    0xe08(%rip),%rsi
lea    0xe04(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1060 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"before calling shell: %s\0A\00"
@.str.1 = [8 x i8] c"/bin/sh\00"
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [3 x i8] c"-c\00"
@errno = global i32 0
@EINTR = global i32 0
@.str.4 = [29 x i8] c"wait cmd failed, errno = %d\0A\00"
@.str.5 = [23 x i8] c"wait cmd, status = %d\0A\00"
define i32 @my_system(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
%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 i64 (...) @fork()
store i64 %10, i64* %4
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 -1, i32* %5
br label %39
13: ; preds = %9
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %3
%20 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %19, i8* null)
%21 = call i32 @_exit(i32 127)
unreachable
22: ; preds = %13
br label %23
23: ; preds = %34, %22
%24 = load i64, i64* %4
%25 = call i64 @waitpid(i64 %24, i32* %5, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = load i32, i32* @EINTR
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @errno
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %32)
store i32 -1, i32* %5
br label %35
34: ; preds = %27
br label %23
35: ; preds = %31, %23
%36 = load i32, i32* %5
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %35
br label %39
39: ; preds = %38, %12
%40 = load i32, i32* %5
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %8
%42 = load i32, i32* %2
ret i32 %42
}

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