#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 fiemap_extent {
  int dummy;
};
struct fiemap {
  int fm_mapped_extents;
  int fm_extent_count;
  int /*<<< orphan*/ fm_extents;
  int /*<<< orphan*/ fm_length;
};
typedef int /*<<< orphan*/ __u64;
typedef int __u32;

/* Variables and functions */
int /*<<< orphan*/ FS_IOC_FIEMAP;
struct fiemap *calloc(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(struct fiemap *);
int ioctl(int, int /*<<< orphan*/, struct fiemap *);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
struct fiemap *realloc(struct fiemap *, int);
char *strerror(int /*<<< orphan*/);
struct fiemap *getfm(int fd, __u64 len) {
  struct fiemap *fm = NULL, *newfm;
  __u32 extent_count;
  if ((fm = calloc(1, sizeof(struct fiemap))) == NULL) {
    printf("calloc error: %s\n", strerror(errno));
    return NULL;
  }
  fm->fm_length = len;
  if (ioctl(fd, FS_IOC_FIEMAP, fm) == -1) {
    printf("ioctl error: %s\n", strerror(errno));
    goto err;
  }
  extent_count = sizeof(struct fiemap_extent) * fm->fm_mapped_extents;
  newfm = realloc(fm, sizeof(struct fiemap) + extent_count);
  if (newfm == NULL) {
    printf("realloc error: %s\n", strerror(errno));
    goto err;
  }
  fm = newfm;
  memset(fm->fm_extents, 0, extent_count);
  fm->fm_extent_count = fm->fm_mapped_extents;
  if (ioctl(fd, FS_IOC_FIEMAP, fm) == -1) {
    printf("ioctl error: %s\n", strerror(errno));
    goto err;
  }
  return fm;
err:
  free(fm);
  return NULL;
}


x86 assembly (clang, -O0):
<getfm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x1,%edi
mov    $0x10,%esi
call   1070 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cd <getfm+0x5d>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ed <getfm+0x17d>
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0xc(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1217 <getfm+0xa7>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
jmp    12dc <getfm+0x16c>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    $0x10,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1269 <getfm+0xf9>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
jmp    12dc <getfm+0x16c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12cf <getfm+0x15f>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd4d(%rip),%rdi
call   1040 <printf@plt>
jmp    12dc <getfm+0x16c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ed <getfm+0x17d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getfm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%r15d
mov    $0x1,%edi
mov    $0x10,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1207 <getfm+0x97>
mov    %rax,%rbp
mov    %ebx,0xc(%rax)
mov    0x2e47(%rip),%r13
mov    0x0(%r13),%esi
mov    %r15d,%edi
mov    %rax,%rdx
call   1060 <ioctl@plt>
lea    0xe5b(%rip),%r14
cmp    $0xffffffff,%eax
je     122d <getfm+0xbd>
mov    0x0(%rbp),%r12d
lea    0x10(,%r12,4),%esi
mov    %rbp,%rdi
call   1080 <realloc@plt>
test   %rax,%rax
je     1226 <getfm+0xb6>
mov    %rax,%rbx
shl    $0x2,%r12d
mov    0x8(%rax),%edi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x4(%rbx)
mov    0x0(%r13),%esi
mov    %r15d,%edi
mov    %rbx,%rdx
call   1060 <ioctl@plt>
mov    %rbx,%rbp
cmp    $0xffffffff,%eax
jne    1250 <getfm+0xe0>
jmp    122d <getfm+0xbd>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde4(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    124e <getfm+0xde>
lea    0xdf6(%rip),%r14
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.fiemap = type { i32, i32, i32, i32 }
@.str = [18 x i8] c"calloc error: %s\0A\00"
@errno = global i32 0
@FS_IOC_FIEMAP = global i32 0
@.str.1 = [17 x i8] c"ioctl error: %s\0A\00"
@.str.2 = [19 x i8] c"realloc error: %s\0A\00"
define %struct.fiemap* @getfm(i32 %0, i32 %1) {
%3 = alloca %struct.fiemap*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.fiemap*
%7 = alloca %struct.fiemap*
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.fiemap* null, %struct.fiemap** %6
%9 = call %struct.fiemap* @calloc(i32 1, i32 16)
store %struct.fiemap* %9, %struct.fiemap** %6
%10 = icmp eq %struct.fiemap* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
store %struct.fiemap* null, %struct.fiemap** %3
br label %73
15: ; preds = %2
%16 = load i32, i32* %5
%17 = load %struct.fiemap*, %struct.fiemap** %6
%18 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %17, i32 0, i32 3
store i32 %16, i32* %18
%19 = load i32, i32* %4
%20 = load i32, i32* @FS_IOC_FIEMAP
%21 = load %struct.fiemap*, %struct.fiemap** %6
%22 = call i32 @ioctl(i32 %19, i32 %20, %struct.fiemap* %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %70
28: ; preds = %15
%29 = load %struct.fiemap*, %struct.fiemap** %6
%30 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = mul i64 4, %32
%34 = trunc i64 %33 to i32
store i32 %34, i32* %8
%35 = load %struct.fiemap*, %struct.fiemap** %6
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = add i64 16, %37
%39 = trunc i64 %38 to i32
%40 = call %struct.fiemap* @realloc(%struct.fiemap* %35, i32 %39)
store %struct.fiemap* %40, %struct.fiemap** %7
%41 = load %struct.fiemap*, %struct.fiemap** %7
%42 = icmp eq %struct.fiemap* %41, null
br i1 %42, label %43, label %47
43: ; preds = %28
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %70
47: ; preds = %28
%48 = load %struct.fiemap*, %struct.fiemap** %7
store %struct.fiemap* %48, %struct.fiemap** %6
%49 = load %struct.fiemap*, %struct.fiemap** %6
%50 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %49, i32 0, i32 2
%51 = load i32, i32* %50
%52 = load i32, i32* %8
%53 = call i32 @memset(i32 %51, i32 0, i32 %52)
%54 = load %struct.fiemap*, %struct.fiemap** %6
%55 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load %struct.fiemap*, %struct.fiemap** %6
%58 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %57, i32 0, i32 1
store i32 %56, i32* %58
%59 = load i32, i32* %4
%60 = load i32, i32* @FS_IOC_FIEMAP
%61 = load %struct.fiemap*, %struct.fiemap** %6
%62 = call i32 @ioctl(i32 %59, i32 %60, %struct.fiemap* %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %68
64: ; preds = %47
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %66)
br label %70
68: ; preds = %47
%69 = load %struct.fiemap*, %struct.fiemap** %6
store %struct.fiemap* %69, %struct.fiemap** %3
br label %73
70: ; preds = %64, %43, %24
%71 = load %struct.fiemap*, %struct.fiemap** %6
%72 = call i32 @free(%struct.fiemap* %71)
store %struct.fiemap* null, %struct.fiemap** %3
br label %73
73: ; preds = %70, %68, %11
%74 = load %struct.fiemap*, %struct.fiemap** %3
ret %struct.fiemap* %74
}

LLVM IR (-O2):
%struct.fiemap = type { i32, i32, i32, i32 }
@.str = [18 x i8] c"calloc error: %s\0A\00"
@errno = global i32 0
@FS_IOC_FIEMAP = global i32 0
@.str.1 = [17 x i8] c"ioctl error: %s\0A\00"
@.str.2 = [19 x i8] c"realloc error: %s\0A\00"
define %struct.fiemap* @getfm(i32 %0, i32 %1) {
%3 = tail call %struct.fiemap* @calloc(i32 1, i32 16)
%4 = icmp eq %struct.fiemap* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %38
9: ; preds = %2
%10 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %3, i64 0, i32 3
store i32 %1, i32* %10
%11 = load i32, i32* @FS_IOC_FIEMAP
%12 = tail call i32 @ioctl(i32 %0, i32 %11, %struct.fiemap* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %31, label %14
14: ; preds = %9
%15 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = shl i32 %16, 2
%18 = add i32 %17, 16
%19 = tail call %struct.fiemap* @realloc(%struct.fiemap* %3, i32 %18)
%20 = icmp eq %struct.fiemap* %19, null
br i1 %20, label %31, label %21
21: ; preds = %14
%22 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %19, i64 0, i32 2
%23 = load i32, i32* %22
%24 = tail call i32 @memset(i32 %23, i32 0, i32 %17)
%25 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %19, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %19, i64 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @FS_IOC_FIEMAP
%29 = tail call i32 @ioctl(i32 %0, i32 %28, %struct.fiemap* %19)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %38
31: ; preds = %21, %14, %9
%32 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %14 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %21 ]
%33 = phi %struct.fiemap* [ %3, %9 ], [ %3, %14 ], [ %19, %21 ]
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 @printf(i8* %32, i8* %35)
%37 = tail call i32 @free(%struct.fiemap* %33)
br label %38
38: ; preds = %21, %31, %5
%39 = phi %struct.fiemap* [ null, %5 ], [ null, %31 ], [ %19, %21 ]
ret %struct.fiemap* %39
}