#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ FIBMAP;
int /*<<< orphan*/ FIGETBSZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int fd, i, block, blocksize, blkcnt;
  struct stat st;
  assert(argv[1] != NULL);
  fd = open(argv[1], O_RDONLY);
  if (fd <= 0) {
    perror("error opening file");
    goto end;
  }
  if (ioctl(fd, FIGETBSZ, &blocksize)) {
    perror("FIBMAP ioctl failed");
    goto end;
  }
  if (fstat(fd, &st)) {
    perror("fstat error");
    goto end;
  }
  blkcnt = (st.st_size + blocksize - 1) / blocksize;
  printf("File %s size %d blocks %d blocksize %d\n", argv[1], (int)st.st_size,
         blkcnt, blocksize);
  for (i = 0; i < blkcnt; i++) {
    block = i;
    if (ioctl(fd, FIBMAP, &block)) {
      perror("FIBMAP ioctl failed");
    }
    printf("%3d %10d\n", i, block);
  }
end:
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11d0 <main+0x60>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
jmp    12c2 <main+0x152>
mov    -0x14(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1200 <main+0x90>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
jmp    12c2 <main+0x152>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     1227 <main+0xb7>
lea    0xe0a(%rip),%rdi
call   1080 <perror@plt>
jmp    12c2 <main+0x152>
mov    -0x28(%rbp),%eax
add    -0x20(%rbp),%eax
sub    $0x1,%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12bd <main+0x14d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     129b <main+0x12b>
lea    0xd7d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    125e <main+0xee>
jmp    12c2 <main+0x152>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
xor    %edi,%edi
cmpq   $0x0,0x8(%rsi)
setne  %dil
call   1060 <assert@plt>
mov    0x8(%r14),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    126d <main+0xfd>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0x14(%rsp),%rdx
mov    %r13d,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1276 <main+0x106>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
call   1090 <fstat@plt>
test   %rax,%rax
jne    127f <main+0x10f>
mov    0x10(%rsp),%ecx
mov    0x14(%rsp),%r8d
lea    (%rcx,%r8,1),%eax
add    $0xffffffff,%eax
cltd
idiv   %r8d
mov    %eax,%ebp
mov    0x8(%r14),%rsi
lea    0xe2f(%rip),%rdi
mov    %ecx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
jle    128b <main+0x11b>
mov    0x2db6(%rip),%r12
lea    0xc(%rsp),%r14
lea    0xe35(%rip),%r15
xor    %ebx,%ebx
jmp    1247 <main+0xd7>
nopw   0x0(%rax,%rax,1)
mov    0xc(%rsp),%edx
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
je     128b <main+0x11b>
mov    %ebx,0xc(%rsp)
mov    (%r12),%esi
mov    %r13d,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     1230 <main+0xc0>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
jmp    1230 <main+0xc0>
lea    0xd8c(%rip),%rdi
jmp    1286 <main+0x116>
lea    0xd96(%rip),%rdi
jmp    1286 <main+0x116>
lea    0xda1(%rip),%rdi
call   1080 <perror@plt>
mov    %r13d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"error opening file\00"
@FIGETBSZ = global i32 0
@.str.1 = [20 x i8] c"FIBMAP ioctl failed\00"
@.str.2 = [12 x i8] c"fstat error\00"
@.str.3 = [40 x i8] c"File %s size %d blocks %d blocksize %d\0A\00"
@FIBMAP = global i32 0
@.str.4 = [10 x i8] c"%3d %10d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %76
27: ; preds = %2
%28 = load i32, i32* %6
%29 = load i32, i32* @FIGETBSZ
%30 = call i64 @ioctl(i32 %28, i32 %29, i32* %9)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %76
34: ; preds = %27
%35 = load i32, i32* %6
%36 = call i64 @fstat(i32 %35, %struct.stat* %11)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %76
40: ; preds = %34
%41 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* %9
%44 = add nsw i32 %42, %43
%45 = sub nsw i32 %44, 1
%46 = load i32, i32* %9
%47 = sdiv i32 %45, %46
store i32 %47, i32* %10
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* %10
%54 = load i32, i32* %9
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %50, i32 %52, i32 %53, i32 %54)
store i32 0, i32* %7
br label %56
56: ; preds = %72, %40
%57 = load i32, i32* %7
%58 = load i32, i32* %10
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %75
60: ; preds = %56
%61 = load i32, i32* %7
store i32 %61, i32* %8
%62 = load i32, i32* %6
%63 = load i32, i32* @FIBMAP
%64 = call i64 @ioctl(i32 %62, i32 %63, i32* %8)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %68
68: ; preds = %66, %60
%69 = load i32, i32* %7
%70 = load i32, i32* %8
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %69, i32 %70)
br label %72
72: ; preds = %68
%73 = load i32, i32* %7
%74 = add nsw i32 %73, 1
store i32 %74, i32* %7
br label %56
75: ; preds = %56
br label %76
76: ; preds = %75, %38, %32, %25
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"error opening file\00"
@FIGETBSZ = global i32 0
@.str.1 = [20 x i8] c"FIBMAP ioctl failed\00"
@.str.2 = [12 x i8] c"fstat error\00"
@.str.3 = [40 x i8] c"File %s size %d blocks %d blocksize %d\0A\00"
@FIBMAP = global i32 0
@.str.4 = [10 x i8] c"%3d %10d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.stat* %5 to i8*
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = load i8*, i8** %9
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %53
20: ; preds = %2
%21 = load i32, i32* @FIGETBSZ
%22 = call i64 @ioctl(i32 %16, i32 %21, i32* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %53
26: ; preds = %20
%27 = call i64 @fstat(i32 %16, %struct.stat* %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %53
31: ; preds = %26
%32 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%33 = load i32, i32* %32
%34 = load i32, i32* %4
%35 = add i32 %33, -1
%36 = add i32 %35, %34
%37 = sdiv i32 %36, %34
%38 = load i8*, i8** %9
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %38, i32 %33, i32 %37, i32 %34)
%40 = icmp sgt i32 %37, 0
br i1 %40, label %41, label %53
41: ; preds = %31, %48
%42 = phi i32 [ %51, %48 ], [ 0, %31 ]
store i32 %42, i32* %3
%43 = load i32, i32* @FIBMAP
%44 = call i64 @ioctl(i32 %16, i32 %43, i32* %3)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %48
48: ; preds = %46, %41
%49 = load i32, i32* %3
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %42, i32 %49)
%51 = add nuw nsw i32 %42, 1
%52 = icmp eq i32 %51, %37
br i1 %52, label %53, label %41
53: ; preds = %48, %31, %29, %24, %18
%54 = call i32 @close(i32 %16)
ret i32 0
}