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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int read(int, char *, size_t);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
void dump_file(char *name, size_t buffer_size) {
  int rd_fd = 0;
  int wr_fd = 0;
  int read_bytes = 0;
  int write_bytes = 0;
  char buf[buffer_size];
  rd_fd = open(name, O_RDONLY);
  if (rd_fd == -1) {
    printf("open %s failed:%s\n", name, strerror(errno));
    return;
  }
  wr_fd = open("/dev/null", O_WRONLY);
  if (wr_fd == -1) {
    printf("open %s failed:%s\n", name, strerror(errno));
    goto boilout;
  }
  while (1) {
    read_bytes = read(rd_fd, buf, buffer_size);
    if (read_bytes == -1) {
      printf("read failed:%s\n", strerror(errno));
      goto boilout;
    }
    write_bytes = write(wr_fd, buf, read_bytes);
    if (write_bytes == -1 || write_bytes != read_bytes) {
      printf("write failed:%s\n", strerror(errno));
      goto boilout;
    }
    if (read_bytes < buffer_size) {
      break;
    }
  }
boilout:
  if (rd_fd > 0) {
    close(rd_fd);
  }
  if (wr_fd > 0) {
    close(wr_fd);
  }
}


x86 assembly (clang, -O0):
<dump_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1209 <dump_file+0xa9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x34(%rbp)
jmp    133a <dump_file+0x1da>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0xdfa(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    125b <dump_file+0xfb>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130f <dump_file+0x1af>
jmp    1260 <dump_file+0x100>
jmp    1265 <dump_file+0x105>
mov    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12a6 <dump_file+0x146>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130f <dump_file+0x1af>
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     12ce <dump_file+0x16e>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12f2 <dump_file+0x192>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130f <dump_file+0x1af>
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1305 <dump_file+0x1a5>
jmp    130a <dump_file+0x1aa>
jmp    1260 <dump_file+0x100>
jmp    130f <dump_file+0x1af>
cmpl   $0x0,-0x14(%rbp)
jle    1321 <dump_file+0x1c1>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jle    1333 <dump_file+0x1d3>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsp,%r13
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1201 <dump_file+0xa1>
mov    %eax,%r15d
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     1231 <dump_file+0xd1>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     128a <dump_file+0x12a>
mov    %eax,%ebx
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12a1 <dump_file+0x141>
cmp    %ebx,%eax
jne    12a1 <dump_file+0x141>
movslq %ebx,%rax
cmp    %r12,%rax
jae    11c0 <dump_file+0x60>
jmp    1253 <dump_file+0xf3>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdea(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdba(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
test   %r15d,%r15d
jle    1260 <dump_file+0x100>
mov    %r15d,%edi
call   1050 <close@plt>
test   %r14d,%r14d
jle    127b <dump_file+0x11b>
mov    %r14d,%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd7e(%rip),%rdi
jmp    12b6 <dump_file+0x156>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd77(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r15d,%r15d
jg     1258 <dump_file+0xf8>
jmp    1260 <dump_file+0x100>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [19 x i8] c"open %s failed:%s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.2 = [16 x i8] c"read failed:%s\0A\00"
@.str.3 = [17 x i8] c"write failed:%s\0A\00"
define void @dump_file(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%12 = load i64, i64* %4
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %9
%14 = alloca i8, i64 %12
store i64 %12, i64* %10
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %23)
store i32 1, i32* %11
br label %82
25: ; preds = %2
%26 = load i32, i32* @O_WRONLY
%27 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i8*, i8** %3
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %33)
br label %69
35: ; preds = %25
br label %36
36: ; preds = %67, %35
br label %37
37: ; preds = %36
%38 = load i32, i32* %5
%39 = load i64, i64* %4
%40 = call i32 @read(i32 %38, i8* %14, i64 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %69
47: ; preds = %37
%48 = load i32, i32* %6
%49 = load i32, i32* %7
%50 = call i32 @write(i32 %48, i8* %14, i32 %49)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %57, label %53
53: ; preds = %47
%54 = load i32, i32* %8
%55 = load i32, i32* %7
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %61
57: ; preds = %53, %47
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %59)
br label %69
61: ; preds = %53
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = load i64, i64* %4
%65 = icmp ult i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %61
br label %68
67: ; preds = %61
br label %36
68: ; preds = %66
br label %69
69: ; preds = %68, %57, %43, %30
%70 = load i32, i32* %5
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = load i32, i32* %5
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %72, %69
%76 = load i32, i32* %6
%77 = icmp sgt i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %78, %75
store i32 0, i32* %11
br label %82
82: ; preds = %81, %20
%83 = load i8*, i8** %9
%84 = load i32, i32* %11
switch i32 %84, label %86 [
i32 0, label %85
i32 1, label %85
]
85: ; preds = %82, %82
ret void
86: ; preds = %82
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [19 x i8] c"open %s failed:%s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.2 = [16 x i8] c"read failed:%s\0A\00"
@.str.3 = [17 x i8] c"write failed:%s\0A\00"
define void @dump_file(i8* %0, i64 %1) {
%3 = alloca i8, i64 %1
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9)
br label %46
11: ; preds = %2
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %17)
br label %38
19: ; preds = %11, %35
%20 = call i32 @read(i32 %5, i8* %3, i64 %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %38
26: ; preds = %19
%27 = call i32 @write(i32 %13, i8* %3, i32 %20)
%28 = icmp ne i32 %27, -1
%29 = icmp eq i32 %27, %20
%30 = and i1 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %26
%32 = load i32, i32* @errno
%33 = tail call i8* @strerror(i32 %32)
%34 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %38
35: ; preds = %26
%36 = sext i32 %20 to i64
%37 = icmp ult i64 %36, %1
br i1 %37, label %38, label %19
38: ; preds = %35, %31, %22, %15
%39 = icmp sgt i32 %5, 0
br i1 %39, label %40, label %42
40: ; preds = %38
%41 = tail call i32 @close(i32 %5)
br label %42
42: ; preds = %40, %38
%43 = icmp sgt i32 %13, 0
br i1 %43, label %44, label %46
44: ; preds = %42
%45 = tail call i32 @close(i32 %13)
br label %46
46: ; preds = %42, %44, %7
ret void
}