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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ bzero(char *, int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *file = fopen("Makefile", "r");
  if (file == NULL) {
    perror("fopen");
    return errno;
  }
  char buf[20];
  bzero(buf, sizeof(buf));
  size_t i = fread(buf, 1, sizeof(buf) - 1, file);
  if (i == 0) {
    printf("none read\n");
    return 0;
  }
  printf("%s\n", buf);
  file = freopen("a.txt", "w+", file);
  if (file == NULL) {
    ferror(file);
    return fileno(file);
  }
  fseek(file, 0, SEEK_SET);
  i = fwrite(buf, 1, sizeof(buf), file);
  if (i == 0) {
    printf("none write\n");
    return 0;
  }
  fseek(file, 0, SEEK_SET);
  char buf2[5];
  i = fread(buf2, sizeof(char), sizeof(buf2) - 1, file);
  if (i == 0) {
    printf("none read\n");
    return 0;
  }
  printf("=> %s\n", buf2);
  fclose(file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
lea    0xe3c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fe <main+0x4e>
lea    0xe23(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1361 <main+0x1b1>
lea    -0x30(%rbp),%rdi
mov    $0x14,%esi
call   10c0 <bzero@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x13,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124c <main+0x9c>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1361 <main+0x1b1>
lea    -0x30(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdx
lea    0xdb3(%rip),%rdi
lea    0xdb2(%rip),%rsi
call   1070 <freopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    129e <main+0xee>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x4(%rbp)
jmp    1361 <main+0x1b1>
mov    -0x10(%rbp),%rdi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x14,%edx
call   10d0 <fwrite@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f2 <main+0x142>
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1361 <main+0x1b1>
mov    -0x10(%rbp),%rdi
mov    0x2cdb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
lea    -0x3d(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1346 <main+0x196>
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1361 <main+0x1b1>
lea    -0x3d(%rbp),%rsi
lea    0xce0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xe31(%rip),%rdi
lea    0xe33(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12eb <main+0x12b>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x14,%esi
call   10d0 <bzero@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x13,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     12bb <main+0xfb>
lea    0x10(%rsp),%rdi
call   1030 <puts@plt>
lea    0xde9(%rip),%rdi
lea    0xde8(%rip),%rsi
mov    %rbx,%rdx
call   1080 <freopen@plt>
test   %rax,%rax
je     12cb <main+0x10b>
mov    %rax,%rbx
mov    0x2d8e(%rip),%r14
mov    (%r14),%edx
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x14,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
test   %rax,%rax
je     12dd <main+0x11d>
mov    (%r14),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0xb(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     12bb <main+0xfb>
lea    0xd75(%rip),%rdi
lea    0xb(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    12c7 <main+0x107>
lea    0xd6a(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    1300 <main+0x140>
xor    %edi,%edi
call   1040 <ferror@plt>
xor    %edi,%edi
call   1090 <fileno@plt>
mov    %eax,%ebp
jmp    1300 <main+0x140>
lea    0xd3d(%rip),%rdi
call   1030 <puts@plt>
jmp    1300 <main+0x140>
lea    0xd19(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"Makefile\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@errno = global i32 0
@.str.3 = [11 x i8] c"none read\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [6 x i8] c"a.txt\00"
@.str.6 = [3 x i8] c"w+\00"
@SEEK_SET = global i32 0
@.str.7 = [12 x i8] c"none write\0A\00"
@.str.8 = [7 x i8] c"=> %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [20 x i8]
%4 = alloca i64
%5 = alloca [5 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 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 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32, i32* @errno
store i32 %11, i32* %1
br label %61
12: ; preds = %0
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%14 = call i32 @bzero(i8* %13, i32 20)
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i64 @fread(i8* %15, i32 1, i32 19, i32* %16)
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %61
22: ; preds = %12
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %2
%26 = call i32* @freopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %25)
store i32* %26, i32** %2
%27 = load i32*, i32** %2
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %34
29: ; preds = %22
%30 = load i32*, i32** %2
%31 = call i32 @ferror(i32* %30)
%32 = load i32*, i32** %2
%33 = call i32 @fileno(i32* %32)
store i32 %33, i32* %1
br label %61
34: ; preds = %22
%35 = load i32*, i32** %2
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %35, i32 0, i32 %36)
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%39 = load i32*, i32** %2
%40 = call i64 @fwrite(i8* %38, i32 1, i32 20, i32* %39)
store i64 %40, i64* %4
%41 = load i64, i64* %4
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %61
45: ; preds = %34
%46 = load i32*, i32** %2
%47 = load i32, i32* @SEEK_SET
%48 = call i32 @fseek(i32* %46, i32 0, i32 %47)
%49 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%50 = load i32*, i32** %2
%51 = call i64 @fread(i8* %49, i32 1, i32 4, i32* %50)
store i64 %51, i64* %4
%52 = load i64, i64* %4
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %61
56: ; preds = %45
%57 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* %57)
%59 = load i32*, i32** %2
%60 = call i32 @fclose(i32* %59)
br label %61
61: ; preds = %56, %54, %43, %29, %20, %9
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [9 x i8] c"Makefile\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@errno = global i32 0
@.str.5 = [6 x i8] c"a.txt\00"
@.str.6 = [3 x i8] c"w+\00"
@SEEK_SET = global i32 0
@.str.8 = [7 x i8] c"=> %s\0A\00"
@str.9 = [11 x i8] c"none write\00"
@str.10 = [10 x i8] c"none read\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = alloca [5 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @errno
br label %43
8: ; preds = %0
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%10 = call i32 @bzero(i8* %9, i32 20)
%11 = call i64 @fread(i8* %9, i32 1, i32 19, i32* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
br label %41
15: ; preds = %8
%16 = call i32 @puts(i8* %9)
%17 = tail call i32* @freopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %3)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @ferror(i32* null)
%21 = tail call i32 @fileno(i32* null)
br label %41
22: ; preds = %15
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @fseek(i32* %17, i32 0, i32 %23)
%25 = call i64 @fwrite(i8* %9, i32 1, i32 20, i32* %17)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
br label %41
29: ; preds = %22
%30 = load i32, i32* @SEEK_SET
%31 = tail call i32 @fseek(i32* %17, i32 0, i32 %30)
%32 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%33 = call i64 @fread(i8* %32, i32 1, i32 4, i32* %17)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
br label %40
37: ; preds = %29
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i8* %32)
%39 = call i32 @fclose(i32* %17)
br label %40
40: ; preds = %37, %35
br label %41
41: ; preds = %40, %27, %19, %13
%42 = phi i32 [ 0, %13 ], [ %21, %19 ], [ 0, %27 ], [ 0, %40 ]
br label %43
43: ; preds = %41, %5
%44 = phi i32 [ %7, %5 ], [ %42, %41 ]
ret i32 %44
}