#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 uintmax_t;
typedef int off_t;

/* Variables and functions */
int UINTMAX_MAX;
scalar_t__ blkdev_valid_offset(int, int);
off_t blkdev_find_size(int fd) {
  uintmax_t high, low = 0;
  for (high = 1024; blkdev_valid_offset(fd, high);) {
    if (high == UINTMAX_MAX)
      return -1;
    low = high;
    if (high >= UINTMAX_MAX / 2)
      high = UINTMAX_MAX;
    else
      high *= 2;
  }
  while (low < high - 1) {
    uintmax_t mid = (low + high) / 2;
    if (blkdev_valid_offset(fd, mid))
      low = mid;
    else
      high = mid;
  }
  blkdev_valid_offset(fd, 0);
  return (low + 1);
}


x86 assembly (clang, -O0):
<blkdev_find_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x400,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <blkdev_valid_offset@plt>
cmp    $0x0,%rax
je     11a5 <blkdev_find_size+0x95>
mov    -0xc(%rbp),%eax
mov    0x2ea8(%rip),%rcx
cmp    (%rcx),%eax
jne    115c <blkdev_find_size+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <blkdev_find_size+0xfa>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jl     1197 <blkdev_find_size+0x87>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    11a0 <blkdev_find_size+0x90>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1129 <blkdev_find_size+0x19>
jmp    11aa <blkdev_find_size+0x9a>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11f7 <blkdev_find_size+0xe7>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <blkdev_valid_offset@plt>
cmp    $0x0,%rax
je     11ec <blkdev_find_size+0xdc>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11f2 <blkdev_find_size+0xe2>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11aa <blkdev_find_size+0x9a>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <blkdev_valid_offset@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blkdev_find_size>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    $0x400,%ebx
mov    $0x400,%esi
call   1030 <blkdev_valid_offset@plt>
test   %rax,%rax
je     1170 <blkdev_find_size+0x60>
mov    $0x400,%ebp
mov    0x2eb5(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    (%r12),%eax
cmp    %eax,%ebp
je     11bd <blkdev_find_size+0xad>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
lea    0x0(%rbp,%rbp,1),%ebx
cmp    %ecx,%ebp
cmovge %eax,%ebx
mov    %r14d,%edi
mov    %ebx,%esi
call   1030 <blkdev_valid_offset@plt>
mov    %ebp,%r15d
mov    %ebx,%ebp
test   %rax,%rax
jne    1140 <blkdev_find_size+0x30>
jmp    1173 <blkdev_find_size+0x63>
xor    %r15d,%r15d
lea    -0x1(%rbx),%eax
cmp    %eax,%r15d
jl     119a <blkdev_find_size+0x8a>
mov    %r15d,%ebp
mov    %r14d,%edi
xor    %esi,%esi
call   1030 <blkdev_valid_offset@plt>
add    $0x1,%ebp
jmp    11c2 <blkdev_find_size+0xb2>
nopl   (%rax)
lea    -0x1(%rbx),%eax
mov    %ebp,%r15d
cmp    %eax,%ebp
jge    117e <blkdev_find_size+0x6e>
lea    (%r15,%rbx,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %r14d,%edi
mov    %ebp,%esi
call   1030 <blkdev_valid_offset@plt>
test   %rax,%rax
jne    1190 <blkdev_find_size+0x80>
mov    %ebp,%ebx
mov    %r15d,%ebp
jmp    1190 <blkdev_find_size+0x80>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@UINTMAX_MAX = global i32 0
define i32 @blkdev_find_size(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
store i32 1024, i32* %4
br label %7
7: ; preds = %28, %1
%8 = load i32, i32* %3
%9 = load i32, i32* %4
%10 = call i64 @blkdev_valid_offset(i32 %8, i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %7
%13 = load i32, i32* %4
%14 = load i32, i32* @UINTMAX_MAX
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %2
br label %54
17: ; preds = %12
%18 = load i32, i32* %4
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = load i32, i32* @UINTMAX_MAX
%21 = sdiv i32 %20, 2
%22 = icmp sge i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @UINTMAX_MAX
store i32 %24, i32* %4
br label %28
25: ; preds = %17
%26 = load i32, i32* %4
%27 = mul nsw i32 %26, 2
store i32 %27, i32* %4
br label %28
28: ; preds = %25, %23
br label %7
29: ; preds = %7
br label %30
30: ; preds = %48, %29
%31 = load i32, i32* %5
%32 = load i32, i32* %4
%33 = sub nsw i32 %32, 1
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %49
35: ; preds = %30
%36 = load i32, i32* %5
%37 = load i32, i32* %4
%38 = add nsw i32 %36, %37
%39 = sdiv i32 %38, 2
store i32 %39, i32* %6
%40 = load i32, i32* %3
%41 = load i32, i32* %6
%42 = call i64 @blkdev_valid_offset(i32 %40, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = load i32, i32* %6
store i32 %45, i32* %5
br label %48
46: ; preds = %35
%47 = load i32, i32* %6
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %44
br label %30
49: ; preds = %30
%50 = load i32, i32* %3
%51 = call i64 @blkdev_valid_offset(i32 %50, i32 0)
%52 = load i32, i32* %5
%53 = add nsw i32 %52, 1
store i32 %53, i32* %2
br label %54
54: ; preds = %49, %16
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@UINTMAX_MAX = global i32 0
define i32 @blkdev_find_size(i32 %0) {
%2 = tail call i64 @blkdev_valid_offset(i32 %0, i32 1024)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %9
4: ; preds = %13, %1
%5 = phi i32 [ 1024, %1 ], [ %17, %13 ]
%6 = phi i32 [ 0, %1 ], [ %10, %13 ]
%7 = add nsw i32 %5, -1
%8 = icmp slt i32 %6, %7
br i1 %8, label %20, label %31
9: ; preds = %1, %13
%10 = phi i32 [ %17, %13 ], [ 1024, %1 ]
%11 = load i32, i32* @UINTMAX_MAX
%12 = icmp eq i32 %10, %11
br i1 %12, label %35, label %13
13: ; preds = %9
%14 = sdiv i32 %11, 2
%15 = icmp slt i32 %10, %14
%16 = shl nsw i32 %10, 1
%17 = select i1 %15, i32 %16, i32 %11
%18 = tail call i64 @blkdev_valid_offset(i32 %0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %4, label %9
20: ; preds = %4, %20
%21 = phi i32 [ %28, %20 ], [ %6, %4 ]
%22 = phi i32 [ %27, %20 ], [ %5, %4 ]
%23 = add nsw i32 %21, %22
%24 = sdiv i32 %23, 2
%25 = tail call i64 @blkdev_valid_offset(i32 %0, i32 %24)
%26 = icmp eq i64 %25, 0
%27 = select i1 %26, i32 %24, i32 %22
%28 = select i1 %26, i32 %21, i32 %24
%29 = add nsw i32 %27, -1
%30 = icmp slt i32 %28, %29
br i1 %30, label %20, label %31
31: ; preds = %20, %4
%32 = phi i32 [ %6, %4 ], [ %28, %20 ]
%33 = tail call i64 @blkdev_valid_offset(i32 %0, i32 0)
%34 = add nsw i32 %32, 1
br label %35
35: ; preds = %9, %31
%36 = phi i32 [ %34, %31 ], [ -1, %9 ]
ret i32 %36
}