#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int fp1, fp2;
  printf("stdin is:\t %d\n", fileno(stdin));
  printf("stdout is:\t %d\n", fileno(stdout));
  printf("stderr is:\t %d\n", fileno(stderr));
  if ((fp1 = open("/home/lennon/share/dev/apue/chapter03/test.txt",
                  O_WRONLY)) == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if ((fp2 = open("/home/lennon/share/dev/apue/chapter03/test2.txt",
                  O_WRONLY)) == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  printf("test.txt is :\t%d\n", fp1);
  printf("test2.txt is :\t%d\n", fp2);
  close(fp1);
  close(fp2);
  exit(0);
}


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)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%esi
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%esi
lea    0xe66(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%esi
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1205 <main+0xa5>
lea    0xe6c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1240 <main+0xe0>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe4c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
lea    0xe88(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2dfa(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe67(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e8 <main+0x88>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
lea    0xe85(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
jne    1202 <main+0xa2>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %eax,%ebp
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
lea    0xe8c(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"stdin is:\09 %d\0A\00"
@stdin = global i32 0
@.str.1 = [16 x i8] c"stdout is:\09 %d\0A\00"
@stdout = global i32 0
@.str.2 = [16 x i8] c"stderr is:\09 %d\0A\00"
@stderr = global i32 0
@.str.3 = [47 x i8] c"/home/lennon/share/dev/apue/chapter03/test.txt\00"
@O_WRONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [48 x i8] c"/home/lennon/share/dev/apue/chapter03/test2.txt\00"
@.str.6 = [18 x i8] c"test.txt is :\09%d\0A\00"
@.str.7 = [19 x i8] c"test2.txt is :\09%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @stdin
%9 = call i32 @fileno(i32 %8)
%10 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @stdout
%12 = call i32 @fileno(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stderr
%15 = call i32 @fileno(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0), i32 %25)
store i32 %26, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
%33 = load i32, i32* %6
%34 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
%41 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [15 x i8] c"stdin is:\09 %d\0A\00"
@stdin = global i32 0
@.str.1 = [16 x i8] c"stdout is:\09 %d\0A\00"
@stdout = global i32 0
@.str.2 = [16 x i8] c"stderr is:\09 %d\0A\00"
@stderr = global i32 0
@.str.3 = [47 x i8] c"/home/lennon/share/dev/apue/chapter03/test.txt\00"
@O_WRONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [48 x i8] c"/home/lennon/share/dev/apue/chapter03/test2.txt\00"
@.str.6 = [18 x i8] c"test.txt is :\09%d\0A\00"
@.str.7 = [19 x i8] c"test2.txt is :\09%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @stdin
%4 = tail call i32 @fileno(i32 %3)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @stdout
%7 = tail call i32 @fileno(i32 %6)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @stderr
%10 = tail call i32 @fileno(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i32, i32* @O_WRONLY
%21 = tail call i32 @open(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %19
%28 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %13)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %21)
%30 = tail call i32 @close(i32 %13)
%31 = tail call i32 @close(i32 %21)
%32 = tail call i32 @exit(i32 0)
unreachable
}