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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ valid_offset(int, int const);
unsigned long count_blocks(char *filename, int blocksize, int fd) {
  loff_t high, low;
  int opened_here = 0;
  if (fd < 0) {
    fd = open(filename, O_RDONLY);
    opened_here = 1;
  }
  if (fd < 0)
    die("count_blocks: open failed (%s)", strerror(errno));
  low = 0;
  for (high = 1; valid_offset(fd, high); high *= 2)
    low = high;
  while (low < high - 1) {
    const loff_t mid = (low + high) / 2;
    if (valid_offset(fd, mid))
      low = mid;
    else
      high = mid;
  }
  valid_offset(fd, 0);
  if (opened_here)
    close(fd);
  return (low + 1) / (blocksize);
}


x86 assembly (clang, -O0):
<count_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    118f <count_blocks+0x3f>
mov    -0x8(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b5 <count_blocks+0x65>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%esi
lea    0xe50(%rip),%rdi
call   1060 <die@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <valid_offset@plt>
cmp    $0x0,%rax
je     11ec <count_blocks+0x9c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c3 <count_blocks+0x73>
jmp    11f1 <count_blocks+0xa1>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    123e <count_blocks+0xee>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <valid_offset@plt>
cmp    $0x0,%rax
je     1233 <count_blocks+0xe3>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1239 <count_blocks+0xe9>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11f1 <count_blocks+0xa1>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1040 <valid_offset@plt>
cmpl   $0x0,-0x1c(%rbp)
je     125a <count_blocks+0x10a>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0xc(%rbp)
cltq
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edx,%r12d
test   %edx,%edx
jns    1199 <count_blocks+0x49>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1199 <count_blocks+0x49>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe6e(%rip),%rdi
mov    %eax,%esi
call   1060 <die@plt>
xor    %ebx,%ebx
mov    $0x1,%ecx
mov    %ebx,%r13d
mov    %ecx,%ebx
mov    %r12d,%edi
mov    %ecx,%esi
call   1040 <valid_offset@plt>
lea    (%rbx,%rbx,1),%ecx
test   %rax,%rax
jne    11a0 <count_blocks+0x50>
lea    -0x1(%rbx),%eax
cmp    %eax,%r13d
jl     120a <count_blocks+0xba>
mov    %r13d,%ebp
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <valid_offset@plt>
test   %r15d,%r15d
jns    11d9 <count_blocks+0x89>
mov    %r12d,%edi
call   1030 <close@plt>
add    $0x1,%ebp
mov    %ebp,%eax
cltd
idiv   %r14d
cltq
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x1(%rbx),%eax
mov    %ebp,%r13d
cmp    %eax,%ebp
jge    11c2 <count_blocks+0x72>
lea    (%rbx,%r13,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %r12d,%edi
mov    %ebp,%esi
call   1040 <valid_offset@plt>
test   %rax,%rax
jne    1200 <count_blocks+0xb0>
mov    %ebp,%ebx
mov    %r13d,%ebp
jmp    1200 <count_blocks+0xb0>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [31 x i8] c"count_blocks: open failed (%s)\00"
@errno = global i32 0
define i64 @count_blocks(i8* nocapture %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %2, 0
br i1 %4, label %5, label %13
5: ; preds = %3
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @errno
%11 = tail call i32 @strerror(i32 %10)
%12 = tail call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %3, %9, %5
%14 = phi i32 [ %7, %9 ], [ %7, %5 ], [ %2, %3 ]
br label %15
15: ; preds = %15, %13
%16 = phi i32 [ 1, %13 ], [ %20, %15 ]
%17 = phi i32 [ 0, %13 ], [ %16, %15 ]
%18 = tail call i64 @valid_offset(i32 %14, i32 %16)
%19 = icmp eq i64 %18, 0
%20 = shl nsw i32 %16, 1
br i1 %19, label %21, label %15
21: ; preds = %15
%22 = add nsw i32 %16, -1
%23 = icmp slt i32 %17, %22
br i1 %23, label %24, label %35
24: ; preds = %21, %24
%25 = phi i32 [ %32, %24 ], [ %17, %21 ]
%26 = phi i32 [ %31, %24 ], [ %16, %21 ]
%27 = add nsw i32 %25, %26
%28 = sdiv i32 %27, 2
%29 = tail call i64 @valid_offset(i32 %14, i32 %28)
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 %28, i32 %26
%32 = select i1 %30, i32 %25, i32 %28
%33 = add nsw i32 %31, -1
%34 = icmp slt i32 %32, %33
br i1 %34, label %24, label %35
35: ; preds = %24, %21
%36 = phi i32 [ %17, %21 ], [ %32, %24 ]
%37 = tail call i64 @valid_offset(i32 %14, i32 0)
br i1 %4, label %38, label %40
38: ; preds = %35
%39 = tail call i32 @close(i32 %14)
br label %40
40: ; preds = %38, %35
%41 = add nsw i32 %36, 1
%42 = sdiv i32 %41, %1
%43 = sext i32 %42 to i64
ret i64 %43
}