#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 atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, size_t);
int /*<<< orphan*/ puts(char *);
size_t read(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    puts("Usage: read granularity file\n");
    return -1;
  }
  int granularity = atoi(argv[1]);
  int fd = open(argv[2], O_RDONLY);
  if (fd == -1) {
    puts("Bad file.");
    return -2;
  }
  char buf[granularity];
  size_t b;
  while (b = read(fd, buf, granularity)) {
    printf("read %zu bytes\n", b);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1198 <main+0x38>
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <main+0xea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e3 <main+0x83>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124a <main+0xea>
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
call   1060 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     123b <main+0xdb>
mov    -0x30(%rbp),%rsi
lea    0xdf7(%rip),%rdi
call   1040 <printf@plt>
jmp    1209 <main+0xa9>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    120d <main+0xad>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0xc0>
mov    %eax,%r15d
mov    %rsp,%r13
mov    %r14d,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
test   %rax,%rax
je     11fe <main+0x9e>
lea    0xe53(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
test   %rax,%rax
jne    11e0 <main+0x80>
mov    %r15d,%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %r13,%rsp
jmp    1231 <main+0xd1>
lea    0xdec(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1231 <main+0xd1>
lea    0xdf7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Usage: read granularity file\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"Bad file.\00"
@.str.2 = [16 x i8] c"read %zu bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @puts(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %46
29: ; preds = %15
%30 = load i32, i32* %6
%31 = zext i32 %30 to i64
%32 = call i8* @llvm.stacksave()
store i8* %32, i8** %8
%33 = alloca i8, i64 %31
store i64 %31, i64* %9
br label %34
34: ; preds = %39, %29
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = call i64 @read(i32 %35, i8* %33, i32 %36)
store i64 %37, i64* %10
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i64, i64* %10
%41 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %40)
br label %34
42: ; preds = %34
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
%45 = load i8*, i8** %8
br label %46
46: ; preds = %42, %27, %13
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [30 x i8] c"Usage: read granularity file\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"Bad file.\00"
@.str.2 = [16 x i8] c"read %zu bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %30
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %30
17: ; preds = %6
%18 = zext i32 %9 to i64
%19 = tail call i8* @llvm.stacksave()
%20 = alloca i8, i64 %18
%21 = call i64 @read(i32 %13, i8* %20, i32 %9)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %17, %23
%24 = phi i64 [ %26, %23 ], [ %21, %17 ]
%25 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i64 %24)
%26 = call i64 @read(i32 %13, i8* %20, i32 %9)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %23
28: ; preds = %23, %17
%29 = tail call i32 @close(i32 %13)
tail call void @llvm.stackrestore(i8* %19)
br label %30
30: ; preds = %15, %28, %4
%31 = phi i32 [ -1, %4 ], [ -2, %15 ], [ 0, %28 ]
ret i32 %31
}