#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 off_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int S_IRUSR;
int S_IWUSR;
unsigned char *calloc(int, int);
int /*<<< orphan*/ ckpt_and_exit();
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit_failure();
int /*<<< orphan*/ fill_chunk(unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
int full_write(int, unsigned char *, int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
char *strerror(int);
void write_a_file(char *file, off_t chunk_size, off_t num_chunks) {
  int fd = -1;
  off_t i;
  unsigned char *chunk = NULL;
  off_t calculated_pos;
  off_t actual_pos;
  srand(0);
  chunk = calloc(chunk_size, 1);
  if (chunk == NULL) {
    printf("ERROR: Out of memory!\n");
    exit_failure();
  }
  fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    printf("ERROR: Failed to open the file '%s' for writing. %d(%s)\n", file,
           errno, strerror(errno));
    exit_failure();
  }
  calculated_pos = 0;
  for (i = 0; i < num_chunks; i++) {
    if ((i % 1024) == 0) {
      fill_chunk(chunk, chunk_size);
      if (full_write(fd, chunk, chunk_size) != chunk_size) {
        printf("ERROR: Failed to write file: %d(%s)\n", errno, strerror(errno));
        exit_failure();
      }
    } else {
      lseek(fd, chunk_size, SEEK_CUR);
    }
    calculated_pos += chunk_size;
    actual_pos = lseek(fd, (off_t)0, SEEK_CUR);
    if (i == 0 || i == ((num_chunks >> 1) - (num_chunks >> 2)) ||
        i == (num_chunks >> 1) ||
        i == ((num_chunks >> 1) + (num_chunks >> 2)) || i == (num_chunks - 1)) {
      printf("Checkpointing at %.0lf%% of the file operation.\n",
             i == 0 ? 0 : ((double)i / (double)num_chunks) * 100.0);
      printf("Skipping checkpoint!\n");
      ckpt_and_exit();
    }
    if (actual_pos != calculated_pos) {
      printf("ERROR: lseek() failed %d(%s) to get current position of "
             "writing file. Expected %lld. Got %lld.\n",
             errno, strerror(errno), (long long int)calculated_pos,
             (long long int)actual_pos);
      exit_failure();
    }
  }
  close(fd);
  free(chunk);
}


x86 assembly (clang, -O0):
<write_a_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
xor    %edi,%edi
call   1080 <srand@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1090 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1219 <write_a_file+0x59>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <exit_failure@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
or     (%rax),%esi
mov    0x2d72(%rip),%rax
or     (%rax),%esi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    129d <write_a_file+0xdd>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <exit_failure@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1474 <write_a_file+0x2b4>
mov    -0x18(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1329 <write_a_file+0x169>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <fill_chunk@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <full_write@plt>
cmp    -0xc(%rbp),%eax
je     1324 <write_a_file+0x164>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <exit_failure@plt>
jmp    133d <write_a_file+0x17d>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2ca2(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0xc(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2c88(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     13ba <write_a_file+0x1fa>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%ecx
sar    $0x1,%ecx
mov    -0x10(%rbp),%edx
sar    $0x2,%edx
sub    %edx,%ecx
cmp    %ecx,%eax
je     13ba <write_a_file+0x1fa>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%ecx
sar    $0x1,%ecx
cmp    %ecx,%eax
je     13ba <write_a_file+0x1fa>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%ecx
sar    $0x1,%ecx
mov    -0x10(%rbp),%edx
sar    $0x2,%edx
add    %edx,%ecx
cmp    %ecx,%eax
je     13ba <write_a_file+0x1fa>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1418 <write_a_file+0x258>
cmpl   $0x0,-0x18(%rbp)
jne    13d1 <write_a_file+0x211>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    13f0 <write_a_file+0x230>
cvtsi2sdl -0x18(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xc19(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
lea    0xc81(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xca4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10d0 <ckpt_and_exit@plt>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1461 <write_a_file+0x2a1>
mov    0x2b95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2b89(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x4c(%rbp),%esi
mov    %rax,%rdx
movslq -0x24(%rbp),%rcx
movslq -0x28(%rbp),%r8
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <exit_failure@plt>
jmp    1466 <write_a_file+0x2a6>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12ab <write_a_file+0xeb>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_a_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r14
xor    %edi,%edi
call   1090 <srand@plt>
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
jne    121a <write_a_file+0x4a>
lea    0xf06(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10c0 <exit_failure@plt>
mov    0x2dd7(%rip),%rax
mov    0x2dc8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d75(%rip),%rax
or     (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
call   10d0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1282 <write_a_file+0xb2>
mov    0x2d64(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10f0 <strerror@plt>
lea    0xda4(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <exit_failure@plt>
test   %r12d,%r12d
jle    136e <write_a_file+0x19e>
mov    0x10(%rsp),%r14
mov    %r14,%rdi
mov    0xc(%rsp),%ebp
mov    %ebp,%esi
call   1070 <fill_chunk@plt>
mov    %ebx,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   10b0 <full_write@plt>
cmp    %ebp,%eax
je     12dc <write_a_file+0x10c>
mov    0x2d07(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10f0 <strerror@plt>
lea    0xd80(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <exit_failure@plt>
mov    0xc(%rsp),%ebx
movslq %ebx,%rax
mov    %rax,0x18(%rsp)
mov    0x2ce9(%rip),%r14
mov    (%r14),%edx
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%ebp
lea    0xd68(%rip),%rdi
xorpd  %xmm0,%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xde7(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10e0 <ckpt_and_exit@plt>
cmp    %ebx,%ebp
je     1364 <write_a_file+0x194>
mov    0x2c91(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   10f0 <strerror@plt>
movslq %ebp,%r8
lea    0xd5b(%rip),%rdi
mov    %r14d,%esi
mov    0x2c8a(%rip),%r14
mov    %rax,%rdx
mov    0x18(%rsp),%rcx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <exit_failure@plt>
cmp    $0x1,%r12d
mov    0x8(%rsp),%ebx
jne    138d <write_a_file+0x1bd>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x10(%rsp),%rdi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    %r12d,%eax
shr    %eax
mov    %r12d,%ecx
shr    $0x2,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %r12d,%xmm0
movsd  %xmm0,0x40(%rsp)
mov    0x18(%rsp),%rdx
lea    (%rdx,%rdx,1),%rbp
neg    %r12d
lea    -0x1(%rax),%edx
mov    %edx,0x30(%rsp)
sub    %ecx,%edx
mov    %edx,0x34(%rsp)
add    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,0x2c(%rsp)
movsd  0xc30(%rip),%xmm0
xor    %r13d,%r13d
movsd  %xmm0,0x20(%rsp)
mov    %r12,0x38(%rsp)
jmp    1413 <write_a_file+0x243>
movsd  0x20(%rsp),%xmm0
addsd  0xc12(%rip),%xmm0
movsd  %xmm0,0x20(%rsp)
add    0x18(%rsp),%rbp
lea    (%r12,%r13,1),%eax
add    $0x1,%eax
mov    %r15d,%r13d
cmp    $0xffffffff,%eax
mov    0x2bcb(%rip),%r14
je     136e <write_a_file+0x19e>
lea    0x1(%r13),%r15d
test   $0x3ff,%r15d
je     1430 <write_a_file+0x260>
mov    (%r14),%edx
mov    %ebx,%edi
mov    0xc(%rsp),%esi
call   1060 <lseek@plt>
jmp    14a0 <write_a_file+0x2d0>
mov    0x10(%rsp),%r14
mov    %r14,%rdi
mov    0xc(%rsp),%ebx
mov    %ebx,%esi
call   1070 <fill_chunk@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    0x2b87(%rip),%r14
mov    %ebx,%edx
call   10b0 <full_write@plt>
cmp    %ebx,%eax
mov    0x8(%rsp),%ebx
je     14a0 <write_a_file+0x2d0>
mov    0x2b59(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10f0 <strerror@plt>
lea    0xbd2(%rip),%rdi
mov    %ebx,%esi
mov    0x8(%rsp),%ebx
mov    0x38(%rsp),%r12
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <exit_failure@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%r14d
cmp    %r13d,0x34(%rsp)
je     14e0 <write_a_file+0x310>
cmp    %r13d,0x30(%rsp)
je     14e0 <write_a_file+0x310>
cmp    %r13d,0x2c(%rsp)
je     14e0 <write_a_file+0x310>
lea    (%r12,%r13,1),%eax
cmp    $0xfffffffe,%eax
je     14e0 <write_a_file+0x310>
cmp    %r14d,%ebp
je     13e0 <write_a_file+0x210>
jmp    151e <write_a_file+0x34e>
nopl   0x0(%rax,%rax,1)
movsd  0x20(%rsp),%xmm0
divsd  0x40(%rsp),%xmm0
mulsd  0xb14(%rip),%xmm0
lea    0xb73(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xbf6(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10e0 <ckpt_and_exit@plt>
cmp    %r14d,%ebp
je     13e0 <write_a_file+0x210>
mov    0x2a9b(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10f0 <strerror@plt>
movslq %r14d,%r8
lea    0xb67(%rip),%rdi
mov    %ebx,%esi
mov    0x8(%rsp),%ebx
mov    0x38(%rsp),%r12
mov    %rax,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10c0 <exit_failure@plt>
jmp    13e0 <write_a_file+0x210>

LLVM IR (-O0):
@.str = [23 x i8] c"ERROR: Out of memory!\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [57 x i8] c"ERROR: Failed to open the file '%s' for writing. %d(%s)\0A\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"ERROR: Failed to write file: %d(%s)\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [49 x i8] c"Checkpointing at %.0lf%% of the file operation.\0A\00"
@.str.4 = [22 x i8] c"Skipping checkpoint!\0A\00"
@.str.5 = [96 x i8] c"ERROR: lseek() failed %d(%s) to get current position of writing file. Expected %lld. Got %lld.\0A\00"
define void @write_a_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 -1, i32* %7
store i8* null, i8** %9
%12 = call i32 @srand(i32 0)
%13 = load i32, i32* %5
%14 = call i8* @calloc(i32 %13, i32 1)
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%19 = call i32 (...) @exit_failure()
br label %20
20: ; preds = %17, %3
%21 = load i8*, i8** %4
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @O_WRONLY
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %21, i32 %26, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %20
%34 = load i8*, i8** %4
%35 = load i32, i32* @errno
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %34, i32 %35, i8* %37)
%39 = call i32 (...) @exit_failure()
br label %40
40: ; preds = %33, %20
store i32 0, i32* %10
store i32 0, i32* %8
br label %41
41: ; preds = %137, %40
%42 = load i32, i32* %8
%43 = load i32, i32* %6
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %140
45: ; preds = %41
%46 = load i32, i32* %8
%47 = srem i32 %46, 1024
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %66
49: ; preds = %45
%50 = load i8*, i8** %9
%51 = load i32, i32* %5
%52 = call i32 @fill_chunk(i8* %50, i32 %51)
%53 = load i32, i32* %7
%54 = load i8*, i8** %9
%55 = load i32, i32* %5
%56 = call i32 @full_write(i32 %53, i8* %54, i32 %55)
%57 = load i32, i32* %5
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %65
59: ; preds = %49
%60 = load i32, i32* @errno
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %60, i8* %62)
%64 = call i32 (...) @exit_failure()
br label %65
65: ; preds = %59, %49
br label %71
66: ; preds = %45
%67 = load i32, i32* %7
%68 = load i32, i32* %5
%69 = load i32, i32* @SEEK_CUR
%70 = call i32 @lseek(i32 %67, i32 %68, i32 %69)
br label %71
71: ; preds = %66, %65
%72 = load i32, i32* %5
%73 = load i32, i32* %10
%74 = add nsw i32 %73, %72
store i32 %74, i32* %10
%75 = load i32, i32* %7
%76 = load i32, i32* @SEEK_CUR
%77 = call i32 @lseek(i32 %75, i32 0, i32 %76)
store i32 %77, i32* %11
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, 0
br i1 %79, label %106, label %80
80: ; preds = %71
%81 = load i32, i32* %8
%82 = load i32, i32* %6
%83 = ashr i32 %82, 1
%84 = load i32, i32* %6
%85 = ashr i32 %84, 2
%86 = sub nsw i32 %83, %85
%87 = icmp eq i32 %81, %86
br i1 %87, label %106, label %88
88: ; preds = %80
%89 = load i32, i32* %8
%90 = load i32, i32* %6
%91 = ashr i32 %90, 1
%92 = icmp eq i32 %89, %91
br i1 %92, label %106, label %93
93: ; preds = %88
%94 = load i32, i32* %8
%95 = load i32, i32* %6
%96 = ashr i32 %95, 1
%97 = load i32, i32* %6
%98 = ashr i32 %97, 2
%99 = add nsw i32 %96, %98
%100 = icmp eq i32 %94, %99
br i1 %100, label %106, label %101
101: ; preds = %93
%102 = load i32, i32* %8
%103 = load i32, i32* %6
%104 = sub nsw i32 %103, 1
%105 = icmp eq i32 %102, %104
br i1 %105, label %106, label %122
106: ; preds = %101, %93, %88, %80, %71
%107 = load i32, i32* %8
%108 = icmp eq i32 %107, 0
br i1 %108, label %109, label %110
109: ; preds = %106
br label %117
110: ; preds = %106
%111 = load i32, i32* %8
%112 = sitofp i32 %111 to double
%113 = load i32, i32* %6
%114 = sitofp i32 %113 to double
%115 = fdiv double %112, %114
%116 = fmul double %115, 1.000000e+02
br label %117
117: ; preds = %110, %109
%118 = phi double [ 0.000000e+00, %109 ], [ %116, %110 ]
%119 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), double %118)
%120 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%121 = call i32 (...) @ckpt_and_exit()
br label %122
122: ; preds = %117, %101
%123 = load i32, i32* %11
%124 = load i32, i32* %10
%125 = icmp ne i32 %123, %124
br i1 %125, label %126, label %136
126: ; preds = %122
%127 = load i32, i32* @errno
%128 = load i32, i32* @errno
%129 = call i8* @strerror(i32 %128)
%130 = load i32, i32* %10
%131 = sext i32 %130 to i64
%132 = load i32, i32* %11
%133 = sext i32 %132 to i64
%134 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([96 x i8], [96 x i8]* @.str.5, i64 0, i64 0), i32 %127, i8* %129, i64 %131, i64 %133)
%135 = call i32 (...) @exit_failure()
br label %136
136: ; preds = %126, %122
br label %137
137: ; preds = %136
%138 = load i32, i32* %8
%139 = add nsw i32 %138, 1
store i32 %139, i32* %8
br label %41
140: ; preds = %41
%141 = load i32, i32* %7
%142 = call i32 @close(i32 %141)
%143 = load i8*, i8** %9
%144 = call i32 @free(i8* %143)
ret void
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [57 x i8] c"ERROR: Failed to open the file '%s' for writing. %d(%s)\0A\00"
@errno = global i32 0
@.str.2 = [37 x i8] c"ERROR: Failed to write file: %d(%s)\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [49 x i8] c"Checkpointing at %.0lf%% of the file operation.\0A\00"
@.str.5 = [96 x i8] c"ERROR: lseek() failed %d(%s) to get current position of writing file. Expected %lld. Got %lld.\0A\00"
@str = [21 x i8] c"Skipping checkpoint!\00"
@str.6 = [22 x i8] c"ERROR: Out of memory!\00"
define void @write_a_file(i8* %0, i32 %1, i32 %2) {
%4 = tail call i32 @srand(i32 0)
%5 = tail call i8* @calloc(i32 %1, i32 1)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
%9 = tail call i32 (...) @exit_failure()
br label %10
10: ; preds = %7, %3
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %12, %11
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %17, %16
%19 = tail call i32 @open(i8* %0, i32 %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %10
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %22, i8* %23)
%25 = tail call i32 (...) @exit_failure()
br label %26
26: ; preds = %21, %10
%27 = icmp sgt i32 %2, 0
br i1 %27, label %28, label %106
28: ; preds = %26
%29 = lshr i32 %2, 1
%30 = lshr i32 %2, 2
%31 = sub nsw i32 %29, %30
%32 = add nuw nsw i32 %29, %30
%33 = add nsw i32 %2, -1
%34 = sitofp i32 %2 to double
%35 = sext i32 %1 to i64
%36 = tail call i32 @fill_chunk(i8* %5, i32 %1)
%37 = tail call i32 @full_write(i32 %19, i8* %5, i32 %1)
%38 = icmp eq i32 %37, %1
br i1 %38, label %44, label %39
39: ; preds = %28
%40 = load i32, i32* @errno
%41 = tail call i8* @strerror(i32 %40)
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %40, i8* %41)
%43 = tail call i32 (...) @exit_failure()
br label %44
44: ; preds = %39, %28
%45 = load i32, i32* @SEEK_CUR
%46 = tail call i32 @lseek(i32 %19, i32 0, i32 %45)
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), double 0.000000e+00)
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%49 = tail call i32 (...) @ckpt_and_exit()
%50 = icmp eq i32 %46, %1
br i1 %50, label %57, label %51
51: ; preds = %44
%52 = load i32, i32* @errno
%53 = tail call i8* @strerror(i32 %52)
%54 = sext i32 %46 to i64
%55 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([96 x i8], [96 x i8]* @.str.5, i64 0, i64 0), i32 %52, i8* %53, i64 %35, i64 %54)
%56 = tail call i32 (...) @exit_failure()
br label %57
57: ; preds = %51, %44
%58 = icmp eq i32 %2, 1
br i1 %58, label %106, label %59
59: ; preds = %57, %103
%60 = phi i64 [ %77, %103 ], [ %35, %57 ]
%61 = phi i32 [ %104, %103 ], [ 1, %57 ]
%62 = and i32 %61, 1023
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %73
64: ; preds = %59
%65 = tail call i32 @fill_chunk(i8* %5, i32 %1)
%66 = tail call i32 @full_write(i32 %19, i8* %5, i32 %1)
%67 = icmp eq i32 %66, %1
br i1 %67, label %76, label %68
68: ; preds = %64
%69 = load i32, i32* @errno
%70 = tail call i8* @strerror(i32 %69)
%71 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %69, i8* %70)
%72 = tail call i32 (...) @exit_failure()
br label %76
73: ; preds = %59
%74 = load i32, i32* @SEEK_CUR
%75 = tail call i32 @lseek(i32 %19, i32 %1, i32 %74)
br label %76
76: ; preds = %73, %68, %64
%77 = add nsw i64 %60, %35
%78 = load i32, i32* @SEEK_CUR
%79 = tail call i32 @lseek(i32 %19, i32 0, i32 %78)
%80 = icmp eq i32 %61, %31
%81 = icmp eq i32 %61, %29
%82 = or i1 %80, %81
%83 = icmp eq i32 %61, %32
%84 = select i1 %82, i1 true, i1 %83
%85 = icmp eq i32 %61, %33
%86 = select i1 %84, i1 true, i1 %85
br i1 %86, label %87, label %94
87: ; preds = %76
%88 = sitofp i32 %61 to double
%89 = fdiv double %88, %34
%90 = fmul double %89, 1.000000e+02
%91 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), double %90)
%92 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%93 = tail call i32 (...) @ckpt_and_exit()
br label %94
94: ; preds = %76, %87
%95 = trunc i64 %77 to i32
%96 = icmp eq i32 %79, %95
br i1 %96, label %103, label %97
97: ; preds = %94
%98 = load i32, i32* @errno
%99 = tail call i8* @strerror(i32 %98)
%100 = sext i32 %79 to i64
%101 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([96 x i8], [96 x i8]* @.str.5, i64 0, i64 0), i32 %98, i8* %99, i64 %77, i64 %100)
%102 = tail call i32 (...) @exit_failure()
br label %103
103: ; preds = %94, %97
%104 = add nuw nsw i32 %61, 1
%105 = icmp eq i32 %104, %2
br i1 %105, label %106, label %59
106: ; preds = %103, %57, %26
%107 = tail call i32 @close(i32 %19)
%108 = tail call i32 @free(i8* %5)
ret void
}