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

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsid();
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int system(char *);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Specify a script/bin to run...\n");
    return 1;
  }
  if (access(argv[1], X_OK)) {
    printf("I can't run this script. Nothing there? Permissions?");
    return 1;
  }
  FILE *fp = NULL;
  pid_t process_id = 0;
  pid_t sid = 0;
  process_id = fork();
  if (process_id < 0) {
    printf("fork failed!\n");
    exit(1);
  }
  if (process_id > 0) {
    printf("process_id of child process %d \n", process_id);
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    exit(1);
  }
  char cmd[255];
  sprintf(cmd, "cd $(dirname %s); ./$(basename %s) -i", argv[1], argv[1]);
  printf("exec command %s\n", cmd);
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return system(cmd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cd <main+0x3d>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1309 <main+0x179>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     1207 <main+0x77>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1309 <main+0x179>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    124d <main+0xbd>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    1271 <main+0xe1>
mov    -0x20(%rbp),%rsi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1070 <umask@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1298 <main+0x108>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdce(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x130(%rbp),%rsi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
lea    -0x130(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x100,%rsp
cmp    $0x1,%edi
jg     11c3 <main+0x23>
lea    0xee6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    127e <main+0xde>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
test   %rax,%rax
je     11f5 <main+0x55>
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    127e <main+0xde>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1287 <main+0xe7>
jne    129d <main+0xfd>
xor    %edi,%edi
call   1080 <umask@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
js     12b5 <main+0x115>
mov    0x8(%rbx),%rcx
lea    0xe29(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rcx,%rdx
call   10a0 <sprintf@plt>
lea    0xe3a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
add    $0x100,%rsp
pop    %rbx
ret
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd91(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Specify a script/bin to run...\0A\00"
@X_OK = global i32 0
@.str.1 = [53 x i8] c"I can't run this script. Nothing there? Permissions?\00"
@.str.2 = [14 x i8] c"fork failed!\0A\00"
@.str.3 = [33 x i8] c"process_id of child process %d \0A\00"
@.str.4 = [38 x i8] c"cd $(dirname %s); ./$(basename %s) -i\00"
@.str.5 = [17 x i8] c"exec command %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca [255 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %63
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @X_OK
%19 = call i64 @access(i8* %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %63
23: ; preds = %14
store i32* null, i32** %6
store i64 0, i64* %7
store i64 0, i64* %8
%24 = call i64 (...) @fork()
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = load i64, i64* %7
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i64 %34)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %30
%38 = call i32 @umask(i32 0)
%39 = call i64 (...) @setsid()
store i64 %39, i64* %8
%40 = load i64, i64* %8
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i32 @sprintf(i8* %45, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %48, i8* %51)
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @STDIN_FILENO
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* @STDOUT_FILENO
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @STDERR_FILENO
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%62 = call i32 @system(i8* %61)
store i32 %62, i32* %3
br label %63
63: ; preds = %44, %21, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@X_OK = global i32 0
@.str.1 = [53 x i8] c"I can't run this script. Nothing there? Permissions?\00"
@.str.3 = [33 x i8] c"process_id of child process %d \0A\00"
@.str.4 = [38 x i8] c"cd $(dirname %s); ./$(basename %s) -i\00"
@.str.5 = [17 x i8] c"exec command %s\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@str = [13 x i8] c"fork failed!\00"
@str.6 = [31 x i8] c"Specify a script/bin to run...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [255 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
br label %44
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @X_OK
%11 = tail call i64 @access(i8* %9, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %44
15: ; preds = %7
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = icmp eq i64 %16, 0
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i64 %16)
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %21
%27 = tail call i32 @umask(i32 0)
%28 = tail call i64 (...) @setsid()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %26
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%34 = load i8*, i8** %8
%35 = call i32 @sprintf(i8* %33, i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i8* %34, i8* %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%37 = load i32, i32* @STDIN_FILENO
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @STDERR_FILENO
%42 = call i32 @close(i32 %41)
%43 = call i32 @system(i8* %33)
br label %44
44: ; preds = %32, %13, %5
%45 = phi i32 [ 1, %5 ], [ 1, %13 ], [ %43, %32 ]
ret i32 %45
}