#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;
scalar_t__ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int run_cmd(char *argv[]) {
  int status = -1;
  int ret = -1;
  pid_t child = 0;
  if ((child = fork()) == 0) {
    execvp(argv[0], argv);
    exit(1);
  } else if (child < 0) {
    return -1;
  }
  do {
    ret = waitpid(child, &status, 0);
  } while (ret == -1 && errno == EINTR);
  return status;
}


x86 assembly (clang, -O0):
<run_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1191 <run_cmd+0x51>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <execvp@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jge    11a8 <run_cmd+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <run_cmd+0xbe>
jmp    11ad <run_cmd+0x6d>
jmp    11b2 <run_cmd+0x72>
mov    -0x20(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jne    11ed <run_cmd+0xad>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11b2 <run_cmd+0x72>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_cmd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
movl   $0xffffffff,0x4(%rsp)
ff
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     11b2 <run_cmd+0x72>
js     11a1 <run_cmd+0x61>
mov    %rax,%rbx
lea    0x4(%rsp),%r14
mov    0x2e70(%rip),%r15
mov    0x2e61(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    119b <run_cmd+0x5b>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1180 <run_cmd+0x40>
mov    0x4(%rsp),%eax
jmp    11a6 <run_cmd+0x66>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r14),%rdi
mov    %r14,%rsi
call   1040 <execvp@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @run_cmd(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i8** %0, i8*** %3
store i32 -1, i32* %4
store i32 -1, i32* %5
store i64 0, i64* %6
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i8**, i8*** %3
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = load i8**, i8*** %3
%14 = call i32 @execvp(i8* %12, i8** %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i64, i64* %6
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -1, i32* %2
br label %36
20: ; preds = %16
br label %21
21: ; preds = %20
br label %22
22: ; preds = %32, %21
%23 = load i64, i64* %6
%24 = call i32 @waitpid(i64 %23, i32* %4, i32 0)
store i32 %24, i32* %5
br label %25
25: ; preds = %22
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %22, label %34
34: ; preds = %32
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %19
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @run_cmd(i8** %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = tail call i32 @execvp(i8* %7, i8** %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = icmp slt i64 %4, 0
br i1 %11, label %21, label %12
12: ; preds = %10, %12
%13 = call i32 @waitpid(i64 %4, i32* %2, i32 0)
%14 = icmp eq i32 %13, -1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %12, label %19
19: ; preds = %12
%20 = load i32, i32* %2
br label %21
21: ; preds = %10, %19
%22 = phi i32 [ %20, %19 ], [ -1, %10 ]
ret i32 %22
}