#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strlen(char *);
void fread2() {
  FILE *fp;
  char c[] = "hello world,hello clang";
  char buffer[20];
  fp = fopen("file.txt", "w+");
  fwrite(c, strlen(c) + 1, 1, fp);
  fseek(fp, 0, SEEK_SET);
  fread(buffer, strlen(c) + 1, 1, fp);
  printf("%s\n", buffer);
  fclose(fp);
}


x86 assembly (clang, -O0):
<fread2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xe81(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe7e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe7b(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe78(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0x40(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread2>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movaps 0xe82(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x676e616c63206f,%rax
mov    %rax,0x10(%rsp)
lea    0xe80(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xe23(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.fread2.c = [24 x i8] c"hello world,hello clang\00"
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define void @fread2() {
%1 = alloca i32*
%2 = alloca [24 x i8]
%3 = alloca [20 x i8]
%4 = bitcast [24 x i8]* %2 to i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 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** %1
%6 = getelementptr inbounds [24 x i8], [24 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [24 x i8], [24 x i8]* %2, i64 0, i64 0
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 1
%10 = load i32*, i32** %1
%11 = call i32 @fwrite(i8* %6, i64 %9, i32 1, i32* %10)
%12 = load i32*, i32** %1
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%16 = getelementptr inbounds [24 x i8], [24 x i8]* %2, i64 0, i64 0
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = load i32*, i32** %1
%20 = call i32 @fread(i8* %15, i64 %18, i32 1, i32* %19)
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %1
%24 = call i32 @fclose(i32* %23)
ret void
}

LLVM IR (-O2):
@__const.fread2.c = [24 x i8] c"hello world,hello clang\00"
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define void @fread2() {
%1 = alloca [24 x i8]
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [24 x i8], [24 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = call i64 @strlen(i8* %3)
%7 = add nsw i64 %6, 1
%8 = call i32 @fwrite(i8* %3, i64 %7, i32 1, i32* %5)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %5, i32 0, i32 %9)
%11 = call i64 @strlen(i8* %3)
%12 = add nsw i64 %11, 1
%13 = call i32 @fread(i8* %4, i64 %12, i32 1, i32* %5)
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%15 = call i32 @fclose(i32* %5)
ret void
}