#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*/ _exit(int);
int /*<<< orphan*/ execv(char const *, char *const *);
int /*<<< orphan*/ perror(char *);
int vfork();
int /*<<< orphan*/ watchdog_close(int /*<<< orphan*/);
int /*<<< orphan*/ watchdog_open();
int run_bg_process(const char *filename, char *const args[]) {
  watchdog_close(0);
  int pid;
  switch (pid = vfork()) {
  case -1:
    perror("vfork failed\n");
    watchdog_open();
    return -1;
  case 0:
    execv(filename, args);
    perror("vfork: Child failed to exec");
    _exit(-1);
  default:
    watchdog_open();
    return pid;
  }
}


x86 assembly (clang, -O0):
<run_bg_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %edi,%edi
call   1070 <watchdog_close@plt>
call   1060 <vfork@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     11a2 <run_bg_process+0x42>
jmp    1192 <run_bg_process+0x32>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11c1 <run_bg_process+0x61>
jmp    11e4 <run_bg_process+0x84>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
mov    $0x0,%al
call   1040 <watchdog_open@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <run_bg_process+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execv@plt>
lea    0xe39(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   1040 <watchdog_open@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_bg_process>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
call   1070 <watchdog_close@plt>
call   1060 <vfork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     118f <run_bg_process+0x2f>
test   %ebp,%ebp
je     119d <run_bg_process+0x3d>
xor    %eax,%eax
call   1040 <watchdog_open@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
jmp    1181 <run_bg_process+0x21>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <execv@plt>
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"vfork failed\0A\00"
@.str.1 = [28 x i8] c"vfork: Child failed to exec\00"
define i32 @run_bg_process(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%7 = call i32 @watchdog_close(i32 0)
%8 = call i32 @vfork()
store i32 %8, i32* %6
switch i32 %8, label %18 [
i32 -1, label %9
i32 0, label %12
]
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (...) @watchdog_open()
store i32 -1, i32* %3
br label %21
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8**, i8*** %5
%15 = call i32 @execv(i8* %13, i8** %14)
%16 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @_exit(i32 -1)
unreachable
18: ; preds = %2
%19 = call i32 (...) @watchdog_open()
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %21
21: ; preds = %18, %9
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@.str = [14 x i8] c"vfork failed\0A\00"
@.str.1 = [28 x i8] c"vfork: Child failed to exec\00"
define i32 @run_bg_process(i8* %0, i8** %1) {
%3 = call i32 @watchdog_close(i32 0)
%4 = call i32 @vfork()
switch i32 %4, label %11 [
i32 -1, label %5
i32 0, label %7
]
5: ; preds = %2
%6 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %11
7: ; preds = %2
%8 = call i32 @execv(i8* %0, i8** %1)
%9 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @_exit(i32 -1)
unreachable
11: ; preds = %2, %5
%12 = call i32 (...) @watchdog_open()
ret i32 %4
}