#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  FILE *fs = fopen("fileio-convert.temp", "w+");
  if (fs == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  };
  int fd = fileno(fs);
  write(fd, "hello\n", 6);
  if (fclose(fs) == -1) {
    perror("fclose");
  };
  fd = open("fileio-convert.temp", O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  };
  fs = fdopen(fd, "r");
  if (fs == NULL) {
    perror("fdopen");
    exit(EXIT_FAILURE);
  };
  char buf[100];
  fgets(buf, 100, fs);
  printf("Read: %s", buf);
  if (fclose(fs) == -1) {
    perror("fclose");
  };
  unlink("fileio-convert.temp");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe37(%rip),%rdi
lea    0xe44(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fe <main+0x4e>
lea    0xe2c(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe09(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0xffffffff,%eax
jne    123c <main+0x8c>
lea    0xded(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    0xdb4(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1278 <main+0xc8>
lea    0xdca(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xda9(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12b0 <main+0x100>
lea    0xd90(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    -0x80(%rbp),%rsi
lea    0xd67(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0xffffffff,%eax
jne    12f0 <main+0x140>
lea    0xd39(%rip),%rdi
call   10c0 <perror@plt>
lea    0xd09(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xe42(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a5 <main+0xf5>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <fileno@plt>
lea    0xe38(%rip),%rsi
mov    %eax,%edi
mov    $0x6,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    $0xffffffff,%eax
je     1286 <main+0xd6>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0xdee(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ae <main+0xfe>
lea    0xe04(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     12b7 <main+0x107>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
lea    0xde0(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    $0xffffffff,%eax
je     1297 <main+0xe7>
lea    0xd90(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd97(%rip),%rdi
call   10c0 <perror@plt>
jmp    1202 <main+0x52>
lea    0xd86(%rip),%rdi
call   10c0 <perror@plt>
jmp    1269 <main+0xb9>
lea    0xd6b(%rip),%rdi
jmp    12be <main+0x10e>
lea    0xd7a(%rip),%rdi
jmp    12be <main+0x10e>
lea    0xd6f(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"fileio-convert.temp\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [7 x i8] c"hello\0A\00"
@.str.4 = [7 x i8] c"fclose\00"
@O_RDONLY = global i32 0
@.str.5 = [5 x i8] c"open\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [7 x i8] c"fdopen\00"
@.str.8 = [9 x i8] c"Read: %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @fileno(i32* %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %22
%32 = load i32, i32* %3
%33 = call i32* @fdopen(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %33, i32** %2
%34 = load i32*, i32** %2
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %31
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%42 = load i32*, i32** %2
%43 = call i32 @fgets(i8* %41, i32 100, i32* %42)
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%45 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %44)
%46 = load i32*, i32** %2
%47 = call i32 @fclose(i32* %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %40
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %40
%52 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%53 = load i32, i32* @EXIT_SUCCESS
ret i32 %53
}

LLVM IR (-O2):
@.str = [20 x i8] c"fileio-convert.temp\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [7 x i8] c"hello\0A\00"
@.str.4 = [7 x i8] c"fclose\00"
@O_RDONLY = global i32 0
@.str.5 = [5 x i8] c"open\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [7 x i8] c"fdopen\00"
@.str.8 = [9 x i8] c"Read: %s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [100 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i32 @fileno(i32* %2)
%10 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%11 = tail call i32 @fclose(i32* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
%24 = tail call i32* @fdopen(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %23
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%32 = call i32 @fgets(i8* %31, i32 100, i32* %24)
%33 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %31)
%34 = call i32 @fclose(i32* %24)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %30
%39 = call i32 @unlink(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_SUCCESS
ret i32 %40
}