#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*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int *get_blocks(const char *filename, int *num_blocks) {
  int fd, block_size, i;
  int *blocks;
  struct stat statinfo;
  if ((fd = open(filename, O_RDONLY)) < 0) {
    fprintf(stderr, "Cannot open %s\n", filename);
    return NULL;
  }
  if (ioctl(fd, FIGETBSZ, &block_size) < 0) {
    fprintf(stderr, "Cannot get block size\n");
    close(fd);
    return NULL;
  }
  if (fstat(fd, &statinfo) < 0) {
    fprintf(stderr, "Cannot stat %s\n", filename);
    close(fd);
    return NULL;
  }
  *num_blocks = (statinfo.st_size + block_size - 1) / block_size;
  blocks = malloc(sizeof(int) * *num_blocks);
  if (!blocks) {
    fprintf(stderr, "Cannot allocate buffer for %d blocks\n", *num_blocks);
    close(fd);
    return NULL;
  }
  for (i = 0; i < *num_blocks; i++) {
    blocks[i] = i;
    if (ioctl(fd, FIBMAP, &blocks[i]) < 0)
      blocks[i] = 0;
  }
  close(fd);
  return blocks;
}


x86 assembly (clang, -O0):
<get_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b6 <get_blocks+0x56>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    132f <get_blocks+0x1cf>
mov    -0x1c(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1201 <get_blocks+0xa1>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    132f <get_blocks+0x1cf>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jge    1247 <get_blocks+0xe7>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    132f <get_blocks+0x1cf>
mov    -0x38(%rbp),%eax
add    -0x20(%rbp),%eax
sub    $0x1,%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12af <get_blocks+0x14f>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    132f <get_blocks+0x1cf>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    131f <get_blocks+0x1bf>
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    130c <get_blocks+0x1ac>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
jmp    1311 <get_blocks+0x1b1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12b6 <get_blocks+0x156>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     124a <get_blocks+0xea>
mov    %eax,%r12d
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %r12d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     1269 <get_blocks+0x109>
mov    %rsp,%rsi
mov    %r12d,%edi
call   1080 <fstat@plt>
test   %rax,%rax
js     1285 <get_blocks+0x125>
mov    (%rsp),%eax
mov    0x4(%rsp),%ecx
add    %ecx,%eax
add    $0xffffffff,%eax
cltd
idiv   %ecx
mov    %eax,%r15d
mov    %eax,(%r14)
lea    0x0(,%r15,4),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12a4 <get_blocks+0x144>
mov    %rax,%r13
test   %r15d,%r15d
jle    12c1 <get_blocks+0x161>
xor    %ebp,%ebp
mov    0x2dcb(%rip),%r15
xor    %ebx,%ebx
jmp    1224 <get_blocks+0xc4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
movslq (%r14),%rax
add    $0x4,%rbp
cmp    %rax,%rbx
jge    12c1 <get_blocks+0x161>
mov    %r13,%rdx
add    %rbp,%rdx
mov    %ebx,0x0(%r13,%rbx,4)
mov    (%r15),%esi
mov    %r12d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jns    1210 <get_blocks+0xb0>
movl   $0x0,0x0(%r13,%rbx,4)
jmp    1210 <get_blocks+0xb0>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
xor    %r13d,%r13d
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12c9 <get_blocks+0x169>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
xor    %r13d,%r13d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12c1 <get_blocks+0x161>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd92(%rip),%rsi
xor    %r13d,%r13d
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12c1 <get_blocks+0x161>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
xor    %r13d,%r13d
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r13,%rax
add    $0x8,%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
@stderr = global i32 0
@.str = [16 x i8] c"Cannot open %s\0A\00"
@FIGETBSZ = global i32 0
@.str.1 = [23 x i8] c"Cannot get block size\0A\00"
@.str.2 = [16 x i8] c"Cannot stat %s\0A\00"
@.str.3 = [38 x i8] c"Cannot allocate buffer for %d blocks\0A\00"
@FIBMAP = global i32 0
define i32* @get_blocks(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.stat
store i8* %0, i8** %4
store i32* %1, i32** %5
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32* null, i32** %3
br label %96
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @FIGETBSZ
%22 = call i64 @ioctl(i32 %20, i32 %21, i32* %7)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
store i32* null, i32** %3
br label %96
29: ; preds = %19
%30 = load i32, i32* %6
%31 = call i64 @fstat(i32 %30, %struct.stat* %10)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %4
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32* null, i32** %3
br label %96
39: ; preds = %29
%40 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i32, i32* %7
%43 = add nsw i32 %41, %42
%44 = sub nsw i32 %43, 1
%45 = load i32, i32* %7
%46 = sdiv i32 %44, %45
%47 = load i32*, i32** %5
store i32 %46, i32* %47
%48 = load i32*, i32** %5
%49 = load i32, i32* %48
%50 = sext i32 %49 to i64
%51 = mul i64 4, %50
%52 = trunc i64 %51 to i32
%53 = call i32* @malloc(i32 %52)
store i32* %53, i32** %9
%54 = load i32*, i32** %9
%55 = icmp ne i32* %54, null
br i1 %55, label %63, label %56
56: ; preds = %39
%57 = load i32, i32* @stderr
%58 = load i32*, i32** %5
%59 = load i32, i32* %58
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
store i32* null, i32** %3
br label %96
63: ; preds = %39
store i32 0, i32* %8
br label %64
64: ; preds = %89, %63
%65 = load i32, i32* %8
%66 = load i32*, i32** %5
%67 = load i32, i32* %66
%68 = icmp slt i32 %65, %67
br i1 %68, label %69, label %92
69: ; preds = %64
%70 = load i32, i32* %8
%71 = load i32*, i32** %9
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
store i32 %70, i32* %74
%75 = load i32, i32* %6
%76 = load i32, i32* @FIBMAP
%77 = load i32*, i32** %9
%78 = load i32, i32* %8
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
%81 = call i64 @ioctl(i32 %75, i32 %76, i32* %80)
%82 = icmp slt i64 %81, 0
br i1 %82, label %83, label %88
83: ; preds = %69
%84 = load i32*, i32** %9
%85 = load i32, i32* %8
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
store i32 0, i32* %87
br label %88
88: ; preds = %83, %69
br label %89
89: ; preds = %88
%90 = load i32, i32* %8
%91 = add nsw i32 %90, 1
store i32 %91, i32* %8
br label %64
92: ; preds = %64
%93 = load i32, i32* %6
%94 = call i32 @close(i32 %93)
%95 = load i32*, i32** %9
store i32* %95, i32** %3
br label %96
96: ; preds = %92, %56, %33, %24, %15
%97 = load i32*, i32** %3
ret i32* %97
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"Cannot open %s\0A\00"
@FIGETBSZ = global i32 0
@.str.1 = [23 x i8] c"Cannot get block size\0A\00"
@.str.2 = [16 x i8] c"Cannot stat %s\0A\00"
@.str.3 = [38 x i8] c"Cannot allocate buffer for %d blocks\0A\00"
@FIBMAP = global i32 0
define i32* @get_blocks(i8* %0, i32* nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %59
13: ; preds = %2
%14 = load i32, i32* @FIGETBSZ
%15 = call i64 @ioctl(i32 %8, i32 %14, i32* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @close(i32 %8)
br label %59
21: ; preds = %13
%22 = call i64 @fstat(i32 %8, %struct.stat* %4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%27 = call i32 @close(i32 %8)
br label %59
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* %3
%32 = add i32 %30, -1
%33 = add i32 %32, %31
%34 = sdiv i32 %33, %31
store i32 %34, i32* %1
%35 = shl i32 %34, 2
%36 = call i32* @malloc(i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %40, label %38
38: ; preds = %28
%39 = icmp sgt i32 %34, 0
br i1 %39, label %44, label %57
40: ; preds = %28
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%43 = call i32 @close(i32 %8)
br label %59
44: ; preds = %38, %52
%45 = phi i64 [ %53, %52 ], [ 0, %38 ]
%46 = getelementptr inbounds i32, i32* %36, i64 %45
%47 = trunc i64 %45 to i32
store i32 %47, i32* %46
%48 = load i32, i32* @FIBMAP
%49 = call i64 @ioctl(i32 %8, i32 %48, i32* %46)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 0, i32* %46
br label %52
52: ; preds = %44, %51
%53 = add nuw nsw i64 %45, 1
%54 = load i32, i32* %1
%55 = sext i32 %54 to i64
%56 = icmp slt i64 %53, %55
br i1 %56, label %44, label %57
57: ; preds = %52, %38
%58 = call i32 @close(i32 %8)
br label %59
59: ; preds = %57, %40, %24, %17, %10
%60 = phi i32* [ null, %10 ], [ null, %17 ], [ null, %24 ], [ %36, %57 ], [ null, %40 ]
ret i32* %60
}