#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ m5_writefile(char *, int, int, char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
void write_file(const char *filename) {
  fprintf(stderr, "opening %s\n", filename);
  int src_fid = open(filename, O_RDONLY);
  if (src_fid < 0) {
    fprintf(stderr, "error opening %s\n", filename);
    return;
  }
  char buf[256 * 1024];
  int offset = 0;
  int len;
  int bytes = 0;
  memset(buf, 0, sizeof(buf));
  while ((len = read(src_fid, buf, sizeof(buf))) > 0) {
    bytes += m5_writefile(buf, len, offset, filename);
    offset += len;
  }
  fprintf(stderr, "written %d bytes\n", bytes);
  close(src_fid);
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40020,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c9 <write_file+0x69>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    127e <write_file+0x11e>
movl   $0x0,-0x40014(%rbp)
movl   $0x0,-0x4001c(%rbp)
lea    -0x40010(%rbp),%rdi
xor    %esi,%esi
mov    $0x40000,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x40010(%rbp),%rsi
mov    $0x40000,%edx
call   1060 <read@plt>
mov    %eax,-0x40018(%rbp)
cmp    $0x0,%eax
jle    1259 <write_file+0xf9>
lea    -0x40010(%rbp),%rdi
mov    -0x40018(%rbp),%esi
mov    -0x40014(%rbp),%edx
mov    -0x8(%rbp),%rcx
call   1030 <m5_writefile@plt>
mov    %rax,%rcx
movslq -0x4001c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x4001c(%rbp)
mov    -0x40018(%rbp),%eax
add    -0x40014(%rbp),%eax
mov    %eax,-0x40014(%rbp)
jmp    11f0 <write_file+0x90>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x4001c(%rbp),%edx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
add    $0x40020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40008,%rsp
mov    %rdi,%r14
mov    0x2e5d(%rip),%rbx
mov    (%rbx),%edi
lea    0xe82(%rip),%rsi
xor    %r13d,%r13d
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     123e <write_file+0xde>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40000,%edx
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x40000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    120a <write_file+0xaa>
mov    %eax,%ebx
xor    %r13d,%r13d
mov    %rsp,%r12
xor    %ebp,%ebp
nopl   (%rax)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <m5_writefile@plt>
add    %eax,%r13d
add    %ebx,%ebp
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x40000,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11e0 <write_file+0x80>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%edi
call   1050 <close@plt>
add    $0x40008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%edi
lea    0xdb9(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
add    $0x40008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"opening %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"error opening %s\0A\00"
@.str.2 = [18 x i8] c"written %d bytes\0A\00"
define void @write_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [262144 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %2
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %2
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %2
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %47
20: ; preds = %1
store i32 0, i32* %5
store i32 0, i32* %7
%21 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%22 = call i32 @memset(i8* %21, i32 0, i32 262144)
br label %23
23: ; preds = %28, %20
%24 = load i32, i32* %3
%25 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 262144)
store i32 %26, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %23
%29 = getelementptr inbounds [262144 x i8], [262144 x i8]* %4, i64 0, i64 0
%30 = load i32, i32* %6
%31 = load i32, i32* %5
%32 = load i8*, i8** %2
%33 = call i64 @m5_writefile(i8* %29, i32 %30, i32 %31, i8* %32)
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = add nsw i64 %35, %33
%37 = trunc i64 %36 to i32
store i32 %37, i32* %7
%38 = load i32, i32* %6
%39 = load i32, i32* %5
%40 = add nsw i32 %39, %38
store i32 %40, i32* %5
br label %23
41: ; preds = %23
%42 = load i32, i32* @stderr
%43 = load i32, i32* %7
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %41, %16
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"opening %s\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [18 x i8] c"error opening %s\0A\00"
@.str.2 = [18 x i8] c"written %d bytes\0A\00"
define void @write_file(i8* %0) {
%2 = alloca [262144 x i8]
%3 = load i32, i32* @stderr
%4 = tail call i32 (i32, i8*, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
11: ; preds = %1
%12 = getelementptr inbounds [262144 x i8], [262144 x i8]* %2, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 262144)
%14 = call i32 @read(i32 %6, i8* %12, i32 262144)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %11, %16
%17 = phi i32 [ %24, %16 ], [ %14, %11 ]
%18 = phi i32 [ %22, %16 ], [ 0, %11 ]
%19 = phi i32 [ %23, %16 ], [ 0, %11 ]
%20 = call i64 @m5_writefile(i8* %12, i32 %17, i32 %19, i8* %0)
%21 = trunc i64 %20 to i32
%22 = add i32 %18, %21
%23 = add nsw i32 %17, %19
%24 = call i32 @read(i32 %6, i8* %12, i32 262144)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %16, label %26
26: ; preds = %16, %11
%27 = phi i32 [ 0, %11 ], [ %22, %16 ]
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%30 = call i32 @close(i32 %6)
br label %31
31: ; preds = %26, %8
ret void
}