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

/* Variables and functions */
scalar_t__ atoi(char *);
scalar_t__ chdir(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getgid();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsid();
int /*<<< orphan*/ wait(int *);
int main(int args, char *argv[]) {
  pid_t target;
  if (args == 1)
    target = getpid();
  else if (args == 2)
    target = atoi(argv[1]);
  else {
    perror("More than 2 arguments");
    exit(-1);
  }
  pid_t pid = fork();
  if (pid == 0) {
    pid_t sid = setsid();
    if (sid < 0) {
      perror("No fue posible crear una nueva sesión");
      exit(-1);
    }
    printf("Session changed to %u\n", sid);
    if ((chdir("/tmp")) < 0) {
      perror("No fue posible cambiar el directorio de trabajo a /tmp\n");
      exit(-1);
    }
    printf("wdir changed to /tmp\n");
    printf("My pid is: %u, my parent's: %u, my session's id: %u and my "
           "grups's%u\n",
           getpid(), getppid(), sid, getgid());
    printf("Child exiting with code 0");
    exit(0);
  } else if (pid > 0) {
    int wstatus;
    wait(&wstatus);
    printf("Parent exiting with code %d\n", wstatus);
    exit(wstatus);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e2 <main+0x32>
mov    $0x0,%al
call   1030 <getpid@plt>
cltq
mov    %rax,-0x18(%rbp)
jmp    121d <main+0x6d>
cmpl   $0x2,-0x8(%rbp)
jne    1202 <main+0x52>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x18(%rbp)
jmp    1218 <main+0x68>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    121d <main+0x6d>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12fe <main+0x14e>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    125f <main+0xaf>
lea    0xdc6(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xddc(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    129d <main+0xed>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <getppid@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1070 <getgid@plt>
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    %eax,%r8d
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    132b <main+0x17b>
lea    -0x2c(%rbp),%rdi
call   10c0 <wait@plt>
mov    -0x2c(%rbp),%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
call   10b0 <exit@plt>
jmp    1330 <main+0x180>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
je     11c9 <main+0x19>
cmp    $0x1,%edi
jne    121c <main+0x6c>
xor    %eax,%eax
call   1040 <getpid@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     11e2 <main+0x32>
jg     11f7 <main+0x47>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
jns    1225 <main+0x75>
lea    0xe21(%rip),%rdi
jmp    1251 <main+0xa1>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
mov    0xc(%rsp),%esi
lea    0xee0(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0xc(%rsp),%edi
call   10b0 <exit@plt>
lea    0xddd(%rip),%rdi
jmp    1251 <main+0xa1>
mov    %rax,%rbx
lea    0xe0e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xed9(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jns    1260 <main+0xb0>
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getgid@plt>
lea    0xdff(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"More than 2 arguments\00"
@.str.1 = [39 x i8] c"No fue posible crear una nueva sesi\C3\B3n\00"
@.str.2 = [23 x i8] c"Session changed to %u\0A\00"
@.str.3 = [5 x i8] c"/tmp\00"
@.str.4 = [56 x i8] c"No fue posible cambiar el directorio de trabajo a /tmp\0A\00"
@.str.5 = [22 x i8] c"wdir changed to /tmp\0A\00"
@.str.6 = [70 x i8] c"My pid is: %u, my parent's: %u, my session's id: %u and my grups's%u\0A\00"
@.str.7 = [26 x i8] c"Child exiting with code 0\00"
@.str.8 = [29 x i8] c"Parent exiting with code %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (...) @getpid()
%14 = sext i32 %13 to i64
store i64 %14, i64* %6
br label %27
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @atoi(i8* %21)
store i64 %22, i64* %6
br label %26
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %18
br label %27
27: ; preds = %26, %12
%28 = call i64 (...) @fork()
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %55
31: ; preds = %27
%32 = call i64 (...) @setsid()
store i64 %32, i64* %8
%33 = load i64, i64* %8
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %31
%39 = load i64, i64* %8
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 %39)
%41 = call i64 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %38
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 (...) @getpid()
%49 = call i32 (...) @getppid()
%50 = load i64, i64* %8
%51 = call i32 (...) @getgid()
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.6, i64 0, i64 0), i32 %48, i32 %49, i64 %50, i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %27
%56 = load i64, i64* %7
%57 = icmp sgt i64 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %55
%59 = call i32 @wait(i32* %9)
%60 = load i32, i32* %9
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %9
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %55
br label %65
65: ; preds = %64
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [22 x i8] c"More than 2 arguments\00"
@.str.1 = [39 x i8] c"No fue posible crear una nueva sesi\C3\B3n\00"
@.str.2 = [23 x i8] c"Session changed to %u\0A\00"
@.str.3 = [5 x i8] c"/tmp\00"
@.str.4 = [56 x i8] c"No fue posible cambiar el directorio de trabajo a /tmp\0A\00"
@.str.6 = [70 x i8] c"My pid is: %u, my parent's: %u, my session's id: %u and my grups's%u\0A\00"
@.str.7 = [26 x i8] c"Child exiting with code 0\00"
@.str.8 = [29 x i8] c"Parent exiting with code %d\0A\00"
@str = [21 x i8] c"wdir changed to /tmp\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
switch i32 %0, label %6 [
i32 1, label %4
i32 2, label %9
]
4: ; preds = %2
%5 = tail call i32 (...) @getpid()
br label %9
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2, %4
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %9
%13 = tail call i64 (...) @setsid()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 %13)
%20 = tail call i64 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (...) @getppid()
%29 = tail call i32 (...) @getgid()
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @.str.6, i64 0, i64 0), i32 %27, i32 %28, i64 %13, i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %9
%34 = icmp sgt i64 %10, 0
br i1 %34, label %35, label %42
35: ; preds = %33
%36 = bitcast i32* %3 to i8*
%37 = call i32 @wait(i32* %3)
%38 = load i32, i32* %3
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %3
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
ret i32 0
}