#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_extent_count;
  int fm_mapped_extents;
  struct fiemap *fm_extents;
  scalar_t__ fm_flags;
  int /*<<< orphan*/ fm_length;
  scalar_t__ fm_start;
};

/* Variables and functions */
int /*<<< orphan*/ FS_IOC_FIEMAP;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct fiemap *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct fiemap *, int /*<<< orphan*/, int);
scalar_t__ realloc(struct fiemap *, int);
int /*<<< orphan*/ stderr;
struct fiemap *read_fiemap(int fd) {
  struct fiemap *fiemap;
  int extents_size;
  if ((fiemap = (struct fiemap *)malloc(sizeof(struct fiemap))) == NULL) {
    fprintf(stderr, "Out of memory allocating fiemap\n");
    return NULL;
  }
  memset(fiemap, 0, sizeof(struct fiemap));
  fiemap->fm_start = 0;
  fiemap->fm_length = ~0;
  fiemap->fm_flags = 0;
  fiemap->fm_extent_count = 0;
  fiemap->fm_mapped_extents = 0;
  if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
    fprintf(stderr, "fiemap ioctl() failed\n");
    return NULL;
  }
  extents_size = sizeof(struct fiemap_extent) * (fiemap->fm_mapped_extents);
  if ((fiemap = (struct fiemap *)realloc(fiemap, sizeof(struct fiemap) +
                                                     extents_size)) == NULL) {
    fprintf(stderr, "Out of memory allocating fiemap\n");
    return NULL;
  }
  memset(fiemap->fm_extents, 0, extents_size);
  fiemap->fm_extent_count = fiemap->fm_mapped_extents;
  fiemap->fm_mapped_extents = 0;
  if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
    fprintf(stderr, "fiemap ioctl() failed\n");
    return NULL;
  }
  return fiemap;
}


x86 assembly (clang, -O0):
<read_fiemap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x28,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1195 <read_fiemap+0x45>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_fiemap+0x193>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,0x20(%rax)
mov    -0x18(%rbp),%rax
movl   $0xffffffff,0x18(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0xc(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    121e <read_fiemap+0xce>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_fiemap+0x193>
mov    -0x18(%rbp),%rax
movslq 0x4(%rax),%rax
shl    $0x2,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    $0x28,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1270 <read_fiemap+0x120>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_fiemap+0x193>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x4(%rax)
mov    -0xc(%rbp),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    12db <read_fiemap+0x18b>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd58(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_fiemap+0x193>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_fiemap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x28,%edi
call   1060 <malloc@plt>
lea    0xe91(%rip),%r15
test   %rax,%rax
je     121e <read_fiemap+0xce>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
movq   $0x0,0x20(%rbp)
movl   $0xffffffff,0x18(%rbp)
movq   $0x0,0x10(%rbp)
movq   $0x0,0x0(%rbp)
mov    0x2e38(%rip),%r13
mov    0x0(%r13),%esi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1040 <ioctl@plt>
lea    0xe5b(%rip),%r12
test   %rax,%rax
js     121b <read_fiemap+0xcb>
mov    0x4(%rbp),%ebx
lea    0x28(,%rbx,4),%esi
mov    %rbp,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     121e <read_fiemap+0xce>
mov    %rax,%rbp
shl    $0x2,%ebx
mov    0x8(%rax),%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <memset@plt>
mov    0x4(%rbp),%eax
mov    %eax,0x0(%rbp)
movl   $0x0,0x4(%rbp)
mov    0x0(%r13),%esi
mov    %r14d,%edi
mov    %rbp,%rdx
call   1040 <ioctl@plt>
mov    %r12,%r15
test   %rax,%rax
jns    1231 <read_fiemap+0xe1>
jmp    121e <read_fiemap+0xce>
mov    %r12,%r15
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
call   1050 <fprintf@plt>
xor    %ebp,%ebp
mov    %rbp,%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, %struct.fiemap*, i64, i32, i64 }
@stderr = global i32 0
@.str = [33 x i8] c"Out of memory allocating fiemap\0A\00"
@FS_IOC_FIEMAP = global i32 0
@.str.1 = [23 x i8] c"fiemap ioctl() failed\0A\00"
define %struct.fiemap* @read_fiemap(i32 %0) {
%2 = alloca %struct.fiemap*
%3 = alloca i32
%4 = alloca %struct.fiemap*
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i64 @malloc(i32 40)
%7 = inttoptr i64 %6 to %struct.fiemap*
store %struct.fiemap* %7, %struct.fiemap** %4
%8 = icmp eq %struct.fiemap* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store %struct.fiemap* null, %struct.fiemap** %2
br label %74
12: ; preds = %1
%13 = load %struct.fiemap*, %struct.fiemap** %4
%14 = call i32 @memset(%struct.fiemap* %13, i32 0, i32 40)
%15 = load %struct.fiemap*, %struct.fiemap** %4
%16 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %15, i32 0, i32 5
store i64 0, i64* %16
%17 = load %struct.fiemap*, %struct.fiemap** %4
%18 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %17, i32 0, i32 4
store i32 -1, i32* %18
%19 = load %struct.fiemap*, %struct.fiemap** %4
%20 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %19, i32 0, i32 3
store i64 0, i64* %20
%21 = load %struct.fiemap*, %struct.fiemap** %4
%22 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %21, i32 0, i32 0
store i32 0, i32* %22
%23 = load %struct.fiemap*, %struct.fiemap** %4
%24 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %23, i32 0, i32 1
store i32 0, i32* %24
%25 = load i32, i32* %3
%26 = load i32, i32* @FS_IOC_FIEMAP
%27 = load %struct.fiemap*, %struct.fiemap** %4
%28 = call i64 @ioctl(i32 %25, i32 %26, %struct.fiemap* %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %12
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store %struct.fiemap* null, %struct.fiemap** %2
br label %74
33: ; preds = %12
%34 = load %struct.fiemap*, %struct.fiemap** %4
%35 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = sext i32 %36 to i64
%38 = mul i64 4, %37
%39 = trunc i64 %38 to i32
store i32 %39, i32* %5
%40 = load %struct.fiemap*, %struct.fiemap** %4
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = add i64 40, %42
%44 = trunc i64 %43 to i32
%45 = call i64 @realloc(%struct.fiemap* %40, i32 %44)
%46 = inttoptr i64 %45 to %struct.fiemap*
store %struct.fiemap* %46, %struct.fiemap** %4
%47 = icmp eq %struct.fiemap* %46, null
br i1 %47, label %48, label %51
48: ; preds = %33
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store %struct.fiemap* null, %struct.fiemap** %2
br label %74
51: ; preds = %33
%52 = load %struct.fiemap*, %struct.fiemap** %4
%53 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %52, i32 0, i32 2
%54 = load %struct.fiemap*, %struct.fiemap** %53
%55 = load i32, i32* %5
%56 = call i32 @memset(%struct.fiemap* %54, i32 0, i32 %55)
%57 = load %struct.fiemap*, %struct.fiemap** %4
%58 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load %struct.fiemap*, %struct.fiemap** %4
%61 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %60, i32 0, i32 0
store i32 %59, i32* %61
%62 = load %struct.fiemap*, %struct.fiemap** %4
%63 = getelementptr inbounds %struct.fiemap, %struct.fiemap* %62, i32 0, i32 1
store i32 0, i32* %63
%64 = load i32, i32* %3
%65 = load i32, i32* @FS_IOC_FIEMAP
%66 = load %struct.fiemap*, %struct.fiemap** %4
%67 = call i64 @ioctl(i32 %64, i32 %65, %struct.fiemap* %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %51
%70 = load i32, i32* @stderr
%71 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store %struct.fiemap* null, %struct.fiemap** %2
br label %74
72: ; preds = %51
%73 = load %struct.fiemap*, %struct.fiemap** %4
store %struct.fiemap* %73, %struct.fiemap** %2
br label %74
74: ; preds = %72, %69, %48, %30, %9
%75 = load %struct.fiemap*, %struct.fiemap** %2
ret %struct.fiemap* %75
}

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