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


x86 assembly (clang, -O0):
<mysystem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <mysystem+0x23>
movl   $0x1,-0x4(%rbp)
jmp    11e7 <mysystem+0xa7>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1185 <mysystem+0x45>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11c2 <mysystem+0x82>
cmpq   $0x0,-0x18(%rbp)
jne    11bd <mysystem+0x7d>
mov    -0x10(%rbp),%rcx
lea    0xe65(%rip),%rdi
lea    0xe63(%rip),%rsi
lea    0xe5f(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
jmp    11c2 <mysystem+0x82>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11e1 <mysystem+0xa1>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysystem>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1185 <mysystem+0x45>
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     118c <mysystem+0x4c>
jne    1194 <mysystem+0x54>
lea    0xe9e(%rip),%rdi
lea    0xe9c(%rip),%rsi
lea    0xe98(%rip),%rdx
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1050 <execl@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    $0x1,%eax
jmp    11ac <mysystem+0x6c>
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11ac <mysystem+0x6c>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

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"
define i32 @mysystem(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 %29
9: ; preds = %1
%10 = call i64 (...) @fork()
store i64 %10, i64* %4
%11 = load i64, i64* %4
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 -1, i32* %5
br label %22
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, 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* %18, i32* null)
%20 = call i32 @_exit(i32 127)
unreachable
21: ; preds = %14
br label %22
22: ; preds = %21, %13
%23 = load i64, i64* %4
%24 = call i32 @waitpid(i64 %23, i32* %5, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %5
br label %27
27: ; preds = %26, %22
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %8
%30 = load i32, i32* %2
ret i32 %30
}

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