#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
void do_io(char *burnpath, char *burnbuf, int iosize, int nread, int nwrite) {
  int ix;
  FILE *fd = NULL;
  fd = fopen(burnpath, "w");
  if (!fd) {
    perror("fopen for write");
  }
  for (ix = 0; ix < nwrite; ix++) {
    if (!fwrite(burnbuf, iosize, 1, fd)) {
      perror("fwrite");
    }
  }
  fclose(fd);
  fd = fopen(burnpath, "r");
  if (!fd) {
    perror("fopen for read");
  }
  for (ix = 0; ix < nread; ix++) {
    if (!fread(burnbuf, iosize, 1, fd)) {
      perror("fread");
    }
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<do_io>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <do_io+0x4d>
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ed <do_io+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x0,%eax
jne    11da <do_io+0x8a>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
jmp    11df <do_io+0x8f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a4 <do_io+0x54>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1221 <do_io+0xd1>
lea    0xdff(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1271 <do_io+0x121>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    125e <do_io+0x10e>
lea    0xdd1(%rip),%rdi
call   1060 <perror@plt>
jmp    1263 <do_io+0x113>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1228 <do_io+0xd8>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_io>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
lea    0xe90(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
je     11e6 <do_io+0x96>
test   %ebp,%ebp
jg     1209 <do_io+0xb9>
mov    %r13,%rdi
call   1040 <fclose@plt>
lea    0xe85(%rip),%rsi
mov    %r15,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
je     122e <do_io+0xde>
test   %ebx,%ebx
jle    1242 <do_io+0xf2>
lea    0xe73(%rip),%r15
jmp    11c5 <do_io+0x75>
nopl   0x0(%rax)
add    $0xffffffff,%ebx
je     1242 <do_io+0xf2>
mov    %r12,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jne    11c0 <do_io+0x70>
mov    %r15,%rdi
call   1060 <perror@plt>
jmp    11c0 <do_io+0x70>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
test   %ebp,%ebp
jg     1209 <do_io+0xb9>
jmp    1185 <do_io+0x35>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
je     1185 <do_io+0x35>
mov    %r12,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
test   %eax,%eax
jne    1200 <do_io+0xb0>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
jmp    1200 <do_io+0xb0>
lea    0xde6(%rip),%rdi
call   1060 <perror@plt>
test   %ebx,%ebx
jg     11b0 <do_io+0x60>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [16 x i8] c"fopen for write\00"
@.str.2 = [7 x i8] c"fwrite\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"fopen for read\00"
@.str.5 = [6 x i8] c"fread\00"
define void @do_io(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32* null, i32** %12
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %5
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %5
store i32 0, i32* %11
br label %20
20: ; preds = %33, %19
%21 = load i32, i32* %11
%22 = load i32, i32* %10
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %36
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = load i32*, i32** %12
%28 = call i32 @fwrite(i8* %25, i32 %26, i32 1, i32* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
br label %33
33: ; preds = %32
%34 = load i32, i32* %11
%35 = add nsw i32 %34, 1
store i32 %35, i32* %11
br label %20
36: ; preds = %20
%37 = load i32*, i32** %12
%38 = call i32 @fclose(i32* %37)
%39 = load i8*, i8** %6
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %40, i32** %12
%41 = load i32*, i32** %12
%42 = icmp ne i32* %41, null
br i1 %42, label %45, label %43
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
store i32 0, i32* %11
br label %46
46: ; preds = %59, %45
%47 = load i32, i32* %11
%48 = load i32, i32* %9
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %62
50: ; preds = %46
%51 = load i8*, i8** %7
%52 = load i32, i32* %8
%53 = load i32*, i32** %12
%54 = call i32 @fread(i8* %51, i32 %52, i32 1, i32* %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %50
br label %59
59: ; preds = %58
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 1
store i32 %61, i32* %11
br label %46
62: ; preds = %46
%63 = load i32*, i32** %12
%64 = call i32 @fclose(i32* %63)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [16 x i8] c"fopen for write\00"
@.str.2 = [7 x i8] c"fwrite\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"fopen for read\00"
@.str.5 = [6 x i8] c"fread\00"
define void @do_io(i8* nocapture %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %5
%11 = icmp sgt i32 %4, 0
br i1 %11, label %12, label %21
12: ; preds = %10, %18
%13 = phi i32 [ %19, %18 ], [ 0, %10 ]
%14 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %6)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %12, %16
%19 = add nuw nsw i32 %13, 1
%20 = icmp eq i32 %19, %4
br i1 %20, label %21, label %12
21: ; preds = %18, %10
%22 = tail call i32 @fclose(i32* %6)
%23 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = icmp sgt i32 %3, 0
br i1 %28, label %29, label %38
29: ; preds = %27, %35
%30 = phi i32 [ %36, %35 ], [ 0, %27 ]
%31 = tail call i32 @fread(i8* %1, i32 %2, i32 1, i32* %23)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %29, %33
%36 = add nuw nsw i32 %30, 1
%37 = icmp eq i32 %36, %3
br i1 %37, label %38, label %29
38: ; preds = %35, %27
%39 = tail call i32 @fclose(i32* %23)
ret void
}