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


x86 assembly (clang, -O0):
<my_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118f <my_system+0x4f>
mov    -0x8(%rbp),%rcx
lea    0xe93(%rip),%rsi
lea    0xe94(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1050 <execl@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
jge    11a6 <my_system+0x66>
movl   $0xffffffff,-0xc(%rbp)
jmp    11cb <my_system+0x8b>
mov    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     11c6 <my_system+0x86>
movl   $0xffffffff,-0xc(%rbp)
jmp    11cb <my_system+0x8b>
jmp    11d0 <my_system+0x90>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_system>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     1187 <my_system+0x47>
js     117a <my_system+0x3a>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmp    %rbx,%rcx
jne    117f <my_system+0x3f>
mov    0x4(%rsp),%eax
jmp    117f <my_system+0x3f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe72(%rip),%rdi
lea    0xe73(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   1050 <execl@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@EXIT_FAILURE = global i32 0
define i32 @my_system(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
store i8* %0, i8** %2
%5 = call i64 (...) @fork()
store i64 %5, i64* %4
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), 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* %9, i32* null)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @_exit(i32 %11)
unreachable
13: ; preds = %1
%14 = load i64, i64* %4
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %3
br label %24
17: ; preds = %13
%18 = load i64, i64* %4
%19 = call i64 @waitpid(i64 %18, i32* %3, i32 0)
%20 = load i64, i64* %4
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %23
23: ; preds = %22, %17
br label %24
24: ; preds = %23, %16
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
@EXIT_FAILURE = global i32 0
define i32 @my_system(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), 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* %0, i32* null)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @_exit(i32 %8)
unreachable
10: ; preds = %1
%11 = icmp slt i64 %4, 0
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = call i64 @waitpid(i64 %4, i32* %2, i32 0)
%14 = icmp eq i64 %13, %4
%15 = load i32, i32* %2
%16 = select i1 %14, i32 %15, i32 -1
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ -1, %10 ], [ %16, %12 ]
ret i32 %18
}