#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 */
struct rlimit {
  int rlim_cur;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ RLIMIT_NOFILE;
char **calloc(int const, int);
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getopt(int const, char **, char *);
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int open(char *, int /*<<< orphan*/);
char *optarg;
int const optind;
int optopt;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
int main(const int argc, char **argv) {
  char *logfile = "bgproc.log";
  bool verbose = false;
  int i, c;
  while ((c = getopt(argc, argv, "hvl:")) != -1) {
    switch (c) {
    case 'l':
      logfile = optarg;
      break;
    case 'v':
      verbose = true;
      break;
    case 'h':
    case '?':
      printf("unkown %c\n", optopt);
      usage();
      break;
    }
  }
  if (optind >= argc) {
    usage();
  }
  char *cmd = argv[optind];
  char **args = calloc(argc - optind + 1, sizeof(char *));
  for (i = optind; i < argc; i++) {
    args[i - optind] = argv[i];
  }
  if (verbose) {
    fprintf(stderr, "command: %s\n", cmd);
    fprintf(stderr, "narg: %d\n", argc - optind);
    for (i = 0; i < argc - optind; i++)
      fprintf(stderr, "arg %d: %s\n", i, args[i]);
  }
  int fdout = creat(logfile, 0644);
  if (fdout < 0) {
    perror("runbg: unable to create log file");
    exit(3);
  }
  pid_t pid;
  struct rlimit lim;
  getrlimit(RLIMIT_NOFILE, &lim);
  for (i = 3; i < lim.rlim_cur; i++) {
    close(i);
  }
  pid = fork();
  if (pid == -1) {
    return -1;
  } else if (pid != 0) {
    close(fdout);
    exit(0);
  }
  if (setsid() == -1) {
    return -1;
  }
  pid = fork();
  if (pid == -1) {
    return -1;
  } else if (pid != 0) {
    exit(0);
  }
  int fd = open("/dev/null", O_RDONLY);
  fdout = creat(logfile, 0644);
  dup2(fd, 0);
  dup2(fdout, 1);
  dup2(fdout, 2);
  execvp(cmd, args);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xdf3(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xde5(%rip),%rdx
call   10c0 <getopt@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     12be <main+0xce>
mov    -0x24(%rbp),%eax
mov    %eax,-0x50(%rbp)
sub    $0x3f,%eax
je     129d <main+0xad>
jmp    124b <main+0x5b>
mov    -0x50(%rbp),%eax
sub    $0x68,%eax
je     129d <main+0xad>
jmp    125c <main+0x6c>
mov    -0x50(%rbp),%eax
sub    $0x6c,%eax
je     127e <main+0x8e>
jmp    126d <main+0x7d>
mov    -0x50(%rbp),%eax
sub    $0x76,%eax
je     1291 <main+0xa1>
jmp    12b9 <main+0xc9>
mov    0x2d73(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    12b9 <main+0xc9>
movl   $0x1,-0x1c(%rbp)
jmp    12b9 <main+0xc9>
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
lea    0xd63(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <usage@plt>
jmp    1218 <main+0x28>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
cmp    -0x8(%rbp),%eax
jl     12d7 <main+0xe7>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x2ce6(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cd1(%rip),%rax
sub    (%rax),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    0x2cb7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1353 <main+0x163>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    0x2c8c(%rip),%rsi
sub    (%rsi),%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1316 <main+0x126>
cmpl   $0x0,-0x1c(%rbp)
je     13f2 <main+0x202>
mov    0x2c54(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xcaa(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2c39(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    0x2c3d(%rip),%rax
sub    (%rax),%edx
lea    0xc94(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x8(%rbp),%ecx
mov    0x2c19(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jge    13ed <main+0x1fd>
mov    0x2bf8(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
mov    -0x38(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xc5a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    13a2 <main+0x1b2>
jmp    13f2 <main+0x202>
mov    -0x18(%rbp),%rdi
mov    $0x1a4,%esi
call   10e0 <creat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1423 <main+0x233>
lea    0xc2a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10f0 <exit@plt>
mov    0x2bae(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
call   1100 <getrlimit@plt>
movl   $0x3,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    145e <main+0x26e>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    143c <main+0x24c>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    147e <main+0x28e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1548 <main+0x358>
cmpl   $0x0,-0x40(%rbp)
je     1497 <main+0x2a7>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
jmp    149c <main+0x2ac>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    14b8 <main+0x2c8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1548 <main+0x358>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    14d8 <main+0x2e8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1548 <main+0x358>
cmpl   $0x0,-0x40(%rbp)
je     14e9 <main+0x2f9>
xor    %edi,%edi
call   10f0 <exit@plt>
jmp    14ee <main+0x2fe>
mov    0x2aeb(%rip),%rax
mov    (%rax),%esi
lea    0xb61(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1a4,%esi
call   10e0 <creat@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x4c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x3c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x3c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10d0 <execvp@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r13d
lea    0xde5(%rip),%rax
mov    %rax,0x10(%rsp)
mov    $0x1,%bpl
lea    0xde1(%rip),%r14
mov    0x2d9f(%rip),%r15
lea    0xdd8(%rip),%r12
jmp    1257 <main+0x57>
nopw   0x0(%rax,%rax,1)
cmp    $0x3f,%eax
jne    128a <main+0x8a>
mov    (%r15),%esi
mov    %r12,%rdi
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10d0 <getopt@plt>
cmp    $0x67,%eax
jle    1240 <main+0x40>
cmp    $0x68,%eax
je     1245 <main+0x45>
cmp    $0x76,%eax
je     1291 <main+0x91>
cmp    $0x6c,%eax
jne    1257 <main+0x57>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
jmp    1257 <main+0x57>
cmp    $0xffffffff,%eax
jne    1257 <main+0x57>
jmp    1295 <main+0x95>
xor    %ebp,%ebp
jmp    1257 <main+0x57>
test   %r13d,%r13d
jg     12a1 <main+0xa1>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    (%rbx),%r14
lea    0x1(%r13),%edi
mov    $0x8,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
test   %r13d,%r13d
jle    12cc <main+0xcc>
mov    %r13d,%edx
shl    $0x3,%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <memcpy@plt>
test   $0x1,%bpl
jne    133c <main+0x13c>
mov    0x2ce7(%rip),%r15
mov    (%r15),%edi
lea    0xd38(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r15),%edi
lea    0xd31(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
test   %r13d,%r13d
jle    133c <main+0x13c>
mov    %r13d,%ebx
lea    0xd22(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%edi
mov    (%r12,%rbp,8),%rcx
mov    %r13,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1320 <main+0x120>
mov    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x1a4,%esi
call   10f0 <creat@plt>
test   %eax,%eax
js     143a <main+0x23a>
mov    %eax,%ebx
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1110 <getrlimit@plt>
cmpl   $0x4,0x8(%rsp)
jl     1390 <main+0x190>
mov    $0x3,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%ebp
cmp    0x8(%rsp),%ebp
jl     1380 <main+0x180>
xor    %eax,%eax
call   1120 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1429 <main+0x229>
test   %ebp,%ebp
jne    1450 <main+0x250>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
je     1424 <main+0x224>
xor    %eax,%eax
call   1120 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1429 <main+0x229>
test   %ebp,%ebp
jne    1457 <main+0x257>
mov    0x2c0d(%rip),%rax
mov    (%rax),%esi
lea    0xc83(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%r13d
mov    %r15,%rdi
mov    $0x1a4,%esi
call   10f0 <creat@plt>
mov    %eax,%ebx
xor    %ebp,%ebp
mov    %r13d,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   10e0 <execvp@plt>
jmp    1429 <main+0x229>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbfd(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x3,%edi
call   1100 <exit@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@.str = [11 x i8] c"bgproc.log\00"
@.str.1 = [5 x i8] c"hvl:\00"
@optarg = global i8* null
@.str.2 = [11 x i8] c"unkown %c\0A\00"
@optopt = global i32 0
@optind = constant i32 0
@stderr = global i32 0
@.str.3 = [13 x i8] c"command: %s\0A\00"
@.str.4 = [10 x i8] c"narg: %d\0A\00"
@.str.5 = [12 x i8] c"arg %d: %s\0A\00"
@.str.6 = [33 x i8] c"runbg: unable to create log file\00"
@RLIMIT_NOFILE = global i32 0
@.str.7 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8**
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.rlimit
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 0, i32* %7
br label %16
16: ; preds = %30, %2
%17 = load i32, i32* %4
%18 = load i8**, i8*** %5
%19 = call i32 @getopt(i32 %17, i8** %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %19, i32* %9
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %31
21: ; preds = %16
%22 = load i32, i32* %9
switch i32 %22, label %30 [
i32 108, label %23
i32 118, label %25
i32 104, label %26
i32 63, label %26
]
23: ; preds = %21
%24 = load i8*, i8** @optarg
store i8* %24, i8** %6
br label %30
25: ; preds = %21
store i32 1, i32* %7
br label %30
26: ; preds = %21, %21
%27 = load i32, i32* @optopt
%28 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @usage()
br label %30
30: ; preds = %21, %26, %25, %23
br label %16
31: ; preds = %16
%32 = load i32, i32* @optind
%33 = load i32, i32* %4
%34 = icmp sge i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 (...) @usage()
br label %37
37: ; preds = %35, %31
%38 = load i8**, i8*** %5
%39 = load i32, i32* @optind
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
%42 = load i8*, i8** %41
store i8* %42, i8** %10
%43 = load i32, i32* %4
%44 = load i32, i32* @optind
%45 = sub nsw i32 %43, %44
%46 = add nsw i32 %45, 1
%47 = call i8** @calloc(i32 %46, i32 8)
store i8** %47, i8*** %11
%48 = load i32, i32* @optind
store i32 %48, i32* %8
br label %49
49: ; preds = %65, %37
%50 = load i32, i32* %8
%51 = load i32, i32* %4
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %68
53: ; preds = %49
%54 = load i8**, i8*** %5
%55 = load i32, i32* %8
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %54, i64 %56
%58 = load i8*, i8** %57
%59 = load i8**, i8*** %11
%60 = load i32, i32* %8
%61 = load i32, i32* @optind
%62 = sub nsw i32 %60, %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %59, i64 %63
store i8* %58, i8** %64
br label %65
65: ; preds = %53
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %49
68: ; preds = %49
%69 = load i32, i32* %7
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %99
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %10
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %73)
%75 = load i32, i32* @stderr
%76 = load i32, i32* %4
%77 = load i32, i32* @optind
%78 = sub nsw i32 %76, %77
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %78)
store i32 0, i32* %8
br label %80
80: ; preds = %95, %71
%81 = load i32, i32* %8
%82 = load i32, i32* %4
%83 = load i32, i32* @optind
%84 = sub nsw i32 %82, %83
%85 = icmp slt i32 %81, %84
br i1 %85, label %86, label %98
86: ; preds = %80
%87 = load i32, i32* @stderr
%88 = load i32, i32* %8
%89 = load i8**, i8*** %11
%90 = load i32, i32* %8
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8*, i8** %89, i64 %91
%93 = load i8*, i8** %92
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %88, i8* %93)
br label %95
95: ; preds = %86
%96 = load i32, i32* %8
%97 = add nsw i32 %96, 1
store i32 %97, i32* %8
br label %80
98: ; preds = %80
br label %99
99: ; preds = %98, %68
%100 = load i8*, i8** %6
%101 = call i32 @creat(i8* %100, i32 420)
store i32 %101, i32* %12
%102 = load i32, i32* %12
%103 = icmp slt i32 %102, 0
br i1 %103, label %104, label %107
104: ; preds = %99
%105 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%106 = call i32 @exit(i32 3)
unreachable
107: ; preds = %99
%108 = load i32, i32* @RLIMIT_NOFILE
%109 = call i32 @getrlimit(i32 %108, %struct.rlimit* %14)
store i32 3, i32* %8
br label %110
110: ; preds = %118, %107
%111 = load i32, i32* %8
%112 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %14, i32 0, i32 0
%113 = load i32, i32* %112
%114 = icmp slt i32 %111, %113
br i1 %114, label %115, label %121
115: ; preds = %110
%116 = load i32, i32* %8
%117 = call i32 @close(i32 %116)
br label %118
118: ; preds = %115
%119 = load i32, i32* %8
%120 = add nsw i32 %119, 1
store i32 %120, i32* %8
br label %110
121: ; preds = %110
%122 = call i32 (...) @fork()
store i32 %122, i32* %13
%123 = load i32, i32* %13
%124 = icmp eq i32 %123, -1
br i1 %124, label %125, label %126
125: ; preds = %121
store i32 -1, i32* %3
br label %163
126: ; preds = %121
%127 = load i32, i32* %13
%128 = icmp ne i32 %127, 0
br i1 %128, label %129, label %133
129: ; preds = %126
%130 = load i32, i32* %12
%131 = call i32 @close(i32 %130)
%132 = call i32 @exit(i32 0)
unreachable
133: ; preds = %126
br label %134
134: ; preds = %133
%135 = call i32 (...) @setsid()
%136 = icmp eq i32 %135, -1
br i1 %136, label %137, label %138
137: ; preds = %134
store i32 -1, i32* %3
br label %163
138: ; preds = %134
%139 = call i32 (...) @fork()
store i32 %139, i32* %13
%140 = load i32, i32* %13
%141 = icmp eq i32 %140, -1
br i1 %141, label %142, label %143
142: ; preds = %138
store i32 -1, i32* %3
br label %163
143: ; preds = %138
%144 = load i32, i32* %13
%145 = icmp ne i32 %144, 0
br i1 %145, label %146, label %148
146: ; preds = %143
%147 = call i32 @exit(i32 0)
unreachable
148: ; preds = %143
br label %149
149: ; preds = %148
%150 = load i32, i32* @O_RDONLY
%151 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %150)
store i32 %151, i32* %15
%152 = load i8*, i8** %6
%153 = call i32 @creat(i8* %152, i32 420)
store i32 %153, i32* %12
%154 = load i32, i32* %15
%155 = call i32 @dup2(i32 %154, i32 0)
%156 = load i32, i32* %12
%157 = call i32 @dup2(i32 %156, i32 1)
%158 = load i32, i32* %12
%159 = call i32 @dup2(i32 %158, i32 2)
%160 = load i8*, i8** %10
%161 = load i8**, i8*** %11
%162 = call i32 @execvp(i8* %160, i8** %161)
br label %163
163: ; preds = %149, %142, %137, %125
%164 = load i32, i32* %3
ret i32 %164
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@.str = [11 x i8] c"bgproc.log\00"
@.str.1 = [5 x i8] c"hvl:\00"
@optarg = global i8* null
@.str.2 = [11 x i8] c"unkown %c\0A\00"
@optopt = global i32 0
@optind = constant i32 0
@stderr = global i32 0
@.str.3 = [13 x i8] c"command: %s\0A\00"
@.str.4 = [10 x i8] c"narg: %d\0A\00"
@.str.5 = [12 x i8] c"arg %d: %s\0A\00"
@.str.6 = [33 x i8] c"runbg: unable to create log file\00"
@RLIMIT_NOFILE = global i32 0
@.str.7 = [10 x i8] c"/dev/null\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = bitcast i8** %1 to i8*
%4 = alloca %struct.rlimit
br label %5
5: ; preds = %10, %2
%6 = phi i1 [ true, %2 ], [ false, %10 ]
%7 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %2 ], [ %9, %10 ]
br label %8
8: ; preds = %5, %13
%9 = phi i8* [ %7, %5 ], [ %14, %13 ]
br label %10
10: ; preds = %12, %8
%11 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
switch i32 %11, label %12 [
i32 -1, label %19
i32 108, label %13
i32 118, label %5
i32 104, label %15
i32 63, label %15
]
12: ; preds = %10, %15
br label %10
13: ; preds = %10
%14 = load i8*, i8** @optarg
br label %8
15: ; preds = %10, %10
%16 = load i32, i32* @optopt
%17 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = tail call i32 (...) @usage()
br label %12
19: ; preds = %10
%20 = icmp slt i32 %0, 1
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = tail call i32 (...) @usage()
br label %23
23: ; preds = %21, %19
%24 = load i8*, i8** %1
%25 = add nsw i32 %0, 1
%26 = tail call i8** @calloc(i32 %25, i32 8)
%27 = icmp sgt i32 %0, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = bitcast i8** %26 to i8*
%30 = zext i32 %0 to i64
%31 = shl nuw nsw i64 %30, 3
br label %32
32: ; preds = %28, %23
br i1 %6, label %49, label %33
33: ; preds = %32
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br i1 %27, label %38, label %49
38: ; preds = %33
%39 = zext i32 %0 to i64
br label %40
40: ; preds = %38, %40
%41 = phi i64 [ 0, %38 ], [ %47, %40 ]
%42 = load i32, i32* @stderr
%43 = getelementptr inbounds i8*, i8** %26, i64 %41
%44 = load i8*, i8** %43
%45 = trunc i64 %41 to i32
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %45, i8* %44)
%47 = add nuw nsw i64 %41, 1
%48 = icmp eq i64 %47, %39
br i1 %48, label %49, label %40
49: ; preds = %40, %33, %32
%50 = tail call i32 @creat(i8* %9, i32 420)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%54 = tail call i32 @exit(i32 3)
unreachable
55: ; preds = %49
%56 = bitcast %struct.rlimit* %4 to i8*
%57 = load i32, i32* @RLIMIT_NOFILE
%58 = call i32 @getrlimit(i32 %57, %struct.rlimit* %4)
%59 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
%60 = load i32, i32* %59
%61 = icmp sgt i32 %60, 3
br i1 %61, label %62, label %68
62: ; preds = %55, %62
%63 = phi i32 [ %65, %62 ], [ 3, %55 ]
%64 = call i32 @close(i32 %63)
%65 = add nuw nsw i32 %63, 1
%66 = load i32, i32* %59
%67 = icmp slt i32 %65, %66
br i1 %67, label %62, label %68
68: ; preds = %62, %55
%69 = call i32 (...) @fork()
switch i32 %69, label %70 [
i32 -1, label %88
i32 0, label %73
]
70: ; preds = %68
%71 = call i32 @close(i32 %50)
%72 = call i32 @exit(i32 0)
unreachable
73: ; preds = %68
%74 = call i32 (...) @setsid()
%75 = icmp eq i32 %74, -1
br i1 %75, label %88, label %76
76: ; preds = %73
%77 = call i32 (...) @fork()
switch i32 %77, label %78 [
i32 -1, label %88
i32 0, label %80
]
78: ; preds = %76
%79 = call i32 @exit(i32 0)
unreachable
80: ; preds = %76
%81 = load i32, i32* @O_RDONLY
%82 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %81)
%83 = call i32 @creat(i8* %9, i32 420)
%84 = call i32 @dup2(i32 %82, i32 0)
%85 = call i32 @dup2(i32 %83, i32 1)
%86 = call i32 @dup2(i32 %83, i32 2)
%87 = call i32 @execvp(i8* %24, i8** %26)
br label %88
88: ; preds = %76, %73, %68, %80
%89 = phi i32 [ 0, %80 ], [ %69, %68 ], [ -1, %73 ], [ %77, %76 ]
ret i32 %89
}