#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__ ENOENT;
int /*<<< orphan*/ PTRACE_TRACEME;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ add_thread(int, int /*<<< orphan*/ *, int);
scalar_t__ errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ perror_with_name(char *);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int spu_create_inferior(char *program, char **allargs) {
  int pid;
  pid = fork();
  if (pid < 0)
    perror_with_name("fork");
  if (pid == 0) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);
    setpgid(0, 0);
    execv(program, allargs);
    if (errno == ENOENT)
      execvp(program, allargs);
    fprintf(stderr, "Cannot exec %s: %s.\n", program, strerror(errno));
    fflush(stderr);
    _exit(0177);
  }
  add_thread(pid, NULL, pid);
  return pid;
}


x86 assembly (clang, -O0):
<spu_create_inferior>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e0 <spu_create_inferior+0x30>
lea    0xe25(%rip),%rdi
call   1050 <perror_with_name@plt>
cmpl   $0x0,-0x14(%rbp)
jne    128c <spu_create_inferior+0xdc>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1080 <ptrace@plt>
xor    %esi,%esi
mov    %esi,%edi
call   10a0 <setpgid@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <execv@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%rax
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%rax
jne    123b <spu_create_inferior+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <execvp@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xd96(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <add_thread@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spu_create_inferior>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1254 <spu_create_inferior+0xa4>
jne    1260 <spu_create_inferior+0xb0>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <ptrace@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <setpgid@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10c0 <execv@plt>
mov    0x2dd0(%rip),%rbp
mov    0x0(%rbp),%rdi
mov    0x2db5(%rip),%rax
cmp    (%rax),%rdi
jne    121f <spu_create_inferior+0x6f>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <execvp@plt>
mov    0x0(%rbp),%rdi
mov    0x2dba(%rip),%rbp
mov    0x0(%rbp),%ebx
call   10b0 <strerror@plt>
lea    0xdd0(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    0x0(%rbp),%edi
call   1070 <fflush@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
lea    0xda5(%rip),%rdi
call   1050 <perror_with_name@plt>
mov    %ebp,%edi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <add_thread@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@PTRACE_TRACEME = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Cannot exec %s: %s.\0A\00"
define i32 @spu_create_inferior(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
%6 = call i32 (...) @fork()
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror_with_name(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = load i32, i32* @PTRACE_TRACEME
%16 = call i32 @ptrace(i32 %15, i32 0, i32 0, i32 0)
%17 = call i32 @setpgid(i32 0, i32 0)
%18 = load i8*, i8** %3
%19 = load i8**, i8*** %4
%20 = call i32 @execv(i8* %18, i8** %19)
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = load i8*, i8** %3
%26 = load i8**, i8*** %4
%27 = call i32 @execvp(i8* %25, i8** %26)
br label %28
28: ; preds = %24, %14
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %3
%31 = load i64, i64* @errno
%32 = call i8* @strerror(i64 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %32)
%34 = load i32, i32* @stderr
%35 = call i32 @fflush(i32 %34)
%36 = call i32 @_exit(i32 127)
unreachable
37: ; preds = %11
%38 = load i32, i32* %5
%39 = load i32, i32* %5
%40 = call i32 @add_thread(i32 %38, i32* null, i32 %39)
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@PTRACE_TRACEME = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [21 x i8] c"Cannot exec %s: %s.\0A\00"
define i32 @spu_create_inferior(i8* %0, i8** %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror_with_name(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %28
7: ; preds = %2
%8 = icmp eq i32 %3, 0
br i1 %8, label %9, label %28
9: ; preds = %7
%10 = load i32, i32* @PTRACE_TRACEME
%11 = tail call i32 @ptrace(i32 %10, i32 0, i32 0, i32 0)
%12 = tail call i32 @setpgid(i32 0, i32 0)
%13 = tail call i32 @execv(i8* %0, i8** %1)
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = tail call i32 @execvp(i8* %0, i8** %1)
%19 = load i64, i64* @errno
br label %20
20: ; preds = %17, %9
%21 = phi i64 [ %19, %17 ], [ %14, %9 ]
%22 = load i32, i32* @stderr
%23 = tail call i8* @strerror(i64 %21)
%24 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %23)
%25 = load i32, i32* @stderr
%26 = tail call i32 @fflush(i32 %25)
%27 = tail call i32 @_exit(i32 127)
unreachable
28: ; preds = %5, %7
%29 = tail call i32 @add_thread(i32 %3, i32* null, i32 %3)
ret i32 %3
}