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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int m5_readfile(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write(int, int /*<<< orphan*/ *, int);
int read_file(int dest_fid) {
  uint8_t buf[256 * 1024];
  int offset = 0;
  int len, ret;
  memset(buf, 0, sizeof(buf));
  while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
    uint8_t *base = buf;
    offset += len;
    do {
      ret = write(dest_fid, base, len);
      if (ret < 0) {
        perror("Failed to write file");
        exit(2);
      } else if (ret == 0) {
        fprintf(stderr, "Failed to write file: "
                        "Unhandled short write\n");
        exit(2);
      }
      base += ret;
      len -= ret;
    } while (len);
  }
  return offset;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100030,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x100014(%rbp)
lea    -0x100010(%rbp),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
lea    -0x100010(%rbp),%rdi
mov    -0x100014(%rbp),%edx
mov    $0x100000,%esi
call   1060 <m5_readfile@plt>
mov    %eax,-0x100018(%rbp)
cmp    $0x0,%eax
jle    1284 <read_file+0x124>
lea    -0x100010(%rbp),%rax
mov    %rax,-0x100028(%rbp)
mov    -0x100018(%rbp),%eax
add    -0x100014(%rbp),%eax
mov    %eax,-0x100014(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x100028(%rbp),%rsi
mov    -0x100018(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x10001c(%rbp)
cmpl   $0x0,-0x10001c(%rbp)
jge    120f <read_file+0xaf>
lea    0xe00(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x10001c(%rbp)
jne    123b <read_file+0xdb>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    1240 <read_file+0xe0>
mov    -0x10001c(%rbp),%ecx
mov    -0x100028(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x100028(%rbp)
mov    -0x10001c(%rbp),%ecx
mov    -0x100018(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x100018(%rbp)
cmpl   $0x0,-0x100018(%rbp)
jne    11d1 <read_file+0x71>
jmp    118b <read_file+0x2b>
mov    -0x100014(%rbp),%eax
add    $0x100030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100000,%rsp
mov    %edi,%r12d
mov    %rsp,%r14
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    $0x100000,%esi
xor    %edx,%edx
call   1060 <m5_readfile@plt>
test   %eax,%eax
jle    11e3 <read_file+0x83>
mov    %eax,%ebp
xor    %r15d,%r15d
nop
add    %ebp,%r15d
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11f6 <read_file+0x96>
je     120c <read_file+0xac>
mov    %eax,%ecx
lea    (%rbx,%rcx,4),%rbx
sub    %eax,%ebp
jne    11b0 <read_file+0x50>
mov    %r14,%rdi
mov    $0x100000,%esi
mov    %r15d,%edx
call   1060 <m5_readfile@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     11a0 <read_file+0x40>
mov    %r15d,%eax
add    $0x100000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Failed to write file\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"Failed to write file: Unhandled short write\0A\00"
define i32 @read_file(i32 %0) {
%2 = alloca i32
%3 = alloca [262144 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %2
store i32 0, i32* %4
%8 = getelementptr inbounds [262144 x i32], [262144 x i32]* %3, i64 0, i64 0
%9 = call i32 @memset(i32* %8, i32 0, i32 1048576)
br label %10
10: ; preds = %49, %1
%11 = getelementptr inbounds [262144 x i32], [262144 x i32]* %3, i64 0, i64 0
%12 = load i32, i32* %4
%13 = call i32 @m5_readfile(i32* %11, i32 1048576, i32 %12)
store i32 %13, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %50
15: ; preds = %10
%16 = getelementptr inbounds [262144 x i32], [262144 x i32]* %3, i64 0, i64 0
store i32* %16, i32** %7
%17 = load i32, i32* %5
%18 = load i32, i32* %4
%19 = add nsw i32 %18, %17
store i32 %19, i32* %4
br label %20
20: ; preds = %46, %15
%21 = load i32, i32* %2
%22 = load i32*, i32** %7
%23 = load i32, i32* %5
%24 = call i32 @write(i32 %21, i32* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %20
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 2)
unreachable
37: ; preds = %30
br label %38
38: ; preds = %37
%39 = load i32, i32* %6
%40 = load i32*, i32** %7
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i32, i32* %40, i64 %41
store i32* %42, i32** %7
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %5
br label %46
46: ; preds = %38
%47 = load i32, i32* %5
%48 = icmp ne i32 %47, 0
br i1 %48, label %20, label %49
49: ; preds = %46
br label %10
50: ; preds = %10
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@.str = [21 x i8] c"Failed to write file\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"Failed to write file: Unhandled short write\0A\00"
define i32 @read_file(i32 %0) {
%2 = alloca [262144 x i32]
%3 = bitcast [262144 x i32]* %2 to i8*
%4 = getelementptr inbounds [262144 x i32], [262144 x i32]* %2, i64 0, i64 0
%5 = call i32 @memset(i32* %4, i32 0, i32 1048576)
%6 = call i32 @m5_readfile(i32* %4, i32 1048576, i32 0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %11, label %34
8: ; preds = %29
%9 = call i32 @m5_readfile(i32* %4, i32 1048576, i32 %14)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %1, %8
%12 = phi i32 [ %9, %8 ], [ %6, %1 ]
%13 = phi i32 [ %14, %8 ], [ 0, %1 ]
%14 = add nsw i32 %12, %13
br label %15
15: ; preds = %29, %11
%16 = phi i32 [ %12, %11 ], [ %32, %29 ]
%17 = phi i32* [ %4, %11 ], [ %31, %29 ]
%18 = call i32 @write(i32 %0, i32* %17, i32 %16)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 2)
unreachable
23: ; preds = %15
%24 = icmp eq i32 %18, 0
br i1 %24, label %25, label %29
25: ; preds = %23
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %23
%30 = zext i32 %18 to i64
%31 = getelementptr inbounds i32, i32* %17, i64 %30
%32 = sub nsw i32 %16, %18
%33 = icmp eq i32 %32, 0
br i1 %33, label %8, label %15
34: ; preds = %8, %1
%35 = phi i32 [ 0, %1 ], [ %14, %8 ]
ret i32 %35
}