#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*/ SEEK_CUR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  FILE *fs = fopen("fileio-fopen.temp", "w+");
  if (fs == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  fprintf(fs, "Hello      , you are nice.\n");
  fseek(fs, -21, SEEK_CUR);
  fprintf(fs, "world");
  fclose(fs);
  fs = fopen("fileio-fopen.temp", "r");
  if (fs == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  char c;
  while (fread(&c, 1, 1, fs) > 0)
    printf("%c", c);
  fclose(fs);
  unlink("fileio-fopen.temp");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11db <main+0x4b>
lea    0xe4d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffeb,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdde(%rip),%rdi
lea    0xe14(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1257 <main+0xc7>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    125c <main+0xcc>
mov    -0x10(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
jle    128f <main+0xff>
lea    0xdbe(%rip),%rdi
movsbl -0x11(%rbp),%esi
call   1060 <printf@plt>
jmp    125c <main+0xcc>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd61(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe60(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     129e <main+0x10e>
mov    %rax,%rbx
lea    0xe5c(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xffffffeb,%esi
call   1070 <fseek@plt>
lea    0xe49(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xdf1(%rip),%rdi
lea    0xe27(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     129e <main+0x10e>
mov    %rax,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jle    1277 <main+0xe7>
lea    0xdf5(%rip),%r14
lea    0xf(%rsp),%r15
nop
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jg     1250 <main+0xc0>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd7a(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd70(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"fileio-fopen.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 = [28 x i8] c"Hello , you are nice.\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [6 x i8] c"world\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32*, i32** %2
%13 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%14 = load i32*, i32** %2
%15 = load i32, i32* @SEEK_CUR
%16 = call i32 @fseek(i32* %14, i32 -21, i32 %15)
%17 = load i32*, i32** %2
%18 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32*, i32** %2
%20 = call i32 @fclose(i32* %19)
%21 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %21, i32** %2
%22 = load i32*, i32** %2
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %11
br label %29
29: ; preds = %33, %28
%30 = load i32*, i32** %2
%31 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %30)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i8, i8* %3
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8 signext %34)
br label %29
36: ; preds = %29
%37 = load i32*, i32** %2
%38 = call i32 @fclose(i32* %37)
%39 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @EXIT_SUCCESS
ret i32 %40
}

LLVM IR (-O2):
@.str = [18 x i8] c"fileio-fopen.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 = [28 x i8] c"Hello , you are nice.\0A\00"
@SEEK_CUR = global i32 0
@.str.4 = [6 x i8] c"world\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [3 x i8] c"%c\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 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 i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 27, i64 1, i32* %2)
%10 = load i32, i32* @SEEK_CUR
%11 = tail call i32 @fseek(i32* %2, i32 -21, i32 %10)
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i64 5, i64 1, i32* %2)
%13 = tail call i32 @fclose(i32* %2)
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %8
%21 = call i64 @fread(i8* %1, i32 1, i32 1, i32* %14)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20, %23
%24 = load i8, i8* %1
%25 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8 signext %24)
%26 = call i64 @fread(i8* %1, i32 1, i32 1, i32* %14)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %23, label %28
28: ; preds = %23, %20
%29 = tail call i32 @fclose(i32* %14)
%30 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_SUCCESS
ret i32 %31
}