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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int /*<<< orphan*/, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main() {
  FILE *fp;
  char input[100] = "fread OK!\n";
  char output[100];
  fp = fopen("fread.txt", "w");
  if (fp == NULL)
    return -1;
  fwrite(input, strlen(input), 1, fp);
  fclose(fp);
  fp = fopen("fread.txt", "r");
  if (fp == NULL)
    return -1;
  fread(output, sizeof(output), 1, fp);
  printf("fread string: %s", output);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe73(%rip),%rsi
mov    $0x64,%edx
call   1070 <memcpy@plt>
lea    0xec6(%rip),%rdi
lea    0xec9(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c5 <main+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <main+0xf4>
lea    -0x80(%rbp),%rax
mov    %rax,-0xf8(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xf8(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe64(%rip),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1227 <main+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <main+0xf4>
lea    -0xf0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x64,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0xf0(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xe0,%rsp
movaps 0xede(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xec2(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xea6(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe8a(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe6e(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe52(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
lea    0xea3(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1070 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     124d <main+0xed>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe5e(%rip),%rdi
lea    0xe63(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     124d <main+0xed>
mov    %rax,%rbx
lea    0x70(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
lea    0xe37(%rip),%rdi
mov    %rbp,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.input = [100 x i8] c"fread OK!\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\00\00\00\00\00\00\00\00\00"
@.str = [10 x i8] c"fread.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [17 x i8] c"fread string: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
store i32 0, i32* %1
%5 = bitcast [100 x i8]* %3 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 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 %10
9: ; preds = %0
store i32 -1, i32* %1
br label %30
10: ; preds = %0
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = load i32*, i32** %2
%15 = call i32 @fwrite(i8* %11, i32 %13, i32 1, i32* %14)
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %18, i32** %2
%19 = load i32*, i32** %2
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %10
store i32 -1, i32* %1
br label %30
22: ; preds = %10
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%24 = load i32*, i32** %2
%25 = call i32 @fread(i8* %23, i32 100, i32 1, i32* %24)
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32*, i32** %2
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %1
br label %30
30: ; preds = %22, %21, %9
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@__const.main.input = [100 x i8] c"fread OK!\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\00\00\00\00\00\00\00\00\00"
@.str = [10 x i8] c"fread.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [17 x i8] c"fread string: %s\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %0
%8 = call i32 @strlen(i8* %3)
%9 = call i32 @fwrite(i8* %3, i32 %8, i32 1, i32* %5)
%10 = call i32 @fclose(i32* %5)
%11 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = call i32 @fread(i8* %4, i32 100, i32 1, i32* %11)
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%16 = call i32 @fclose(i32* %11)
br label %17
17: ; preds = %7, %0, %13
%18 = phi i32 [ 0, %13 ], [ -1, %0 ], [ -1, %7 ]
ret i32 %18
}