#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*/ FIBMAP;
int /*<<< orphan*/ FIGETBSZ;
int /*<<< orphan*/ die(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int getfd(char const *);
int getpagesize();
int ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t getblk(const char *fname, off_t b) {
  int fd = getfd(fname);
  int bsz, bno;
  int pgsz = getpagesize();
  if (ioctl(fd, FIGETBSZ, &bsz) == -1)
    die("ioctl(FIGETBSZ, %s): %s\n", fname, strerror(errno));
  if (pgsz > 0 && bsz > 0 && pgsz != bsz)
    bno = (b * pgsz) / bsz;
  else
    bno = b;
  if (ioctl(fd, FIBMAP, &bno) == -1)
    die("ioctl(FIBMAP, %s): %s\n", fname, strerror(errno));
  return bno;
}


x86 assembly (clang, -O0):
<getblk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <getfd@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    -0x14(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11bb <getblk+0x6b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe4a(%rip),%rdi
call   1040 <die@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    11ee <getblk+0x9e>
cmpl   $0x0,-0x14(%rbp)
jle    11ee <getblk+0x9e>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11ee <getblk+0x9e>
mov    -0xc(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x18(%rbp)
jmp    11f4 <getblk+0xa4>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    123a <getblk+0xea>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xde4(%rip),%rdi
call   1040 <die@plt>
movslq -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getblk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
call   1060 <getfd@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1050 <getpagesize@plt>
mov    %eax,%ebp
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %r15d,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11a7 <getblk+0x57>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe63(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <die@plt>
test   %ebp,%ebp
jle    11c1 <getblk+0x71>
mov    0x4(%rsp),%ecx
test   %ecx,%ecx
jle    11c1 <getblk+0x71>
cmp    %ecx,%ebp
je     11c1 <getblk+0x71>
imul   %ebx,%ebp
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %eax,%ebx
mov    %ebx,(%rsp)
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11fc <getblk+0xac>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe27(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <die@plt>
movslq (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FIGETBSZ = global i32 0
@.str = [25 x i8] c"ioctl(FIGETBSZ, %s): %s\0A\00"
@errno = global i32 0
@FIBMAP = global i32 0
@.str.1 = [23 x i8] c"ioctl(FIBMAP, %s): %s\0A\00"
define i64 @getblk(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = call i32 @getfd(i8* %9)
store i32 %10, i32* %5
%11 = call i32 (...) @getpagesize()
store i32 %11, i32* %8
%12 = load i32, i32* %5
%13 = load i32, i32* @FIGETBSZ
%14 = call i32 @ioctl(i32 %12, i32 %13, i32* %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17, i32 %19)
br label %21
21: ; preds = %16, %2
%22 = load i32, i32* %8
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %37
24: ; preds = %21
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i32, i32* %8
%29 = load i32, i32* %6
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load i32, i32* %4
%33 = load i32, i32* %8
%34 = mul nsw i32 %32, %33
%35 = load i32, i32* %6
%36 = sdiv i32 %34, %35
store i32 %36, i32* %7
br label %39
37: ; preds = %27, %24, %21
%38 = load i32, i32* %4
store i32 %38, i32* %7
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %5
%41 = load i32, i32* @FIBMAP
%42 = call i32 @ioctl(i32 %40, i32 %41, i32* %7)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i8*, i8** %3
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %45, i32 %47)
br label %49
49: ; preds = %44, %39
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
ret i64 %51
}

LLVM IR (-O2):
@FIGETBSZ = global i32 0
@.str = [25 x i8] c"ioctl(FIGETBSZ, %s): %s\0A\00"
@errno = global i32 0
@FIBMAP = global i32 0
@.str.1 = [23 x i8] c"ioctl(FIBMAP, %s): %s\0A\00"
define i64 @getblk(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @getfd(i8* %0)
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 (...) @getpagesize()
%9 = load i32, i32* @FIGETBSZ
%10 = call i32 @ioctl(i32 %5, i32 %9, i32* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %14)
br label %16
16: ; preds = %12, %2
%17 = icmp slt i32 %8, 1
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 1
%20 = select i1 %17, i1 true, i1 %19
%21 = icmp eq i32 %8, %18
%22 = select i1 %20, i1 true, i1 %21
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = mul nsw i32 %8, %1
%25 = sdiv i32 %24, %18
br label %26
26: ; preds = %16, %23
%27 = phi i32 [ %25, %23 ], [ %1, %16 ]
store i32 %27, i32* %4
%28 = load i32, i32* @FIBMAP
%29 = call i32 @ioctl(i32 %5, i32 %28, i32* %4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %33)
br label %35
35: ; preds = %31, %26
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
ret i64 %37
}