#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*/ O_RDWR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
char **malloc(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsid();
int vfork();
void vfork_daemon_rexec(int nochdir, int noclose, int argc, char **argv,
                        char *foreground_opt) {
  int f;
  char **vfork_args;
  int a = 0;
  setsid();
  if (!nochdir)
    chdir("/");
  if (!noclose && (f = open("/dev/null", O_RDWR, 0)) != -1) {
    dup2(f, STDIN_FILENO);
    dup2(f, STDOUT_FILENO);
    dup2(f, STDERR_FILENO);
    if (f > 2)
      close(f);
  }
  vfork_args = malloc(sizeof(char *) * (argc + 2));
  while (*argv) {
    vfork_args[a++] = *argv;
    argv++;
  }
  vfork_args[a++] = foreground_opt;
  vfork_args[a++] = NULL;
  switch (vfork()) {
  case 0:
    if (vfork())
      _exit(0);
    execvp(vfork_args[0], vfork_args);
    perror("execv");
    exit(-1);
  case -1:
    perror("vfork");
    exit(-1);
  default:
    exit(0);
  }
}


x86 assembly (clang, -O0):
<vfork_daemon_rexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x0,%al
call   1070 <setsid@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11ed <vfork_daemon_rexec+0x3d>
lea    0xe18(%rip),%rdi
call   1040 <chdir@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1264 <vfork_daemon_rexec+0xb4>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xdfb(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1264 <vfork_daemon_rexec+0xb4>
mov    -0x24(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
cmpl   $0x2,-0x24(%rbp)
jle    125f <vfork_daemon_rexec+0xaf>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
jmp    1264 <vfork_daemon_rexec+0xb4>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12b7 <vfork_daemon_rexec+0x107>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    127b <vfork_daemon_rexec+0xcb>
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x34(%rbp)
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
movq   $0x0,(%rax,%rcx,8)
call   10b0 <vfork@plt>
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     134a <vfork_daemon_rexec+0x19a>
jmp    12ff <vfork_daemon_rexec+0x14f>
mov    -0x38(%rbp),%eax
test   %eax,%eax
jne    1360 <vfork_daemon_rexec+0x1b0>
jmp    130f <vfork_daemon_rexec+0x15f>
call   10b0 <vfork@plt>
cmp    $0x0,%eax
je     1324 <vfork_daemon_rexec+0x174>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   10c0 <execvp@plt>
lea    0xcd1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xcc1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<vfork_daemon_rexec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%rbx
mov    %edx,%r15d
mov    %esi,%r12d
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <setsid@plt>
test   %ebp,%ebp
jne    11dd <vfork_daemon_rexec+0x2d>
lea    0xe28(%rip),%rdi
call   1040 <chdir@plt>
test   %r12d,%r12d
jne    1240 <vfork_daemon_rexec+0x90>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    0xe10(%rip),%rdi
xor    %edx,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1240 <vfork_daemon_rexec+0x90>
mov    %eax,%ebp
mov    %eax,%r12d
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <dup2@plt>
cmp    $0x3,%ebp
jl     1240 <vfork_daemon_rexec+0x90>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r15d,%eax
lea    0x10(,%rax,8),%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    (%rbx),%rax
xor    %ecx,%ecx
test   %rax,%rax
je     128b <vfork_daemon_rexec+0xdb>
xor    %ecx,%ecx
mov    %rax,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rcx),%rax
mov    %rdx,0x0(%rbp,%rcx,8)
mov    0x8(%rbx,%rcx,8),%rsi
mov    %rax,%rcx
mov    %rsi,%rdx
test   %rsi,%rsi
jne    1270 <vfork_daemon_rexec+0xc0>
mov    %eax,%ecx
mov    %ecx,%eax
mov    %r14,0x0(%rbp,%rax,8)
movq   $0x0,0x8(%rbp,%rax,8)
call   10b0 <vfork@plt>
cmp    $0xffffffff,%eax
je     12c7 <vfork_daemon_rexec+0x117>
test   %eax,%eax
jne    12dd <vfork_daemon_rexec+0x12d>
call   10b0 <vfork@plt>
test   %eax,%eax
jne    12e4 <vfork_daemon_rexec+0x134>
mov    0x0(%rbp),%rdi
mov    %rbp,%rsi
call   10c0 <execvp@plt>
lea    0xd47(%rip),%rdi
jmp    12ce <vfork_daemon_rexec+0x11e>
lea    0xd44(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [6 x i8] c"execv\00"
@.str.3 = [6 x i8] c"vfork\00"
define void @vfork_daemon_rexec(i32 %0, i32 %1, i32 %2, i8** %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8**
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8** %3, i8*** %9
store i8* %4, i8** %10
store i32 0, i32* %13
%14 = call i32 (...) @setsid()
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %5
%18 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %5
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %42, label %22
22: ; preds = %19
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 0)
store i32 %24, i32* %11
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = load i32, i32* %11
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = load i32, i32* %11
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @dup2(i32 %30, i32 %31)
%33 = load i32, i32* %11
%34 = load i32, i32* @STDERR_FILENO
%35 = call i32 @dup2(i32 %33, i32 %34)
%36 = load i32, i32* %11
%37 = icmp sgt i32 %36, 2
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = load i32, i32* %11
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %26
br label %42
42: ; preds = %41, %22, %19
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 2
%45 = sext i32 %44 to i64
%46 = mul i64 8, %45
%47 = trunc i64 %46 to i32
%48 = call i8** @malloc(i32 %47)
store i8** %48, i8*** %12
br label %49
49: ; preds = %53, %42
%50 = load i8**, i8*** %9
%51 = load i8*, i8** %50
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %63
53: ; preds = %49
%54 = load i8**, i8*** %9
%55 = load i8*, i8** %54
%56 = load i8**, i8*** %12
%57 = load i32, i32* %13
%58 = add nsw i32 %57, 1
store i32 %58, i32* %13
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8*, i8** %56, i64 %59
store i8* %55, i8** %60
%61 = load i8**, i8*** %9
%62 = getelementptr inbounds i8*, i8** %61, i32 1
store i8** %62, i8*** %9
br label %49
63: ; preds = %49
%64 = load i8*, i8** %10
%65 = load i8**, i8*** %12
%66 = load i32, i32* %13
%67 = add nsw i32 %66, 1
store i32 %67, i32* %13
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds i8*, i8** %65, i64 %68
store i8* %64, i8** %69
%70 = load i8**, i8*** %12
%71 = load i32, i32* %13
%72 = add nsw i32 %71, 1
store i32 %72, i32* %13
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds i8*, i8** %70, i64 %73
store i8* null, i8** %74
%75 = call i32 @vfork()
switch i32 %75, label %92 [
i32 0, label %76
i32 -1, label %89
]
76: ; preds = %63
%77 = call i32 @vfork()
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 @_exit(i32 0)
unreachable
81: ; preds = %76
%82 = load i8**, i8*** %12
%83 = getelementptr inbounds i8*, i8** %82, i64 0
%84 = load i8*, i8** %83
%85 = load i8**, i8*** %12
%86 = call i32 @execvp(i8* %84, i8** %85)
%87 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%88 = call i32 @exit(i32 -1)
unreachable
89: ; preds = %63
%90 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%91 = call i32 @exit(i32 -1)
unreachable
92: ; preds = %63
%93 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str.2 = [6 x i8] c"execv\00"
@.str.3 = [6 x i8] c"vfork\00"
define void @vfork_daemon_rexec(i32 %0, i32 %1, i32 %2, i8** nocapture %3, i8* %4) {
%6 = call i32 (...) @setsid()
%7 = icmp eq i32 %0, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = icmp eq i32 %1, 0
br i1 %11, label %12, label %26
12: ; preds = %10
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %26, label %16
16: ; preds = %12
%17 = load i32, i32* @STDIN_FILENO
%18 = call i32 @dup2(i32 %14, i32 %17)
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @dup2(i32 %14, i32 %19)
%21 = load i32, i32* @STDERR_FILENO
%22 = call i32 @dup2(i32 %14, i32 %21)
%23 = icmp sgt i32 %14, 2
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @close(i32 %14)
br label %26
26: ; preds = %16, %24, %12, %10
%27 = shl i32 %2, 3
%28 = add i32 %27, 16
%29 = call i8** @malloc(i32 %28)
%30 = load i8*, i8** %3
%31 = icmp eq i8* %30, null
br i1 %31, label %43, label %32
32: ; preds = %26, %32
%33 = phi i64 [ %36, %32 ], [ 0, %26 ]
%34 = phi i8* [ %39, %32 ], [ %30, %26 ]
%35 = phi i8** [ %38, %32 ], [ %3, %26 ]
%36 = add nuw i64 %33, 1
%37 = getelementptr inbounds i8*, i8** %29, i64 %33
store i8* %34, i8** %37
%38 = getelementptr inbounds i8*, i8** %35, i64 1
%39 = load i8*, i8** %38
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %32
41: ; preds = %32
%42 = trunc i64 %36 to i32
br label %43
43: ; preds = %41, %26
%44 = phi i32 [ 0, %26 ], [ %42, %41 ]
%45 = add nuw nsw i32 %44, 1
%46 = zext i32 %44 to i64
%47 = getelementptr inbounds i8*, i8** %29, i64 %46
store i8* %4, i8** %47
%48 = zext i32 %45 to i64
%49 = getelementptr inbounds i8*, i8** %29, i64 %48
store i8* null, i8** %49
%50 = call i32 @vfork()
switch i32 %50, label %64 [
i32 0, label %51
i32 -1, label %61
]
51: ; preds = %43
%52 = call i32 @vfork()
%53 = icmp eq i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = call i32 @_exit(i32 0)
unreachable
56: ; preds = %51
%57 = load i8*, i8** %29
%58 = call i32 @execvp(i8* %57, i8** %29)
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %43
%62 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @exit(i32 -1)
unreachable
64: ; preds = %43
%65 = call i32 @exit(i32 0)
unreachable
}