#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*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long,
                           int /*<<< orphan*/);
unsigned long lseek(int, unsigned long, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
unsigned long size_device(int fd) {
  char b;
  int r;
  unsigned long low, mid, high;
  low = 0;
  high = -1;
  while (mid = low + (high - low) / 2, mid > low) {
    if (lseek(fd, mid, SEEK_SET) != mid) {
      fprintf(stderr, "lseek to %lu failed: %s\n", mid, strerror(errno));
      exit(1);
    }
    r = read(fd, &b, 1);
    if (r < 0) {
      fprintf(stderr, "read failed at position %lu: %s\n", mid,
              strerror(errno));
      exit(1);
    }
    if (r > 0)
      low = mid;
    else
      high = mid;
  }
  return high;
}


x86 assembly (clang, -O0):
<size_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    -0x18(%rbp),%rcx
shr    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1273 <size_device+0x123>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    -0x20(%rbp),%rax
je     11f0 <size_device+0xa0>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xe1f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    124f <size_device+0xff>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xdd9(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jle    1266 <size_device+0x116>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    126e <size_device+0x11e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    116b <size_device+0x1b>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<size_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
mov    $0xffffffffffffffff,%r14
movabs $0x7fffffffffffffff,%rbx
ff ff 7f
xor    %ebp,%ebp
mov    0x2e58(%rip),%r13
lea    0x7(%rsp),%r15
nopl   (%rax)
mov    0x0(%r13),%edx
mov    %r12d,%edi
mov    %rbx,%rsi
call   1030 <lseek@plt>
cmp    %rbx,%rax
jne    11d3 <size_device+0x83>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11f3 <size_device+0xa3>
cmovne %rbx,%rbp
cmove  %rbx,%r14
mov    %r14,%rbx
sub    %rbp,%rbx
shr    %rbx
add    %rbp,%rbx
cmp    %rbp,%rbx
ja     1180 <size_device+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e06(%rip),%rax
mov    (%rax),%ebp
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe0f(%rip),%rsi
jmp    1211 <size_device+0xc1>
mov    0x2de6(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe08(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"lseek to %lu failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"read failed at position %lu: %s\0A\00"
define i64 @size_device(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %5
store i64 -1, i64* %7
br label %8
8: ; preds = %51, %1
%9 = load i64, i64* %5
%10 = load i64, i64* %7
%11 = load i64, i64* %5
%12 = sub i64 %10, %11
%13 = udiv i64 %12, 2
%14 = add i64 %9, %13
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = load i64, i64* %5
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %52
18: ; preds = %8
%19 = load i32, i32* %2
%20 = load i64, i64* %6
%21 = load i32, i32* @SEEK_SET
%22 = call i64 @lseek(i32 %19, i64 %20, i32 %21)
%23 = load i64, i64* %6
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = load i64, i64* %6
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %27, i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = load i32, i32* %2
%34 = call i32 @read(i32 %33, i8* %3, i32 1)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i64, i64* %6
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %39, i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %32
%45 = load i32, i32* %4
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = load i64, i64* %6
store i64 %48, i64* %5
br label %51
49: ; preds = %44
%50 = load i64, i64* %6
store i64 %50, i64* %7
br label %51
51: ; preds = %49, %47
br label %8
52: ; preds = %8
%53 = load i64, i64* %7
ret i64 %53
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"lseek to %lu failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"read failed at position %lu: %s\0A\00"
define i64 @size_device(i32 %0) {
%2 = alloca i8
br label %3
3: ; preds = %1, %25
%4 = phi i64 [ 9223372036854775807, %1 ], [ %31, %25 ]
%5 = phi i64 [ -1, %1 ], [ %28, %25 ]
%6 = phi i64 [ 0, %1 ], [ %27, %25 ]
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @lseek(i32 %0, i64 %4, i32 %7)
%9 = icmp eq i64 %8, %4
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 %4, i32 %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = call i32 @read(i32 %0, i8* %2, i32 1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i32 @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %4, i32 %22)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = icmp eq i32 %17, 0
%27 = select i1 %26, i64 %6, i64 %4
%28 = select i1 %26, i64 %4, i64 %5
%29 = sub i64 %28, %27
%30 = lshr i64 %29, 1
%31 = add i64 %30, %27
%32 = icmp ugt i64 %31, %27
br i1 %32, label %3, label %33
33: ; preds = %25
ret i64 %28
}