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


x86 assembly (clang, -O0):
<system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1225 <system+0xd5>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1199 <system+0x49>
lea    0xe71(%rip),%rdi
call   1040 <err_sys@plt>
jmp    121f <system+0xcf>
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <system+0x81>
mov    -0x10(%rbp),%rcx
lea    0xe61(%rip),%rdi
lea    0xe5f(%rip),%rsi
lea    0xe5b(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
jmp    11d6 <system+0x86>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    1215 <system+0xc5>
mov    0x2dea(%rip),%rax
mov    (%rax),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
je     1210 <system+0xc0>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11d6 <system+0x86>
jmp    121a <system+0xca>
jmp    121f <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   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11c5 <system+0x75>
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11cc <system+0x7c>
je     11e8 <system+0x98>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jns    11d8 <system+0x88>
mov    0x2e54(%rip),%r15
mov    0x2e45(%rip),%r12
lea    0x4(%rsp),%r14
jmp    11b2 <system+0x62>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jns    11d8 <system+0x88>
mov    (%r15),%rax
cmp    (%r12),%rax
je     11a0 <system+0x50>
movl   $0xffffffff,0x4(%rsp)
ff
jmp    11a0 <system+0x50>
mov    $0x1,%eax
jmp    11dc <system+0x8c>
lea    0xe2d(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe21(%rip),%rdi
lea    0xe1f(%rip),%rsi
lea    0xe1b(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1060 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"error on fork()\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 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 %36
9: ; preds = %1
%10 = call i64 (...) @fork()
store i64 %10, i64* %4
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %34
14: ; preds = %9
%15 = load i64, i64* %4
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = 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* %18, i32* null)
%20 = call i32 @_exit(i32 127)
unreachable
21: ; preds = %14
br label %22
22: ; preds = %31, %21
%23 = load i64, i64* %4
%24 = call i64 @waitpid(i64 %23, i32* %5, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 -1, i32* %5
br label %31
31: ; preds = %30, %26
br label %22
32: ; preds = %22
br label %33
33: ; preds = %32
br label %34
34: ; preds = %33, %12
%35 = load i32, i32* %5
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %8
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str = [16 x i8] c"error on fork()\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 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 %28, label %5
5: ; preds = %1
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %26
10: ; preds = %5
%11 = icmp eq i64 %6, 0
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = call i64 @waitpid(i64 %6, i32* %2, i32 0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %18, label %26
15: ; preds = %10
%16 = 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, i32* null)
%17 = tail call i32 @_exit(i32 127)
unreachable
18: ; preds = %12, %23
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %23, label %22
22: ; preds = %18
store i32 -1, i32* %2
br label %23
23: ; preds = %22, %18
%24 = call i64 @waitpid(i64 %6, i32* %2, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %18, label %26
26: ; preds = %23, %12, %8
%27 = load i32, i32* %2
br label %28
28: ; preds = %1, %26
%29 = phi i32 [ %27, %26 ], [ 1, %1 ]
ret i32 %29
}