#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 /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
int do_system(char *command, int i_debug) {
  int pid = 0, status = 0;
  extern char **environ;
  if (command == 0)
    return 1;
  pid = fork();
  if (pid == -1)
    return -1;
  setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
  if (pid == 0) {
    char *argv[4];
    argv[0] = "sh";
    argv[1] = "-c";
    argv[2] = command;
    argv[3] = 0;
    if (i_debug)
      printf("%s\r\n", command);
    execve("/bin/sh", argv, environ);
    exit(127);
  }
  do {
    if (waitpid(pid, &status, 0) == -1) {
      if (errno != EINTR)
        return -1;
    } else
      return status;
  } while (1);
  return status;
}


x86 assembly (clang, -O0):
<do_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <do_system+0x34>
movl   $0x1,-0x4(%rbp)
jmp    12a6 <do_system+0x146>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b4 <do_system+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <do_system+0x146>
lea    0xe45(%rip),%rdi
lea    0xe43(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
cmpl   $0x0,-0x18(%rbp)
jne    123a <do_system+0xda>
lea    0xe53(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe3b(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1216 <do_system+0xb6>
mov    -0x10(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
lea    -0x40(%rbp),%rsi
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdx
lea    0xe00(%rip),%rdi
call   1050 <execve@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
jmp    123f <do_system+0xdf>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1281 <do_system+0x121>
mov    0x2d7b(%rip),%rax
mov    (%rax),%rax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%rax
je     127c <do_system+0x11c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a6 <do_system+0x146>
jmp    128c <do_system+0x12c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a6 <do_system+0x146>
jmp    1291 <do_system+0x131>
mov    $0x1,%al
test   $0x1,%al
jne    123f <do_system+0xdf>
jmp    12a0 <do_system+0x140>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_system>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0x0,0xc(%rsp)
test   %rdi,%rdi
je     11ec <do_system+0x8c>
mov    %esi,%r15d
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11f9 <do_system+0x99>
mov    %eax,%ebp
lea    0xe66(%rip),%rdi
lea    0xe64(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
test   %ebp,%ebp
je     1209 <do_system+0xa9>
lea    0xc(%rsp),%r15
mov    0x2e1d(%rip),%r12
mov    0x2e0e(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11f4 <do_system+0x94>
mov    (%r12),%rax
cmp    (%rbx),%rax
je     11d0 <do_system+0x70>
jmp    11f9 <do_system+0x99>
mov    $0x1,%r14d
jmp    11f9 <do_system+0x99>
mov    0xc(%rsp),%r14d
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe07(%rip),%rax
mov    %rax,0x18(%rsp)
mov    %rbx,0x20(%rsp)
movq   $0x0,0x28(%rsp)
test   %r15d,%r15d
je     1243 <do_system+0xe3>
lea    0xdeb(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%rdx
lea    0xdd7(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <execve@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [30 x i8] c"/sbin:/bin:/usr/sbin:/usr/bin\00"
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [5 x i8] c"%s\0D\0A\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@environ = external global i8**
@errno = global i64 0
@EINTR = global i64 0
define i32 @do_system(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4 x i8*]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%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 %54
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 %54
17: ; preds = %12
%18 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 1)
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %17
%22 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %22
%23 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %23
%24 = load i8*, i8** %4
%25 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 2
store i8* %24, i8** %25
%26 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 3
store i8* null, i8** %26
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load i8*, i8** %4
%31 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %21
%33 = getelementptr inbounds [4 x i8*], [4 x i8*]* %8, i64 0, i64 0
%34 = load i8**, i8*** @environ
%35 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %33, i8** %34)
%36 = call i32 @exit(i32 127)
unreachable
37: ; preds = %17
br label %38
38: ; preds = %51, %37
%39 = load i32, i32* %6
%40 = call i32 @waitpid(i32 %39, i32* %7, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %3
br label %54
47: ; preds = %42
br label %50
48: ; preds = %38
%49 = load i32, i32* %7
store i32 %49, i32* %3
br label %54
50: ; preds = %47
br label %51
51: ; preds = %50
br i1 true, label %38, label %52
52: ; preds = %51
%53 = load i32, i32* %7
store i32 %53, i32* %3
br label %54
54: ; preds = %52, %48, %46, %16, %11
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [30 x i8] c"/sbin:/bin:/usr/sbin:/usr/bin\00"
@.str.2 = [3 x i8] c"sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [5 x i8] c"%s\0D\0A\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@environ = external global i8**
@errno = global i64 0
@EINTR = global i64 0
define i32 @do_system(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [4 x i8*]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = icmp eq i8* %0, null
br i1 %6, label %35, label %7
7: ; preds = %2
%8 = tail call i32 (...) @fork()
%9 = icmp eq i32 %8, -1
br i1 %9, label %35, label %10
10: ; preds = %7
%11 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 1)
%12 = icmp eq i32 %8, 0
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = bitcast [4 x i8*]* %4 to i8*
%15 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %16
%17 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 2
store i8* %0, i8** %17
%18 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 3
store i8* null, i8** %18
%19 = icmp eq i32 %1, 0
br i1 %19, label %22, label %20
20: ; preds = %13
%21 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %20, %13
%23 = load i8**, i8*** @environ
%24 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %15, i8** %23)
%25 = call i32 @exit(i32 127)
unreachable
26: ; preds = %10, %29
%27 = call i32 @waitpid(i32 %8, i32* %3, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %26, label %35
33: ; preds = %26
%34 = load i32, i32* %3
br label %35
35: ; preds = %29, %7, %2, %33
%36 = phi i32 [ %34, %33 ], [ 1, %2 ], [ -1, %7 ], [ -1, %29 ]
ret i32 %36
}