#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 */
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
void genln(char **prog, int *fd_in, int *fd_out) {
  pid_t pid = fork();
  if (pid == 0) {
    if (fd_out != NULL) {
      printf("CLOSING STDOUT\n");
      close(STDOUT_FILENO);
      dup(fd_out[1]);
      close(fd_out[1]);
      close(fd_out[0]);
    }
    if (fd_in != NULL) {
      printf("CLOSING STDIN\n");
      close(STDIN_FILENO);
      dup(fd_in[0]);
      close(fd_in[0]);
      close(fd_in[1]);
    }
    printf("%s\n", prog[0]);
    execvp(prog[0], prog);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<genln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1249 <genln+0xe9>
cmpq   $0x0,-0x18(%rbp)
je     11d4 <genln+0x74>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <dup@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x10(%rbp)
je     121d <genln+0xbd>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <dup@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <execvp@plt>
xor    %edi,%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genln>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1180 <genln+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret
test   %rbx,%rbx
je     11b6 <genln+0x56>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x4(%rbx),%edi
call   1040 <dup@plt>
mov    0x4(%rbx),%edi
call   1050 <close@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
test   %r15,%r15
je     11ee <genln+0x8e>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    (%r15),%edi
call   1040 <dup@plt>
mov    (%r15),%edi
call   1050 <close@plt>
mov    0x4(%r15),%edi
call   1050 <close@plt>
mov    (%r14),%rdi
call   1030 <puts@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"CLOSING STDOUT\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [15 x i8] c"CLOSING STDIN\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define void @genln(i8** %0, i32* %1, i32* %2) {
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %60
11: ; preds = %3
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @close(i32 %16)
%18 = load i32*, i32** %6
%19 = getelementptr inbounds i32, i32* %18, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup(i32 %20)
%22 = load i32*, i32** %6
%23 = getelementptr inbounds i32, i32* %22, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32*, i32** %6
%27 = getelementptr inbounds i32, i32* %26, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %14, %11
%31 = load i32*, i32** %5
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %49
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @STDIN_FILENO
%36 = call i32 @close(i32 %35)
%37 = load i32*, i32** %5
%38 = getelementptr inbounds i32, i32* %37, i64 0
%39 = load i32, i32* %38
%40 = call i32 @dup(i32 %39)
%41 = load i32*, i32** %5
%42 = getelementptr inbounds i32, i32* %41, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i32*, i32** %5
%46 = getelementptr inbounds i32, i32* %45, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %33, %30
%50 = load i8**, i8*** %4
%51 = getelementptr inbounds i8*, i8** %50, i64 0
%52 = load i8*, i8** %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = load i8**, i8*** %4
%55 = getelementptr inbounds i8*, i8** %54, i64 0
%56 = load i8*, i8** %55
%57 = load i8**, i8*** %4
%58 = call i32 @execvp(i8* %56, i8** %57)
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %3
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@str = [14 x i8] c"CLOSING STDIN\00"
@str.3 = [15 x i8] c"CLOSING STDOUT\00"
define void @genln(i8** %0, i32* %1, i32* %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %38
6: ; preds = %3
%7 = icmp eq i32* %2, null
br i1 %7, label %19, label %8
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
%10 = load i32, i32* @STDOUT_FILENO
%11 = tail call i32 @close(i32 %10)
%12 = getelementptr inbounds i32, i32* %2, i64 1
%13 = load i32, i32* %12
%14 = tail call i32 @dup(i32 %13)
%15 = load i32, i32* %12
%16 = tail call i32 @close(i32 %15)
%17 = load i32, i32* %2
%18 = tail call i32 @close(i32 %17)
br label %19
19: ; preds = %8, %6
%20 = icmp eq i32* %1, null
br i1 %20, label %32, label %21
21: ; preds = %19
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @STDIN_FILENO
%24 = tail call i32 @close(i32 %23)
%25 = load i32, i32* %1
%26 = tail call i32 @dup(i32 %25)
%27 = load i32, i32* %1
%28 = tail call i32 @close(i32 %27)
%29 = getelementptr inbounds i32, i32* %1, i64 1
%30 = load i32, i32* %29
%31 = tail call i32 @close(i32 %30)
br label %32
32: ; preds = %21, %19
%33 = load i8*, i8** %0
%34 = tail call i32 @puts(i8* dereferenceable(1) %33)
%35 = load i8*, i8** %0
%36 = tail call i32 @execvp(i8* %35, i8** %0)
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %3
ret void
}