#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*/ FIBMAP;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
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;
  int block;
  int first_block;
  int last_block;
  int totals_block;
  int fragments;
  int i;
  int n;
  for (n = 1; n < argc; n++) {
    if (argc < 2) {
      printf("Used to see file maps \nUsage: %s filename1 "
             "[[..[filename2]...filename(N-1)] filenameN]\n",
             argv[0]);
      return 0;
    }
    fd = open(argv[n], O_RDONLY);
    if (fd == -1) {
      perror("open failed");
      continue;
    }
    i = 0;
    block = 0;
    first_block = 0;
    last_block = 0;
    fragments = 0;
    totals_block = 0;
    while (ioctl(fd, FIBMAP, &block) == 0) {
      if (first_block == 0) {
        last_block = block - 1;
        first_block = block;
      }
      if (block != last_block + 1) {
        totals_block += last_block - first_block + 1;
        fragments++;
        first_block = block;
        last_block = block;
      } else {
        last_block++;
      }
      if (!block) {
        printf("%d\n", fragments);
        break;
      }
      i++;
      block = i;
    }
    if (errno) {
      perror("FIBMAP failed");
    }
    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)
movl   $0x1,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12e8 <main+0x198>
cmpl   $0x2,-0x8(%rbp)
jge    11a4 <main+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <main+0x19f>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11dc <main+0x8c>
lea    0xe86(%rip),%rdi
call   1070 <perror@plt>
jmp    12da <main+0x18a>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    12b5 <main+0x165>
cmpl   $0x0,-0x1c(%rbp)
jne    123e <main+0xee>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
je     1278 <main+0x128>
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x1,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1281 <main+0x131>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    12a1 <main+0x151>
mov    -0x28(%rbp),%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b5 <main+0x165>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1206 <main+0xb6>
mov    0x2d1c(%rip),%rax
cmpq   $0x0,(%rax)
je     12d2 <main+0x182>
lea    0xd9b(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    116d <main+0x1d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%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,0x10(%rsp)
cmp    $0x2,%edi
jl     1291 <main+0x141>
mov    %edi,%eax
mov    %rax,0x8(%rsp)
mov    $0x1,%r12d
nopl   0x0(%rax)
mov    0x10(%rsp),%rax
mov    (%rax,%r12,8),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1275 <main+0x125>
mov    %eax,%ebp
movl   $0x0,0x4(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
lea    0x4(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1250 <main+0x100>
xor    %r13d,%r13d
mov    $0x1,%r15d
xor    %ebx,%ebx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%ecx
mov    %r13d,%edx
mov    0x4(%rsp),%ebx
lea    -0x1(%rbx),%eax
test   %r14d,%r14d
cmovne %ecx,%eax
add    $0x1,%eax
xor    %r13d,%r13d
cmp    %eax,%ebx
setne  %r13b
add    %edx,%r13d
cmp    $0x1,%ebx
jb     1238 <main+0xe8>
test   %r14d,%r14d
cmove  %ebx,%r14d
cmp    %eax,%ebx
cmovne %ebx,%r14d
mov    %r15d,0x4(%rsp)
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
lea    0x4(%rsp),%rdx
call   1040 <ioctl@plt>
add    $0x1,%r15d
test   %rax,%rax
je     11e0 <main+0x90>
jmp    1250 <main+0x100>
lea    0xdcd(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
nopl   0x0(%rax)
mov    0x2d81(%rip),%rax
cmpq   $0x0,(%rax)
jne    1283 <main+0x133>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%r12
cmp    0x8(%rsp),%r12
jne    1180 <main+0x30>
jmp    1291 <main+0x141>
lea    0xd84(%rip),%rdi
call   1070 <perror@plt>
jmp    1264 <main+0x114>
lea    0xd86(%rip),%rdi
call   1070 <perror@plt>
jmp    125d <main+0x10d>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [88 x i8] c"Used to see file maps \0AUsage: %s filename1 [[..[filename2]...filename(N-1)] filenameN]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@FIBMAP = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@errno = global i64 0
@.str.3 = [14 x i8] c"FIBMAP failed\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 i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %13
br label %14
14: ; preds = %88, %2
%15 = load i32, i32* %13
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %91
18: ; preds = %14
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str, i64 0, i64 0), i8* %24)
store i32 0, i32* %3
br label %92
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = load i32, i32* %13
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %88
38: ; preds = %26
store i32 0, i32* %12
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %11
store i32 0, i32* %10
br label %39
39: ; preds = %76, %38
%40 = load i32, i32* %6
%41 = load i32, i32* @FIBMAP
%42 = call i64 @ioctl(i32 %40, i32 %41, i32* %7)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %80
44: ; preds = %39
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* %7
%49 = sub nsw i32 %48, 1
store i32 %49, i32* %9
%50 = load i32, i32* %7
store i32 %50, i32* %8
br label %51
51: ; preds = %47, %44
%52 = load i32, i32* %7
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
%55 = icmp ne i32 %52, %54
br i1 %55, label %56, label %67
56: ; preds = %51
%57 = load i32, i32* %9
%58 = load i32, i32* %8
%59 = sub nsw i32 %57, %58
%60 = add nsw i32 %59, 1
%61 = load i32, i32* %10
%62 = add nsw i32 %61, %60
store i32 %62, i32* %10
%63 = load i32, i32* %11
%64 = add nsw i32 %63, 1
store i32 %64, i32* %11
%65 = load i32, i32* %7
store i32 %65, i32* %8
%66 = load i32, i32* %7
store i32 %66, i32* %9
br label %70
67: ; preds = %51
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
store i32 %69, i32* %9
br label %70
70: ; preds = %67, %56
%71 = load i32, i32* %7
%72 = icmp ne i32 %71, 0
br i1 %72, label %76, label %73
73: ; preds = %70
%74 = load i32, i32* %11
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %74)
br label %80
76: ; preds = %70
%77 = load i32, i32* %12
%78 = add nsw i32 %77, 1
store i32 %78, i32* %12
%79 = load i32, i32* %12
store i32 %79, i32* %7
br label %39
80: ; preds = %73, %39
%81 = load i64, i64* @errno
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %85
85: ; preds = %83, %80
%86 = load i32, i32* %6
%87 = call i32 @close(i32 %86)
br label %88
88: ; preds = %85, %36
%89 = load i32, i32* %13
%90 = add nsw i32 %89, 1
store i32 %90, i32* %13
br label %14
91: ; preds = %14
store i32 0, i32* %3
br label %92
92: ; preds = %91, %21
%93 = load i32, i32* %3
ret i32 %93
}

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