#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;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getpgrp();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setsid();
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <program> [args...]\n", argv[0]);
    return EXIT_FAILURE;
  }
  if (getpid() == getpgrp()) {
    pid_t pid = fork();
    switch (pid) {
    case 0:
      break;
    case -1:
      perror("fork");
      return EXIT_FAILURE;
    default:
      return EXIT_SUCCESS;
    }
  }
  if (setsid() < 0) {
    perror("setsid");
    return EXIT_FAILURE;
  }
  execvp(argv[1], &argv[1]);
  perror("exec");
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11bd <main+0x4d>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getpgrp@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jne    1245 <main+0xd5>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xffffffff,%eax
je     1212 <main+0xa2>
jmp    11fd <main+0x8d>
mov    -0x24(%rbp),%eax
test   %eax,%eax
jne    122f <main+0xbf>
jmp    120d <main+0x9d>
jmp    1240 <main+0xd0>
lea    0xe06(%rip),%rdi
call   1070 <perror@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <main+0x130>
jmp    1245 <main+0xd5>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    1273 <main+0x103>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
lea    0xd9c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     119d <main+0x2d>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e3d(%rip),%rax
jmp    11ca <main+0x5a>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getpgrp@plt>
cmp    %rax,%rbx
jne    11d4 <main+0x64>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11d4 <main+0x64>
cmp    $0xffffffff,%eax
je     11e9 <main+0x79>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
jns    11f2 <main+0x82>
lea    0xe3d(%rip),%rdi
jmp    1209 <main+0x99>
lea    0xe2f(%rip),%rdi
jmp    1209 <main+0x99>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   1080 <execvp@plt>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
jmp    1194 <main+0x24>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s <program> [args...]\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [5 x i8] c"exec\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %3
br label %45
16: ; preds = %2
%17 = call i64 (...) @getpid()
%18 = call i64 (...) @getpgrp()
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = call i32 (...) @fork()
store i32 %21, i32* %6
%22 = load i32, i32* %6
switch i32 %22, label %27 [
i32 0, label %23
i32 -1, label %24
]
23: ; preds = %20
br label %29
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %3
br label %45
27: ; preds = %20
%28 = load i32, i32* @EXIT_SUCCESS
store i32 %28, i32* %3
br label %45
29: ; preds = %23
br label %30
30: ; preds = %29, %16
%31 = call i64 (...) @setsid()
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %3
br label %45
36: ; preds = %30
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = call i32 @execvp(i8* %39, i8** %41)
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
store i32 %44, i32* %3
br label %45
45: ; preds = %36, %33, %27, %24, %9
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s <program> [args...]\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [5 x i8] c"exec\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %26
8: ; preds = %2
%9 = tail call i64 (...) @getpid()
%10 = tail call i64 (...) @getpgrp()
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = tail call i32 (...) @fork()
switch i32 %13, label %26 [
i32 0, label %16
i32 -1, label %14
]
14: ; preds = %12
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
16: ; preds = %12, %8
%17 = tail call i64 (...) @setsid()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %26
21: ; preds = %16
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i32 @execvp(i8* %23, i8** %22)
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %12, %14, %21, %19, %4
%27 = phi i32* [ @EXIT_FAILURE, %4 ], [ @EXIT_FAILURE, %19 ], [ @EXIT_FAILURE, %21 ], [ @EXIT_FAILURE, %14 ], [ @EXIT_SUCCESS, %12 ]
%28 = load i32, i32* %27
ret i32 %28
}