#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*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  FILE *fp;
  int fd;
  char str[100] = "fileno test string\n";
  fp = fopen("fileno.txt", "w");
  fd = fileno(fp);
  if (fp == NULL)
    return -1;
  write(fd, str, strlen(str));
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe83(%rip),%rsi
mov    $0x64,%edx
call   1060 <memcpy@plt>
lea    0xed6(%rip),%rdi
lea    0xeda(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <main+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <main+0x9b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x8c(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
movaps 0xef1(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xed5(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xeb9(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe9d(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe81(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe65(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
lea    0xeb6(%rip),%rdi
lea    0xeba(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fileno@plt>
test   %rbx,%rbx
je     11ef <main+0x9f>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11f4 <main+0xa4>
mov    $0xffffffff,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.str = [100 x i8] c"fileno test string\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [11 x i8] c"fileno.txt\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = bitcast [100 x i8]* %4 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = call i32 @fileno(i32* %7)
store i32 %8, i32* %3
%9 = load i32*, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %20
12: ; preds = %0
%13 = load i32, i32* %3
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
%17 = call i32 @write(i32 %13, i8* %14, i32 %16)
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
br label %20
20: ; preds = %12, %11
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@__const.main.str = [100 x i8] c"fileno test string\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [11 x i8] c"fileno.txt\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @fileno(i32* %3)
%5 = icmp eq i32* %3, null
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = call i32 @strlen(i8* %2)
%8 = call i32 @write(i32 %4, i8* %2, i32 %7)
%9 = call i32 @fclose(i32* %3)
br label %10
10: ; preds = %0, %6
%11 = phi i32 [ 0, %6 ], [ -1, %0 ]
ret i32 %11
}