#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*/ assert(int);
int /*<<< orphan*/ free(char *);
int get_fileSize(int);
int /*<<< orphan*/ perror(char *);
int posix_memalign(char **, int, int);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int my_fallocate(int fd, int offset, int len) {
  assert(offset == 0);
  printf("In my_fallocate\n");
  int blksize = 512;
  char *buffer = NULL;
  int ret = posix_memalign(&buffer, blksize, blksize);
  if (ret) {
    perror("posix_memalign");
    return -1;
  }
  for (; offset < len; offset += 512) {
    int leftSize = 512;
    int nwrite;
    while (leftSize > 0) {
      nwrite = write(fd, buffer, leftSize);
      if (nwrite < 0)
        return -1;
      leftSize -= nwrite;
    }
  }
  free(buffer);
  printf("new fileSize = %d\n", get_fileSize(fd));
  return 0;
}


x86 assembly (clang, -O0):
<my_fallocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x200,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1090 <posix_memalign@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11e3 <my_fallocate+0x73>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <my_fallocate+0x10a>
jmp    11e8 <my_fallocate+0x78>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1252 <my_fallocate+0xe2>
movl   $0x200,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    123d <my_fallocate+0xcd>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    122d <my_fallocate+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <my_fallocate+0x10a>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
jmp    11fb <my_fallocate+0x8b>
jmp    1242 <my_fallocate+0xd2>
mov    -0xc(%rbp),%eax
add    $0x200,%eax
mov    %eax,-0xc(%rbp)
jmp    11e8 <my_fallocate+0x78>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1060 <get_fileSize@plt>
mov    %eax,%esi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fallocate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebx
xor    %edi,%edi
test   %esi,%esi
sete   %dil
call   1080 <assert@plt>
lea    0xe7f(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,(%rsp)
mov    %rsp,%rdi
mov    $0x200,%esi
mov    $0x200,%edx
call   10a0 <posix_memalign@plt>
test   %eax,%eax
jne    1217 <my_fallocate+0x97>
cmp    %r14d,%r15d
jge    11f3 <my_fallocate+0x73>
mov    $0x200,%ebp
mov    (%rsp),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1050 <write@plt>
test   %eax,%eax
js     1223 <my_fallocate+0xa3>
sub    %eax,%ebp
test   %ebp,%ebp
jg     11d0 <my_fallocate+0x50>
add    $0x200,%r15d
cmp    %r14d,%r15d
jl     11cb <my_fallocate+0x4b>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1070 <get_fileSize@plt>
lea    0xe05(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1228 <my_fallocate+0xa8>
lea    0xde2(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"In my_fallocate\0A\00"
@.str.1 = [15 x i8] c"posix_memalign\00"
@.str.2 = [19 x i8] c"new fileSize = %d\0A\00"
define i32 @my_fallocate(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 512, i32* %8
store i8* null, i8** %9
%18 = load i32, i32* %8
%19 = load i32, i32* %8
%20 = call i32 @posix_memalign(i8** %9, i32 %18, i32 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %56
25: ; preds = %3
br label %26
26: ; preds = %47, %25
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %50
30: ; preds = %26
store i32 512, i32* %11
br label %31
31: ; preds = %42, %30
%32 = load i32, i32* %11
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %31
%35 = load i32, i32* %5
%36 = load i8*, i8** %9
%37 = load i32, i32* %11
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
store i32 %38, i32* %12
%39 = load i32, i32* %12
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 -1, i32* %4
br label %56
42: ; preds = %34
%43 = load i32, i32* %12
%44 = load i32, i32* %11
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %11
br label %31
46: ; preds = %31
br label %47
47: ; preds = %46
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 512
store i32 %49, i32* %6
br label %26
50: ; preds = %26
%51 = load i8*, i8** %9
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %5
%54 = call i32 @get_fileSize(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %54)
store i32 0, i32* %4
br label %56
56: ; preds = %50, %41, %23
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"posix_memalign\00"
@.str.2 = [19 x i8] c"new fileSize = %d\0A\00"
@str = [16 x i8] c"In my_fallocate\00"
define i32 @my_fallocate(i32 %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = icmp eq i32 %1, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%9 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%10 = call i32 @posix_memalign(i8** %4, i32 512, i32 512)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = icmp slt i32 %1, %2
br i1 %13, label %16, label %29
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %34
16: ; preds = %12, %26
%17 = phi i32 [ %27, %26 ], [ %1, %12 ]
br label %20
18: ; preds = %20
%19 = icmp sgt i32 %25, 0
br i1 %19, label %20, label %26
20: ; preds = %16, %18
%21 = phi i32 [ 512, %16 ], [ %25, %18 ]
%22 = load i8*, i8** %4
%23 = call i32 @write(i32 %0, i8* %22, i32 %21)
%24 = icmp slt i32 %23, 0
%25 = sub nsw i32 %21, %23
br i1 %24, label %34, label %18
26: ; preds = %18
%27 = add nsw i32 %17, 512
%28 = icmp slt i32 %27, %2
br i1 %28, label %16, label %29
29: ; preds = %26, %12
%30 = load i8*, i8** %4
%31 = call i32 @free(i8* %30)
%32 = call i32 @get_fileSize(i32 %0)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %20, %29, %14
%35 = phi i32 [ -1, %14 ], [ 0, %29 ], [ -1, %20 ]
ret i32 %35
}