#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*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
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 [arg ...]\n", argv[0]);
    exit(1);
  }
  if (getpgrp() == getpid()) {
    switch (fork()) {
    case -1:
      perror("fork");
      exit(1);
    case 0:
      break;
    default:
      exit(0);
    }
  }
  if (setsid() < 0) {
    perror("setsid");
    exit(1);
  }
  execvp(argv[1], argv + 1);
  perror("execvp");
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c6 <main+0x46>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1060 <getpgrp@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
jne    1239 <main+0xb9>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     1212 <main+0x92>
jmp    1202 <main+0x82>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     1228 <main+0xa8>
jmp    122d <main+0xad>
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1234 <main+0xb4>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    1239 <main+0xb9>
mov    $0x0,%al
call   1040 <setsid@plt>
cmp    $0x0,%rax
jge    1260 <main+0xe0>
lea    0xdd1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   1080 <execvp@plt>
lea    0xdad(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jg     11ae <main+0x2e>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   1060 <getpgrp@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
cmp    %rax,%rbx
jne    11dd <main+0x5d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11dd <main+0x5d>
cmp    $0xffffffff,%eax
jne    1218 <main+0x98>
lea    0xe42(%rip),%rdi
jmp    1209 <main+0x89>
xor    %eax,%eax
call   1040 <setsid@plt>
test   %rax,%rax
jns    11f2 <main+0x72>
lea    0xe32(%rip),%rdi
jmp    1209 <main+0x89>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   1080 <execvp@plt>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s program [arg ...]\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [7 x i8] c"setsid\00"
@.str.3 = [7 x i8] c"execvp\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 slt i32 %6, 2
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i64 (...) @getpgrp()
%17 = call i64 (...) @getpid()
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = call i32 (...) @fork()
switch i32 %20, label %25 [
i32 -1, label %21
i32 0, label %24
]
21: ; preds = %19
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %19
br label %27
25: ; preds = %19
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %24
br label %28
28: ; preds = %27, %15
%29 = call i64 (...) @setsid()
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = call i32 @execvp(i8* %37, i8** %39)
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
}

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