#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*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int execute(char **comm, int how) {
  int pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "minish : fork error\n");
    return (-1);
  } else if (pid == 0) {
    execvp(*comm, comm);
    fprintf(stderr, "minish : command not found\n");
    exit(127);
  }
  if (how == 1) {
    printf("[%d]\n", pid);
    return 0;
  }
  while (waitpid(pid, NULL, 0) < 0)
    if (errno != EINTR)
      return -1;
  return 0;
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11a3 <execute+0x43>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <execute+0xf5>
cmpl   $0x0,-0x18(%rbp)
jne    11dc <execute+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <execvp@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>
jmp    11e1 <execute+0x81>
cmpl   $0x1,-0x14(%rbp)
jne    1206 <execute+0xa6>
mov    -0x18(%rbp),%esi
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <execute+0xf5>
jmp    120b <execute+0xab>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    124e <execute+0xee>
mov    0x2dae(%rip),%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
je     1249 <execute+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <execute+0xf5>
jmp    120b <execute+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
js     1192 <execute+0x32>
je     11ec <execute+0x8c>
mov    %eax,%ebp
cmp    $0x1,%ebx
jne    11a9 <execute+0x49>
lea    0xeaa(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
xor    %ebx,%ebx
jmp    11df <execute+0x7f>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11da <execute+0x7a>
mov    0x2e28(%rip),%r14
mov    0x2e19(%rip),%r15
nopw   0x0(%rax,%rax,1)
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jns    11df <execute+0x7f>
mov    (%r14),%rax
cmp    (%r15),%rax
je     11c0 <execute+0x60>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x7f,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"minish : fork error\0A\00"
@.str.1 = [28 x i8] c"minish : command not found\0A\00"
@.str.2 = [6 x i8] c"[%d]\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @execute(i8** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %42
12: ; preds = %2
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i8**, i8*** %4
%17 = load i8*, i8** %16
%18 = load i8**, i8*** %4
%19 = call i32 @execvp(i8* %17, i8** %18)
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 127)
unreachable
23: ; preds = %12
br label %24
24: ; preds = %23
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %6
%29 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 0, i32* %3
br label %42
30: ; preds = %24
br label %31
31: ; preds = %40, %30
%32 = load i32, i32* %6
%33 = call i64 @waitpid(i32 %32, i32* null, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %3
br label %42
40: ; preds = %35
br label %31
41: ; preds = %31
store i32 0, i32* %3
br label %42
42: ; preds = %41, %39, %27, %9
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"minish : fork error\0A\00"
@.str.1 = [28 x i8] c"minish : command not found\0A\00"
@.str.2 = [6 x i8] c"[%d]\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @execute(i8** %0, i32 %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %27
8: ; preds = %2
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %16
10: ; preds = %8
%11 = load i8*, i8** %0
%12 = tail call i32 @execvp(i8* %11, i8** %0)
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 127)
unreachable
16: ; preds = %8
%17 = icmp eq i32 %1, 1
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %3)
br label %27
20: ; preds = %16, %23
%21 = tail call i64 @waitpid(i32 %3, i32* null, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %20, label %27
27: ; preds = %20, %23, %18, %5
%28 = phi i32 [ -1, %5 ], [ 0, %18 ], [ 0, %20 ], [ -1, %23 ]
ret i32 %28
}