#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*/ BLKGETSIZE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ probe(int, long);
long determineSize(char *device) {
  long low = 0, high = 1, mid;
  int size;
  int f = open(device, O_RDONLY);
  if (f < 0) {
    perror(device);
    exit(1);
  }
  if (ioctl(f, BLKGETSIZE, &size) >= 0) {
    size *= 512;
  } else {
    for (;;) {
      if (!probe(f, high))
        break;
      low = high;
      high *= 2;
    }
    do {
      mid = (low + high) / 2;
      if (probe(f, mid)) {
        low = mid;
      } else {
        high = mid;
      }
    } while (high - low - 1);
    size = low + 1;
  }
  close(f);
  return size;
}


x86 assembly (clang, -O0):
<determineSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11ae <determineSize+0x4e>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jl     11db <determineSize+0x7b>
mov    -0x24(%rbp),%eax
shl    $0x9,%eax
mov    %eax,-0x24(%rbp)
jmp    1280 <determineSize+0x120>
jmp    11e0 <determineSize+0x80>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <probe@plt>
cmp    $0x0,%rax
jne    11fb <determineSize+0x9b>
jmp    1214 <determineSize+0xb4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11e0 <determineSize+0x80>
jmp    1219 <determineSize+0xb9>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <probe@plt>
cmp    $0x0,%rax
je     1252 <determineSize+0xf2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    125a <determineSize+0xfa>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    125f <determineSize+0xff>
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
sub    $0x1,%rax
cmp    $0x0,%rax
jne    1219 <determineSize+0xb9>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movslq -0x24(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<determineSize>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1223 <determineSize+0xc3>
mov    %eax,%r14d
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11a9 <determineSize+0x49>
mov    0x4(%rsp),%r15d
shl    $0x9,%r15d
jmp    1206 <determineSize+0xa6>
mov    $0x1,%ecx
xor    %ebp,%ebp
mov    %rbp,%r15
mov    %rcx,%rbp
mov    %r14d,%edi
mov    %rcx,%rsi
call   1030 <probe@plt>
mov    %rbp,%rcx
add    %rbp,%rcx
test   %rax,%rax
jne    11b0 <determineSize+0x50>
jmp    11dc <determineSize+0x7c>
xchg   %ax,%ax
mov    %rbx,%r15
lea    0x1(%r15),%rax
cmp    %rax,%rbp
je     1202 <determineSize+0xa2>
lea    (%r15,%rbp,1),%rax
mov    %rax,%rbx
shr    $0x3f,%rbx
add    %rax,%rbx
sar    %rbx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <probe@plt>
test   %rax,%rax
jne    11d0 <determineSize+0x70>
mov    %rbx,%rbp
jmp    11d3 <determineSize+0x73>
add    $0x1,%r15d
mov    %r15d,0x4(%rsp)
mov    %r14d,%edi
call   1050 <close@plt>
movslq 0x4(%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@BLKGETSIZE = global i32 0
define i64 @determineSize(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i64 0, i64* %3
store i64 1, i64* %4
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i32 @perror(i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i32, i32* %7
%19 = load i32, i32* @BLKGETSIZE
%20 = call i64 @ioctl(i32 %18, i32 %19, i32* %6)
%21 = icmp sge i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* %6
%24 = mul nsw i32 %23, 512
store i32 %24, i32* %6
br label %61
25: ; preds = %17
br label %26
26: ; preds = %32, %25
%27 = load i32, i32* %7
%28 = load i64, i64* %4
%29 = call i64 @probe(i32 %27, i64 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %26
br label %36
32: ; preds = %26
%33 = load i64, i64* %4
store i64 %33, i64* %3
%34 = load i64, i64* %4
%35 = mul nsw i64 %34, 2
store i64 %35, i64* %4
br label %26
36: ; preds = %31
br label %37
37: ; preds = %51, %36
%38 = load i64, i64* %3
%39 = load i64, i64* %4
%40 = add nsw i64 %38, %39
%41 = sdiv i64 %40, 2
store i64 %41, i64* %5
%42 = load i32, i32* %7
%43 = load i64, i64* %5
%44 = call i64 @probe(i32 %42, i64 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = load i64, i64* %5
store i64 %47, i64* %3
br label %50
48: ; preds = %37
%49 = load i64, i64* %5
store i64 %49, i64* %4
br label %50
50: ; preds = %48, %46
br label %51
51: ; preds = %50
%52 = load i64, i64* %4
%53 = load i64, i64* %3
%54 = sub nsw i64 %52, %53
%55 = sub nsw i64 %54, 1
%56 = icmp ne i64 %55, 0
br i1 %56, label %37, label %57
57: ; preds = %51
%58 = load i64, i64* %3
%59 = add nsw i64 %58, 1
%60 = trunc i64 %59 to i32
store i32 %60, i32* %6
br label %61
61: ; preds = %57, %22
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
ret i64 %65
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@BLKGETSIZE = global i32 0
define i64 @determineSize(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i32, i32* @BLKGETSIZE
%12 = call i64 @ioctl(i32 %5, i32 %11, i32* %2)
%13 = icmp sgt i64 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %2
%16 = shl nsw i32 %15, 9
br label %37
17: ; preds = %10, %17
%18 = phi i64 [ %22, %17 ], [ 1, %10 ]
%19 = phi i64 [ %18, %17 ], [ 0, %10 ]
%20 = call i64 @probe(i32 %5, i64 %18)
%21 = icmp eq i64 %20, 0
%22 = shl nsw i64 %18, 1
br i1 %21, label %23, label %17
23: ; preds = %17, %23
%24 = phi i64 [ %30, %23 ], [ %18, %17 ]
%25 = phi i64 [ %31, %23 ], [ %19, %17 ]
%26 = add nsw i64 %25, %24
%27 = sdiv i64 %26, 2
%28 = call i64 @probe(i32 %5, i64 %27)
%29 = icmp eq i64 %28, 0
%30 = select i1 %29, i64 %27, i64 %24
%31 = select i1 %29, i64 %25, i64 %27
%32 = add i64 %31, 1
%33 = icmp eq i64 %30, %32
br i1 %33, label %34, label %23
34: ; preds = %23
%35 = trunc i64 %31 to i32
%36 = add i32 %35, 1
br label %37
37: ; preds = %34, %14
%38 = phi i32 [ %36, %34 ], [ %16, %14 ]
store i32 %38, i32* %2
%39 = call i32 @close(i32 %5)
%40 = load i32, i32* %2
%41 = sext i32 %40 to i64
ret i64 %41
}