#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*/ buf;

/* Variables and functions */
int m5_readfile(char *, int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ write(int, char *, int);
int read_file(int dest_fid) {
  char buf[256 * 1024];
  int offset = 0;
  int len;
  memset(buf, 0, sizeof(buf));
  while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
    write(dest_fid, buf, len);
    offset += len;
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40020,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x40014(%rbp)
lea    -0x40010(%rbp),%rdi
xor    %esi,%esi
mov    $0x40000,%edx
call   1040 <memset@plt>
lea    -0x40010(%rbp),%rdi
mov    -0x40014(%rbp),%edx
mov    $0x40000,%esi
call   1050 <m5_readfile@plt>
mov    %eax,-0x40018(%rbp)
cmp    $0x0,%eax
jle    11ad <read_file+0x7d>
mov    -0x8(%rbp),%edi
lea    -0x40010(%rbp),%rsi
mov    -0x40018(%rbp),%edx
call   1030 <write@plt>
mov    -0x40018(%rbp),%eax
add    -0x40014(%rbp),%eax
mov    %eax,-0x40014(%rbp)
jmp    115b <read_file+0x2b>
mov    -0x4(%rbp),%eax
add    $0x40020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x40008,%rsp
mov    %edi,%r14d
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40000,%edx
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    $0x40000,%esi
xor    %edx,%edx
call   1050 <m5_readfile@plt>
test   %eax,%eax
jle    1194 <read_file+0x64>
mov    %eax,%ebx
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
add    %ebx,%ebp
mov    %r15,%rdi
mov    $0x40000,%esi
mov    %ebp,%edx
call   1050 <m5_readfile@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1170 <read_file+0x40>
add    $0x40008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [262144 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%7 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 262144)
br label %9
9: ; preds = %14, %1
%10 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @m5_readfile(i8* %10, i32 262144, i32 %11)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load i32, i32* %3
%16 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* %6
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
%19 = load i32, i32* %6
%20 = load i32, i32* %5
%21 = add nsw i32 %20, %19
store i32 %21, i32* %5
br label %9
22: ; preds = %9
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
define i32 @read_file(i32 %0) {
%2 = alloca [262144 x i8]
%3 = getelementptr inbounds [262144 x i8], [262144 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 262144)
%5 = call i32 @m5_readfile(i8* %3, i32 262144, i32 0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %1, %7
%8 = phi i32 [ %12, %7 ], [ %5, %1 ]
%9 = phi i32 [ %11, %7 ], [ 0, %1 ]
%10 = call i32 @write(i32 %0, i8* %3, i32 %8)
%11 = add nsw i32 %8, %9
%12 = call i32 @m5_readfile(i8* %3, i32 262144, i32 %11)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %7, label %14
14: ; preds = %7, %1
ret i32 undef
}