#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*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("%s <shell script>\n", argv[0]);
    return 0;
  }
  setsid();
  chdir("/");
  close(0);
  close(1);
  close(2);
  if (fork() == 0) {
    system(argv[1]);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11af <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1209 <main+0x99>
mov    $0x0,%al
call   1070 <setsid@plt>
lea    0xe56(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    1202 <main+0x92>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11c3 <main+0x53>
xor    %eax,%eax
call   1070 <setsid@plt>
lea    0xe8c(%rip),%rdi
call   1030 <chdir@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    11d2 <main+0x62>
mov    0x8(%rbx),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    (%rbx),%rsi
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"%s <shell script>\0A\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 2
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 0
%11 = load i8*, i8** %10
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 0, i32* %3
br label %28
13: ; preds = %2
%14 = call i32 (...) @setsid()
%15 = call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @close(i32 0)
%17 = call i32 @close(i32 1)
%18 = call i32 @close(i32 2)
%19 = call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %13
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @system(i8* %24)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %13
store i32 0, i32* %3
br label %28
28: ; preds = %27, %8
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [19 x i8] c"%s <shell script>\0A\00"
@.str.1 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %20
7: ; preds = %2
%8 = tail call i32 (...) @setsid()
%9 = tail call i32 @chdir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @close(i32 0)
%11 = tail call i32 @close(i32 1)
%12 = tail call i32 @close(i32 2)
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %7
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @system(i8* %17)
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %7, %4
ret i32 0
}