#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*/ execlp(char *, char *, char *, char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int _system(char *pszCmd) {
  pid_t pid;
  int status;
  if (pszCmd == NULL)
    return 1;
  if ((pid = fork()) < 0) {
    perror("fork error.");
    return -1;
  } else if (pid == 0) {
    execlp("/bin/sh", "sh", "-c", pszCmd, (char *)0);
    perror("execlp error");
    _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    1173 <_system+0x23>
movl   $0x1,-0x4(%rbp)
jmp    123d <_system+0xed>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11a0 <_system+0x50>
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <_system+0xed>
cmpq   $0x0,-0x18(%rbp)
jne    11e4 <_system+0x94>
mov    -0x10(%rbp),%rcx
lea    0xe56(%rip),%rdi
lea    0xe54(%rip),%rsi
lea    0xe50(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <execlp@plt>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
jmp    11e9 <_system+0x99>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jge    122d <_system+0xdd>
mov    0x2dd7(%rip),%rax
mov    (%rax),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <_system+0xd8>
movl   $0xffffffff,-0x1c(%rbp)
jmp    122d <_system+0xdd>
jmp    11e9 <_system+0x99>
jmp    1232 <_system+0xe2>
jmp    1237 <_system+0xe7>
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     11b2 <_system+0x62>
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     11c9 <_system+0x79>
je     11dc <_system+0x8c>
mov    %rax,%rbx
lea    0x4(%rsp),%r14
mov    0x2e63(%rip),%r15
mov    0x2e54(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
jns    11b9 <_system+0x69>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1190 <_system+0x40>
mov    $0xffffffff,%eax
jmp    11bd <_system+0x6d>
mov    $0x1,%eax
jmp    11bd <_system+0x6d>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe30(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11bd <_system+0x6d>
lea    0xe29(%rip),%rdi
lea    0xe27(%rip),%rsi
lea    0xe23(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1060 <execlp@plt>
lea    0xe14(%rip),%rdi
call   1050 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

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

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