#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
int setsid();
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "Bad arguments\n");
    exit(1);
  }
  char *pid_name = argv[1];
  char *script = argv[2];
  if (setsid() == -1) {
    pid_t pid = fork();
    if (pid == -1)
      fail("Cannot set session id (failed to fork)");
    else if (pid != 0)
      exit(0);
    else if (setsid() == -1)
      fail("Cannot set session id (after fork)");
  }
  FILE *pid_file;
  pid_file = fopen(pid_name, "w");
  if (pid_file == NULL)
    fail("Cannot open pid file");
  fprintf(pid_file, "%d", getpid());
  fclose(pid_file);
  char *cmd_line[4];
  cmd_line[0] = "bash";
  cmd_line[1] = "-c";
  cmd_line[2] = script;
  cmd_line[3] = NULL;
  execvp(cmd_line[0], cmd_line);
  fail("Cannot exec process");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d2 <main+0x42>
mov    0x2e29(%rip),%rax
movslq (%rax),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    125b <main+0xcb>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    121f <main+0x8f>
lea    0xdfa(%rip),%rdi
call   1040 <fail@plt>
jmp    1256 <main+0xc6>
cmpl   $0x0,-0x24(%rbp)
je     1230 <main+0xa0>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xbc>
lea    0xdef(%rip),%rdi
call   1040 <fail@plt>
jmp    1251 <main+0xc1>
jmp    1256 <main+0xc6>
jmp    125b <main+0xcb>
mov    -0x18(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1286 <main+0xf6>
lea    0xdda(%rip),%rdi
call   1040 <fail@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdba(%rip),%rax
mov    %rax,-0x50(%rbp)
lea    0xdb4(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x50(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1090 <execvp@plt>
lea    0xd8f(%rip),%rdi
call   1040 <fail@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jne    1281 <main+0xe1>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r14
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0x59>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0x4d>
test   %eax,%eax
jne    12ab <main+0x10b>
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0x59>
lea    0xe4b(%rip),%rdi
jmp    11f4 <main+0x54>
lea    0xe1b(%rip),%rdi
call   1040 <fail@plt>
lea    0xe59(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    121c <main+0x7c>
lea    0xe44(%rip),%rdi
call   1040 <fail@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe2e(%rip),%rdi
mov    %rdi,(%rsp)
lea    0xe28(%rip),%rax
mov    %rax,0x8(%rsp)
mov    %r14,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %rsp,%rsi
call   1090 <execvp@plt>
lea    0xe09(%rip),%rdi
call   1040 <fail@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d58(%rip),%rax
movslq (%rax),%rcx
lea    0xd6e(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Bad arguments\0A\00"
@.str.1 = [39 x i8] c"Cannot set session id (failed to fork)\00"
@.str.2 = [35 x i8] c"Cannot set session id (after fork)\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [21 x i8] c"Cannot open pid file\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [5 x i8] c"bash\00"
@.str.7 = [3 x i8] c"-c\00"
@.str.8 = [20 x i8] c"Cannot exec process\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [4 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i32*
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
%26 = call i32 (...) @setsid()
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %47
28: ; preds = %19
%29 = call i32 (...) @fork()
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @fail(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %46
34: ; preds = %28
%35 = load i32, i32* %8
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %34
%40 = call i32 (...) @setsid()
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @fail(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45, %32
br label %47
47: ; preds = %46, %19
%48 = load i8*, i8** %6
%49 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %49, i32** %9
%50 = load i32*, i32** %9
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @fail(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %47
%55 = load i32*, i32** %9
%56 = call i32 (...) @getpid()
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %56)
%58 = load i32*, i32** %9
%59 = call i32 @fclose(i32* %58)
%60 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8** %60
%61 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8** %61
%62 = load i8*, i8** %7
%63 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 2
store i8* %62, i8** %63
%64 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 3
store i8* null, i8** %64
%65 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%66 = load i8*, i8** %65
%67 = getelementptr inbounds [4 x i8*], [4 x i8*]* %10, i64 0, i64 0
%68 = call i32 @execvp(i8* %66, i8** %67)
%69 = call i32 @fail(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Bad arguments\0A\00"
@.str.1 = [39 x i8] c"Cannot set session id (failed to fork)\00"
@.str.2 = [35 x i8] c"Cannot set session id (after fork)\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [21 x i8] c"Cannot open pid file\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [5 x i8] c"bash\00"
@.str.7 = [3 x i8] c"-c\00"
@.str.8 = [20 x i8] c"Cannot exec process\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8*]
%4 = icmp eq i32 %0, 3
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to i32*
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 14, i64 1, i32* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 (...) @setsid()
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %28
18: ; preds = %11
%19 = tail call i32 (...) @fork()
switch i32 %19, label %20 [
i32 -1, label %25
i32 0, label %22
]
20: ; preds = %18
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %18
%23 = tail call i32 (...) @setsid()
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22, %18
%26 = phi i8* [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), %18 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), %22 ]
%27 = tail call i32 @fail(i8* %26)
br label %28
28: ; preds = %25, %22, %11
%29 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 @fail(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = tail call i32 (...) @getpid()
%35 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %34)
%36 = tail call i32 @fclose(i32* %29)
%37 = bitcast [4 x i8*]* %3 to i8*
%38 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8** %38
%39 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8** %39
%40 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 2
store i8* %15, i8** %40
%41 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 3
store i8* null, i8** %41
%42 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8** %38)
%43 = call i32 @fail(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}