#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int main(void) {
  FILE *fp;
  int fileno_fp;
  printf("fileno stdin = %d\n", fileno(stdin));
  printf("fileno stdout = %d\n", fileno(stdout));
  printf("fileno stderr = %d\n", fileno(stderr));
  fp = fopen("test.txt", "r");
  if (fp == NULL) {
    perror("fopen error!");
    return -1;
  }
  fileno_fp = fileno(fp);
  printf("fileno fp = %d\n", fileno_fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
mov    %eax,%esi
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
mov    %eax,%esi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
mov    %eax,%esi
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
lea    0xe7e(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <main+0xa0>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <main+0xcb>
mov    -0x10(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
lea    0xe99(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
lea    0xe8f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe8c(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e5 <main+0x95>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
lea    0xe81(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"fileno stdin = %d\0A\00"
@stdin = global i32* null
@.str.1 = [20 x i8] c"fileno stdout = %d\0A\00"
@stdout = global i32* null
@.str.2 = [20 x i8] c"fileno stderr = %d\0A\00"
@stderr = global i32* null
@.str.3 = [9 x i8] c"test.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [13 x i8] c"fopen error!\00"
@.str.6 = [16 x i8] c"fileno fp = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32*, i32** @stdin
%5 = call i32 @fileno(i32* %4)
%6 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32*, i32** @stdout
%8 = call i32 @fileno(i32* %7)
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32*, i32** @stderr
%11 = call i32 @fileno(i32* %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %25
18: ; preds = %0
%19 = load i32*, i32** %2
%20 = call i32 @fileno(i32* %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
store i32 0, i32* %1
br label %25
25: ; preds = %18, %16
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [19 x i8] c"fileno stdin = %d\0A\00"
@stdin = global i32* null
@.str.1 = [20 x i8] c"fileno stdout = %d\0A\00"
@stdout = global i32* null
@.str.2 = [20 x i8] c"fileno stderr = %d\0A\00"
@stderr = global i32* null
@.str.3 = [9 x i8] c"test.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [13 x i8] c"fopen error!\00"
@.str.6 = [16 x i8] c"fileno fp = %d\0A\00"
define i32 @main() {
%1 = load i32*, i32** @stdin
%2 = tail call i32 @fileno(i32* %1)
%3 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = load i32*, i32** @stdout
%5 = tail call i32 @fileno(i32* %4)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32*, i32** @stderr
%8 = tail call i32 @fileno(i32* %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %18
14: ; preds = %0
%15 = tail call i32 @fileno(i32* %10)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %15)
%17 = tail call i32 @fclose(i32* %10)
br label %18
18: ; preds = %14, %12
%19 = phi i32 [ -1, %12 ], [ 0, %14 ]
ret i32 %19
}