#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*/ execv(char *, char **);
int vfork();
int waitpid(int, int *, int /*<<< orphan*/);
int my_system(char *bin, char **argv) {
  int status, pid;
  pid = vfork();
  if (pid == -1)
    return 1;
  if (pid == 0) {
    execv(bin, argv);
    return 127;
  }
  do {
    if (waitpid(pid, &status, 0) == -1) {
      if (errno != EINTR)
        return 1;
    } else
      return status;
  } while (1);
}


x86 assembly (clang, -O0):
<my_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
call   1040 <vfork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    115e <my_system+0x2e>
movl   $0x1,-0x4(%rbp)
jmp    11e7 <my_system+0xb7>
cmpl   $0x0,-0x20(%rbp)
jne    1181 <my_system+0x51>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <execv@plt>
movl   $0x7f,-0x4(%rbp)
jmp    11e7 <my_system+0xb7>
jmp    1186 <my_system+0x56>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11c8 <my_system+0x98>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
je     11c3 <my_system+0x93>
movl   $0x1,-0x4(%rbp)
jmp    11e7 <my_system+0xb7>
jmp    11d3 <my_system+0xa3>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <my_system+0xb7>
jmp    11d8 <my_system+0xa8>
mov    $0x1,%al
test   $0x1,%al
jne    1186 <my_system+0x56>
jmp    11e7 <my_system+0xb7>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_system>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    $0x1,%r15d
call   1040 <vfork@plt>
cmp    $0xffffffff,%eax
je     119c <my_system+0x6c>
test   %eax,%eax
jne    1167 <my_system+0x37>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <execv@plt>
mov    $0x7f,%eax
jmp    11a5 <my_system+0x75>
mov    %eax,%ebx
lea    0x4(%rsp),%r14
xor    %r13d,%r13d
mov    0x2e68(%rip),%r12
mov    0x2e59(%rip),%rbp
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r13d,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11a1 <my_system+0x71>
mov    (%r12),%rax
cmp    0x0(%rbp),%rax
je     1180 <my_system+0x50>
mov    %r15d,%eax
jmp    11a5 <my_system+0x75>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @my_system(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = call i32 @vfork()
store i32 %8, i32* %7
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %34
12: ; preds = %2
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i8**, i8*** %5
%18 = call i32 @execv(i8* %16, i8** %17)
store i32 127, i32* %3
br label %34
19: ; preds = %12
br label %20
20: ; preds = %33, %19
%21 = load i32, i32* %7
%22 = call i32 @waitpid(i32 %21, i32* %6, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 1, i32* %3
br label %34
29: ; preds = %24
br label %32
30: ; preds = %20
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %34
32: ; preds = %29
br label %33
33: ; preds = %32
br i1 true, label %20, label %34
34: ; preds = %11, %15, %28, %30, %33
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @my_system(i8* %0, i8** %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @vfork()
switch i32 %5, label %8 [
i32 -1, label %17
i32 0, label %6
]
6: ; preds = %2
%7 = call i32 @execv(i8* %0, i8** %1)
br label %17
8: ; preds = %2, %11
%9 = call i32 @waitpid(i32 %5, i32* %3, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %17
15: ; preds = %8
%16 = load i32, i32* %3
br label %17
17: ; preds = %11, %2, %15, %6
%18 = phi i32 [ 127, %6 ], [ %16, %15 ], [ 1, %2 ], [ 1, %11 ]
ret i32 %18
}