#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*/ O_RDONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main() {
  int fd;
  printf("hello, this should be output\n");
  fclose(stdout);
  printf("hello, my cat\n");
  fd = open("data.in", O_RDONLY);
  if (fd >= 0) {
    fprintf(stderr, "fd:%d\n", fd);
  } else {
    fprintf(stderr, "error fd:%d\n", fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
lea    0xeae(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11b4 <main+0x74>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe91(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11cc <main+0x8c>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe6e(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%edi
lea    0xe80(%rip),%rcx
lea    0xe73(%rip),%rsi
cmovns %rcx,%rsi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [30 x i8] c"hello, this should be output\0A\00"
@stdout = global i32 0
@.str.1 = [15 x i8] c"hello, my cat\0A\00"
@.str.2 = [8 x i8] c"data.in\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [7 x i8] c"fd:%d\0A\00"
@.str.4 = [13 x i8] c"error fd:%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @stdout
%5 = call i32 @fclose(i32 %4)
%6 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* %2
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %19
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i32, i32* %2
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %11
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str.2 = [8 x i8] c"data.in\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [7 x i8] c"fd:%d\0A\00"
@.str.4 = [13 x i8] c"error fd:%d\0A\00"
@str = [29 x i8] c"hello, this should be output\00"
@str.5 = [14 x i8] c"hello, my cat\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @stdout
%3 = tail call i32 @fclose(i32 %2)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, -1
%8 = load i32, i32* @stderr
%9 = select i1 %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0)
%10 = tail call i32 @fprintf(i32 %8, i8* %9, i32 %6)
ret i32 0
}