#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 */
int /*<<< orphan*/ PT_TRACE_ME;
int /*<<< orphan*/ _exit(int);
void *add_process(int);
int /*<<< orphan*/ add_thread(int, void *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
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(int /*<<< orphan*/);
int vfork();
int fbsd_create_inferior(char *program, char **allargs) {
  void *new_process;
  int pid;
  pid = vfork();
  if (pid < 0)
    perror_with_name("vfork");
  if (pid == 0) {
    ptrace(PT_TRACE_ME, 0, 0, 0);
    setpgid(0, 0);
    execv(program, allargs);
    fprintf(stderr, "Cannot exec %s: %s.\n", program, strerror(errno));
    fflush(stderr);
    _exit(0177);
  }
  new_process = add_process(pid);
  add_thread(pid, new_process);
  return pid;
}


x86 assembly (clang, -O0):
<fbsd_create_inferior>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
call   10a0 <vfork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11de <fbsd_create_inferior+0x2e>
lea    0xe27(%rip),%rdi
call   1060 <perror_with_name@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1262 <fbsd_create_inferior+0xb2>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1090 <ptrace@plt>
xor    %esi,%esi
mov    %esi,%edi
call   10b0 <setpgid@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <execv@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc1(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <add_process@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <add_thread@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fbsd_create_inferior>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   10a0 <vfork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     122f <fbsd_create_inferior+0x7f>
jne    123b <fbsd_create_inferior+0x8b>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
xor    %ebp,%ebp
mov    %ebp,%esi
mov    %ebp,%edx
mov    %ebp,%ecx
call   1090 <ptrace@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   10b0 <setpgid@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10d0 <execv@plt>
mov    0x2de8(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdf6(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    0x0(%rbp),%edi
call   1080 <fflush@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
lea    0xdca(%rip),%rdi
call   1060 <perror_with_name@plt>
mov    %ebp,%edi
call   1040 <add_process@plt>
mov    %ebp,%edi
mov    %rax,%rsi
call   1050 <add_thread@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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