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


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1215 <system+0xd5>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1184 <system+0x44>
movl   $0xffffffff,-0x1c(%rbp)
jmp    120f <system+0xcf>
cmpq   $0x0,-0x18(%rbp)
jne    11bc <system+0x7c>
mov    -0x10(%rbp),%rcx
lea    0xe66(%rip),%rdi
lea    0xe64(%rip),%rsi
lea    0xe60(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
jmp    11c1 <system+0x81>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jge    1205 <system+0xc5>
mov    0x2dff(%rip),%rax
mov    (%rax),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
je     1200 <system+0xc0>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1205 <system+0xc5>
jmp    11c1 <system+0x81>
jmp    120a <system+0xca>
jmp    120f <system+0xcf>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     119e <system+0x5e>
mov    %rdi,%rbp
xor    %eax,%eax
call   1060 <fork@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     11ab <system+0x6b>
je     11bb <system+0x7b>
mov    %rax,%rbx
lea    0xc(%rsp),%r15
mov    0x2e69(%rip),%r12
mov    0x2e5a(%rip),%rbp
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
jns    11a6 <system+0x66>
mov    (%r12),%rax
cmp    0x0(%rbp),%rax
je     1180 <system+0x40>
jmp    11ab <system+0x6b>
mov    $0x1,%r14d
jmp    11ab <system+0x6b>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
lea    0xe3c(%rip),%rsi
lea    0xe38(%rip),%rdx
mov    %rbp,%rcx
xor    %r8d,%r8d
call   1050 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

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 @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 %35
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 %33
13: ; preds = %9
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
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, i8* null)
%19 = call i32 @_exit(i32 127)
unreachable
20: ; preds = %13
br label %21
21: ; preds = %30, %20
%22 = load i64, i64* %4
%23 = call i64 @waitpid(i64 %22, i32* %5, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %5
br label %31
30: ; preds = %25
br label %21
31: ; preds = %29, %21
br label %32
32: ; preds = %31
br label %33
33: ; preds = %32, %12
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %8
%36 = load i32, i32* %2
ret i32 %36
}

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