#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned long long);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int checksize_main(int argc, char **argv) {
  char buf[512];
  int e, fd, cc;
  off_t cur_sect, next_sect, incr_sect;
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    e = errno;
    fprintf(stderr, "open %s: %d (%s)\n", argv[1], e, strerror(e));
    return 1;
  }
  incr_sect = (1ULL * 1024ULL * 1024ULL * 1024ULL) / 512ULL;
  cur_sect = 0;
  while (1) {
    next_sect = cur_sect + incr_sect;
    lseek(fd, (next_sect * (off_t)512), SEEK_SET);
    errno = 0;
    cc = read(fd, buf, 512);
    if (cc == 512) {
      cur_sect = next_sect;
    } else {
      if (incr_sect <= 1)
        break;
      incr_sect /= 2;
    }
  }
  printf("the size is %lld\n", (unsigned long long)(cur_sect + 1));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<checksize_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x218(%rbp)
cmpl   $0x0,-0x218(%rbp)
jge    121d <checksize_main+0xad>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x214(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x23c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x238(%rbp)
mov    -0x214(%rbp),%eax
mov    %eax,-0x22c(%rbp)
mov    -0x214(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x23c(%rbp),%edi
mov    -0x238(%rbp),%rdx
mov    -0x22c(%rbp),%ecx
mov    %rax,%r8
lea    0xdf4(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <checksize_main+0x190>
movl   $0x200000,-0x228(%rbp)
movl   $0x0,-0x220(%rbp)
mov    -0x220(%rbp),%eax
add    -0x228(%rbp),%eax
mov    %eax,-0x224(%rbp)
mov    -0x218(%rbp),%edi
mov    -0x224(%rbp),%esi
shl    $0x9,%esi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    0x2d69(%rip),%rax
movl   $0x0,(%rax)
mov    -0x218(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x200,-0x21c(%rbp)
jne    12ab <checksize_main+0x13b>
mov    -0x224(%rbp),%eax
mov    %eax,-0x220(%rbp)
jmp    12d1 <checksize_main+0x161>
cmpl   $0x1,-0x228(%rbp)
jg     12bd <checksize_main+0x14d>
jmp    12d6 <checksize_main+0x166>
mov    -0x228(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x228(%rbp)
jmp    1231 <checksize_main+0xc1>
mov    -0x220(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rsi
lea    0xd29(%rip),%rdi
call   1030 <printf@plt>
mov    -0x218(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksize_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1214 <checksize_main+0xa4>
mov    %eax,%ebx
mov    $0x200000,%ebp
xor    %r15d,%r15d
mov    0x2e25(%rip),%r13
mov    %rsp,%r14
xchg   %ax,%ax
lea    (%r15,%rbp,1),%r12d
mov    %r12d,%esi
shl    $0x9,%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <lseek@plt>
movl   $0x0,0x0(%r13)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
cmp    $0x200,%eax
je     11f1 <checksize_main+0x81>
cmp    $0x2,%ebp
jb     11f6 <checksize_main+0x86>
shr    %ebp
jmp    11b0 <checksize_main+0x40>
mov    %r12d,%r15d
jmp    11b0 <checksize_main+0x40>
add    $0x1,%r15d
movslq %r15d,%rsi
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    124c <checksize_main+0xdc>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebx
mov    0x2db4(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%r14),%r14
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"open %s: %d (%s)\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [18 x i8] c"the size is %lld\0A\00"
define i32 @checksize_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %2
%21 = load i32, i32* @errno
store i32 %21, i32* %7
%22 = load i32, i32* @stderr
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* %7
%27 = load i32, i32* %7
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26, i8* %28)
store i32 1, i32* %3
br label %62
30: ; preds = %2
store i32 2097152, i32* %12
store i32 0, i32* %10
br label %31
31: ; preds = %30, %54
%32 = load i32, i32* %10
%33 = load i32, i32* %12
%34 = add nsw i32 %32, %33
store i32 %34, i32* %11
%35 = load i32, i32* %8
%36 = load i32, i32* %11
%37 = mul nsw i32 %36, 512
%38 = load i32, i32* @SEEK_SET
%39 = call i32 @lseek(i32 %35, i32 %37, i32 %38)
store i32 0, i32* @errno
%40 = load i32, i32* %8
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 512)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, 512
br i1 %44, label %45, label %47
45: ; preds = %31
%46 = load i32, i32* %11
store i32 %46, i32* %10
br label %54
47: ; preds = %31
%48 = load i32, i32* %12
%49 = icmp sle i32 %48, 1
br i1 %49, label %50, label %51
50: ; preds = %47
br label %55
51: ; preds = %47
%52 = load i32, i32* %12
%53 = sdiv i32 %52, 2
store i32 %53, i32* %12
br label %54
54: ; preds = %51, %45
br label %31
55: ; preds = %50
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %58)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %55, %20
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"open %s: %d (%s)\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [18 x i8] c"the size is %lld\0A\00"
define i32 @checksize_main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %5
%14 = tail call i8* @strerror(i32 %11)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %11, i8* %14)
br label %36
16: ; preds = %24, %29
%17 = phi i32 [ %30, %29 ], [ %26, %24 ]
%18 = add nsw i32 %17, %25
%19 = shl nsw i32 %18, 9
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lseek(i32 %8, i32 %19, i32 %20)
store i32 0, i32* @errno
%22 = call i32 @read(i32 %8, i8* %4, i32 512)
%23 = icmp eq i32 %22, 512
br i1 %23, label %24, label %27
24: ; preds = %16, %2
%25 = phi i32 [ 0, %2 ], [ %18, %16 ]
%26 = phi i32 [ 2097152, %2 ], [ %17, %16 ]
br label %16
27: ; preds = %16
%28 = icmp ult i32 %17, 2
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = lshr i32 %17, 1
br label %16
31: ; preds = %27
%32 = add nsw i32 %25, 1
%33 = sext i32 %32 to i64
%34 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %33)
%35 = tail call i32 @close(i32 %8)
br label %36
36: ; preds = %31, %10
%37 = phi i32 [ 1, %10 ], [ 0, %31 ]
ret i32 %37
}