#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, size_t, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
void write_test(int should_seek, unsigned long file_size) {
  FILE *fp;
  unsigned long i;
  char c;
  size_t nmemb, size;
  printf("Will write %lu bytes.\n", file_size);
  printf("Will %sseek before each write.\n", should_seek ? "" : "not ");
  fp = NULL;
  if ((fp = fopen("/tmp/junk.bin", "w+b")) == NULL) {
    fprintf(stderr, "Could not open /tmp/junk.bin for writing.\n");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < file_size; i++) {
    if (i % 1024 == 0) {
      printf("\r%lu of %lu bytes written ...", i, file_size);
    }
    if (should_seek) {
      fseek(fp, 0, SEEK_CUR);
    }
    size = 1;
    nmemb = 1;
    c = '0' + ((char)(i % 10));
    fwrite(&c, size, nmemb, fp);
  }
  printf("\r%lu of %lu bytes written.    \n", i, file_size);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%ecx
lea    0xe9c(%rip),%rsi
lea    0xf14(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
lea    0xe76(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1203 <write_test+0x93>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12b0 <write_test+0x140>
mov    -0x20(%rbp),%rax
and    $0x3ff,%rax
cmp    $0x0,%rax
jne    1243 <write_test+0xd3>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x4(%rbp)
je     1261 <write_test+0xf1>
mov    -0x18(%rbp),%rdi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
movq   $0x1,-0x38(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %dl,%al
movsbl %al,%eax
add    $0x30,%eax
mov    %al,-0x21(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x21(%rbp),%rdi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    120b <write_test+0x9b>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r14d
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r14d,%r14d
lea    0xe9e(%rip),%rax
lea    0xf16(%rip),%rsi
cmove  %rax,%rsi
lea    0xe6c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe83(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     127d <write_test+0x10d>
mov    %rax,%r13
test   %r12,%r12
je     1253 <write_test+0xe3>
movabs $0xcccccccccccccccd,%r15
cc cc cc
xor    %ebx,%ebx
jmp    1206 <write_test+0x96>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
or     $0x30,%ebp
mov    %ebp,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
je     1253 <write_test+0xe3>
mov    %rbx,%rax
mul    %r15
mov    %rdx,%rbp
shr    $0x3,%rbp
add    %ebp,%ebp
test   $0x3ff,%ebx
jne    1231 <write_test+0xc1>
lea    0xe55(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x0(%rbp,%rbp,4),%eax
mov    %ebx,%ebp
sub    %eax,%ebp
test   %r14d,%r14d
je     11f0 <write_test+0x80>
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
jmp    11f0 <write_test+0x80>
lea    0xe3d(%rip),%rdi
mov    %r12,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Will write %lu bytes.\0A\00"
@.str.1 = [32 x i8] c"Will %sseek before each write.\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [5 x i8] c"not \00"
@.str.4 = [14 x i8] c"/tmp/junk.bin\00"
@.str.5 = [4 x i8] c"w+b\00"
@stderr = global i32 0
@.str.6 = [43 x i8] c"Could not open /tmp/junk.bin for writing.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [30 x i8] c"\0D%lu of %lu bytes written ...\00"
@SEEK_CUR = global i32 0
@.str.8 = [32 x i8] c"\0D%lu of %lu bytes written. \0A\00"
define void @write_test(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
%10 = load i64, i64* %4
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %10)
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 0
%14 = zext i1 %13 to i64
%15 = select i1 %13, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32* null, i32** %5
%17 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
store i32* %17, i32** %5
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
store i64 0, i64* %6
br label %25
25: ; preds = %55, %24
%26 = load i64, i64* %6
%27 = load i64, i64* %4
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %58
29: ; preds = %25
%30 = load i64, i64* %6
%31 = urem i64 %30, 1024
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i64, i64* %6
%35 = load i64, i64* %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i64 %34, i64 %35)
br label %37
37: ; preds = %33, %29
%38 = load i32, i32* %3
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32*, i32** %5
%42 = load i32, i32* @SEEK_CUR
%43 = call i32 @fseek(i32* %41, i32 0, i32 %42)
br label %44
44: ; preds = %40, %37
store i64 1, i64* %9
store i64 1, i64* %8
%45 = load i64, i64* %6
%46 = urem i64 %45, 10
%47 = trunc i64 %46 to i8
%48 = sext i8 %47 to i32
%49 = add nsw i32 48, %48
%50 = trunc i32 %49 to i8
store i8 %50, i8* %7
%51 = load i64, i64* %9
%52 = load i64, i64* %8
%53 = load i32*, i32** %5
%54 = call i32 @fwrite(i8* %7, i64 %51, i64 %52, i32* %53)
br label %55
55: ; preds = %44
%56 = load i64, i64* %6
%57 = add i64 %56, 1
store i64 %57, i64* %6
br label %25
58: ; preds = %25
%59 = load i64, i64* %6
%60 = load i64, i64* %4
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i64 %59, i64 %60)
%62 = load i32*, i32** %5
%63 = call i32 @fclose(i32* %62)
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"Will write %lu bytes.\0A\00"
@.str.1 = [32 x i8] c"Will %sseek before each write.\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [5 x i8] c"not \00"
@.str.4 = [14 x i8] c"/tmp/junk.bin\00"
@.str.5 = [4 x i8] c"w+b\00"
@stderr = global i32 0
@.str.6 = [43 x i8] c"Could not open /tmp/junk.bin for writing.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [30 x i8] c"\0D%lu of %lu bytes written ...\00"
@SEEK_CUR = global i32 0
@.str.8 = [32 x i8] c"\0D%lu of %lu bytes written. \0A\00"
define void @write_test(i32 %0, i64 %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i64 %1)
%4 = icmp eq i32 %0, 0
%5 = select i1 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = icmp eq i64 %1, 0
br i1 %10, label %33, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %9, %26
%17 = phi i64 [ %31, %26 ], [ 0, %9 ]
%18 = and i64 %17, 1023
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i64 %17, i64 %1)
br label %22
22: ; preds = %20, %16
br i1 %4, label %26, label %23
23: ; preds = %22
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @fseek(i32* %7, i32 0, i32 %24)
br label %26
26: ; preds = %23, %22
%27 = urem i64 %17, 10
%28 = trunc i64 %27 to i32
%29 = or i32 %28, 48
%30 = tail call i32 @fputc(i32 %29, i32* %7)
%31 = add nuw i64 %17, 1
%32 = icmp eq i64 %31, %1
br i1 %32, label %33, label %16
33: ; preds = %26, %9
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i64 %1, i64 %1)
%35 = tail call i32 @fclose(i32* %7)
ret void
}