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


x86 assembly (clang, -O0):
<read_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%ecx
lea    0xe92(%rip),%rsi
lea    0xf41(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1205 <read_test+0x95>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1333 <read_test+0x1c3>
mov    -0x20(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %dl,%al
movsbl %al,%eax
add    $0x30,%eax
mov    %al,-0x22(%rbp)
mov    -0x20(%rbp),%rax
and    $0x3ff,%rax
cmp    $0x0,%rax
jne    125e <read_test+0xee>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x4(%rbp)
je     127c <read_test+0x10c>
mov    -0x18(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
movq   $0x1,-0x38(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x21(%rbp),%rdi
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     12e0 <read_test+0x170>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movsbl -0x21(%rbp),%eax
movsbl -0x22(%rbp),%ecx
cmp    %ecx,%eax
je     131d <read_test+0x1ad>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
movsbl -0x22(%rbp),%edx
movsbl -0x21(%rbp),%ecx
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    1322 <read_test+0x1b2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    120d <read_test+0x9d>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %edi,%r14d
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
lea    0xe9c(%rip),%rax
lea    0xf4b(%rip),%rsi
cmove  %rax,%rsi
lea    0xe6b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe81(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12fb <read_test+0x18b>
mov    %rax,%rbp
test   %r13,%r13
je     128b <read_test+0x11b>
lea    0x7(%rsp),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rax
movabs $0xcccccccccccccccd,%rcx
cc cc cc
mul    %rcx
mov    %rdx,%r15
test   $0x3ff,%ebx
je     1270 <read_test+0x100>
test   %r14d,%r14d
je     1223 <read_test+0xb3>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rdi
mov    %rbp,%rcx
call   1030 <fread@plt>
cmp    $0x1,%rax
jne    12b5 <read_test+0x145>
shr    $0x3,%r15
add    %r15d,%r15d
lea    (%r15,%r15,4),%eax
mov    %ebx,%edx
sub    %eax,%edx
or     $0x30,%edx
movsbl 0x7(%rsp),%ecx
cmp    %ecx,%edx
jne    12d6 <read_test+0x166>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11f0 <read_test+0x80>
jmp    128b <read_test+0x11b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe00(%rip),%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
jne    1210 <read_test+0xa0>
jmp    1223 <read_test+0xb3>
lea    0xe3d(%rip),%rdi
mov    %r13,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
mov    0x2d1c(%rip),%rcx
mov    (%rcx),%edi
lea    0xdcd(%rip),%rsi
mov    $0x1,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12ed <read_test+0x17d>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12ed <read_test+0x17d>

LLVM IR (-O0):
@.str = [22 x i8] c"Will read %lu bytes.\0A\00"
@.str.1 = [31 x i8] c"Will %sseek before each read.\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"r+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 = [27 x i8] c"\0D%lu of %lu bytes read ...\00"
@SEEK_CUR = global i32 0
@.str.8 = [36 x i8] c"Expected to read %lu but read %lu.\0A\00"
@.str.9 = [25 x i8] c"Expected %c but was %c.\0A\00"
@.str.10 = [29 x i8] c"\0D%lu of %lu bytes read. \0A\00"
define void @read_test(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8
%8 = alloca i8
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
store i32* null, i32** %5
%12 = load i64, i64* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %12)
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
%16 = zext i1 %15 to i64
%17 = select i1 %15, 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)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = 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* %19, i32** %5
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %2
store i64 0, i64* %6
br label %27
27: ; preds = %83, %26
%28 = load i64, i64* %6
%29 = load i64, i64* %4
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %86
31: ; preds = %27
%32 = load i64, i64* %6
%33 = urem i64 %32, 10
%34 = trunc i64 %33 to i8
%35 = sext i8 %34 to i32
%36 = add nsw i32 48, %35
%37 = trunc i32 %36 to i8
store i8 %37, i8* %8
%38 = load i64, i64* %6
%39 = urem i64 %38, 1024
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %31
%42 = load i64, i64* %6
%43 = load i64, i64* %4
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i64 %42, i64 %43)
br label %45
45: ; preds = %41, %31
%46 = load i32, i32* %3
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i32*, i32** %5
%50 = load i32, i32* @SEEK_CUR
%51 = call i32 @fseek(i32* %49, i32 0, i32 %50)
br label %52
52: ; preds = %48, %45
store i64 1, i64* %10
store i64 1, i64* %9
%53 = load i64, i64* %10
%54 = load i64, i64* %9
%55 = load i32*, i32** %5
%56 = call i64 @fread(i8* %7, i64 %53, i64 %54, i32* %55)
store i64 %56, i64* %11
%57 = load i64, i64* %11
%58 = load i64, i64* %9
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %67
60: ; preds = %52
%61 = load i32, i32* @stderr
%62 = load i64, i64* %9
%63 = load i64, i64* %11
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i64 %62, i64 %63)
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %52
%68 = load i8, i8* %7
%69 = sext i8 %68 to i32
%70 = load i8, i8* %8
%71 = sext i8 %70 to i32
%72 = icmp ne i32 %69, %71
br i1 %72, label %73, label %82
73: ; preds = %67
%74 = load i32, i32* @stderr
%75 = load i8, i8* %8
%76 = sext i8 %75 to i32
%77 = load i8, i8* %7
%78 = sext i8 %77 to i32
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0), i32 %76, i32 %78)
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %67
br label %83
83: ; preds = %82
%84 = load i64, i64* %6
%85 = add i64 %84, 1
store i64 %85, i64* %6
br label %27
86: ; preds = %27
%87 = load i64, i64* %6
%88 = load i64, i64* %4
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i64 %87, i64 %88)
%90 = load i32*, i32** %5
%91 = call i32 @fclose(i32* %90)
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Will read %lu bytes.\0A\00"
@.str.1 = [31 x i8] c"Will %sseek before each read.\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"r+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 = [27 x i8] c"\0D%lu of %lu bytes read ...\00"
@SEEK_CUR = global i32 0
@.str.8 = [36 x i8] c"Expected to read %lu but read %lu.\0A\00"
@.str.9 = [25 x i8] c"Expected %c but was %c.\0A\00"
@.str.10 = [29 x i8] c"\0D%lu of %lu bytes read. \0A\00"
define void @read_test(i32 %0, i64 %1) {
%3 = alloca i8
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %1)
%5 = icmp eq i32 %0, 0
%6 = select i1 %5, 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)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = 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))
%9 = icmp eq i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = icmp eq i64 %1, 0
br i1 %11, label %51, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %10, %48
%18 = phi i64 [ %49, %48 ], [ 0, %10 ]
%19 = urem i64 %18, 10
%20 = trunc i64 %19 to i32
%21 = or i32 %20, 48
%22 = and i64 %18, 1023
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i64 %18, i64 %1)
br label %26
26: ; preds = %24, %17
br i1 %5, label %30, label %27
27: ; preds = %26
%28 = load i32, i32* @SEEK_CUR
%29 = tail call i32 @fseek(i32* %8, i32 0, i32 %28)
br label %30
30: ; preds = %27, %26
%31 = call i64 @fread(i8* %3, i64 1, i64 1, i32* %8)
%32 = icmp eq i64 %31, 1
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), i64 1, i64 %31)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
%39 = load i8, i8* %3
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %21, %40
br i1 %41, label %48, label %42
42: ; preds = %38
%43 = sext i8 %39 to i32
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0), i32 %21, i32 %43)
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %38
%49 = add nuw i64 %18, 1
%50 = icmp eq i64 %49, %1
br i1 %50, label %51, label %17
51: ; preds = %48, %10
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i64 %1, i64 %1)
%53 = tail call i32 @fclose(i32* %8)
ret void
}