#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 /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
void test_fread() {
  FILE *stream;
  char list[30];
  int i, numread, numwrite;
  if ((stream = fopen("fread.out", "w+")) != NULL) {
    for (i = 0; i < 26; i++) {
      list[i] = (char)('z' - i);
    }
    numwrite = fwrite(list, sizeof(char), 26, stream);
    printf("Wrote %d items\n", numwrite);
    fclose(stream);
  } else {
    printf("Problem openting the file\n");
  }
  if ((stream = fopen("fread.out", "r+")) != NULL) {
    numread = fread(list, sizeof(char), 26, stream);
    printf("Number of items read = %d\n", numread);
    printf("content is %s\n", list);
    fclose(stream);
  }
}


x86 assembly (clang, -O0):
<test_fread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xea1(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11e3 <test_fread+0x93>
movl   $0x0,-0x34(%rbp)
cmpl   $0x1a,-0x34(%rbp)
jge    11aa <test_fread+0x5a>
mov    $0x7a,%eax
sub    -0x34(%rbp),%eax
mov    %al,%cl
movslq -0x34(%rbp),%rax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1180 <test_fread+0x30>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1a,%edx
call   1070 <fwrite@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11f1 <test_fread+0xa1>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe08(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     1258 <test_fread+0x108>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1a,%edx
call   1030 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fread>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xea2(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d2 <test_fread+0x72>
mov    %rax,%rbx
movaps 0xe77(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x636465666768696a,%rax
65 64 63
mov    %rax,0x10(%rsp)
movw   $0x6162,0x18(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1a,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11de <test_fread+0x7e>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
lea    0xe2b(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     123a <test_fread+0xda>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1a,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
lea    0xe18(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe23(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"fread.out\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [16 x i8] c"Wrote %d items\0A\00"
@.str.3 = [27 x i8] c"Problem openting the file\0A\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [27 x i8] c"Number of items read = %d\0A\00"
@.str.6 = [15 x i8] c"content is %s\0A\00"
define void @test_fread() {
%1 = alloca i32*
%2 = alloca [30 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %1
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %30
8: ; preds = %0
store i32 0, i32* %3
br label %9
9: ; preds = %19, %8
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 26
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i32, i32* %3
%14 = sub nsw i32 122, %13
%15 = trunc i32 %14 to i8
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 %17
store i8 %15, i8* %18
br label %19
19: ; preds = %12
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %9
22: ; preds = %9
%23 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%24 = load i32*, i32** %1
%25 = call i32 @fwrite(i8* %23, i32 1, i32 26, i32* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %1
%29 = call i32 @fclose(i32* %28)
br label %32
30: ; preds = %0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
%33 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %33, i32** %1
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %45
35: ; preds = %32
%36 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%37 = load i32*, i32** %1
%38 = call i32 @fread(i8* %36, i32 1, i32 26, i32* %37)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %41)
%43 = load i32*, i32** %1
%44 = call i32 @fclose(i32* %43)
br label %45
45: ; preds = %35, %32
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"fread.out\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [16 x i8] c"Wrote %d items\0A\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [27 x i8] c"Number of items read = %d\0A\00"
@.str.6 = [15 x i8] c"content is %s\0A\00"
@str = [26 x i8] c"Problem openting the file\00"
define void @test_fread() {
%1 = alloca [30 x i8]
%2 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %20, label %5
5: ; preds = %0
%6 = bitcast [30 x i8]* %1 to <16 x i8>*
store <16 x i8> <i8 122, i8 121, i8 120, i8 119, i8 118, i8 117, i8 116, i8 115, i8 114, i8 113, i8 112, i8 111, i8 110, i8 109, i8 108, i8 107>, <16 x i8>* %6
%7 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 16
store i8 106, i8* %7
%8 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 17
store i8 105, i8* %8
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 18
store i8 104, i8* %9
%10 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 19
store i8 103, i8* %10
%11 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 20
store i8 102, i8* %11
%12 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 21
store i8 101, i8* %12
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 22
store i8 100, i8* %13
%14 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 23
store i8 99, i8* %14
%15 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 24
store i8 98, i8* %15
%16 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 25
store i8 97, i8* %16
%17 = call i32 @fwrite(i8* %2, i32 1, i32 26, i32* %3)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = tail call i32 @fclose(i32* %3)
br label %22
20: ; preds = %0
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %20, %5
%23 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = call i32 @fread(i8* %2, i32 1, i32 26, i32* %23)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%29 = call i32 @fclose(i32* %23)
br label %30
30: ; preds = %25, %22
ret void
}