#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 {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ fstat(int, struct stat *);
scalar_t__ ftruncate(int, size_t);
int /*<<< orphan*/ read(int, char *, int);
int alloc_pagecache(int fd, size_t size) {
  char buf[4096];
  struct stat st;
  int i;
  if (fstat(fd, &st))
    goto cleanup;
  size += st.st_size;
  if (ftruncate(fd, size))
    goto cleanup;
  for (i = 0; i < size; i += sizeof(buf))
    read(fd, buf, sizeof(buf));
  return 0;
cleanup:
  return -1;
}


x86 assembly (clang, -O0):
<alloc_pagecache>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1018(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
je     1160 <alloc_pagecache+0x30>
jmp    11dd <alloc_pagecache+0xad>
mov    -0x1018(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <ftruncate@plt>
cmp    $0x0,%rax
je     118a <alloc_pagecache+0x5a>
jmp    11dd <alloc_pagecache+0xad>
movl   $0x0,-0x101c(%rbp)
movslq -0x101c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11d1 <alloc_pagecache+0xa1>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
movslq -0x101c(%rbp),%rax
add    $0x1000,%rax
mov    %eax,-0x101c(%rbp)
jmp    1194 <alloc_pagecache+0x64>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <alloc_pagecache+0xb4>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_pagecache>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1050 <fstat@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    119d <alloc_pagecache+0x6d>
add    0x8(%rsp),%r12
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <ftruncate@plt>
test   %rax,%rax
jne    119d <alloc_pagecache+0x6d>
xor    %r14d,%r14d
test   %r12,%r12
je     119d <alloc_pagecache+0x6d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
add    $0x1000,%ebx
movslq %ebx,%rbx
cmp    %r12,%rbx
jb     1180 <alloc_pagecache+0x50>
mov    %r14d,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
define i32 @alloc_pagecache(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca [4096 x i8]
%7 = alloca %struct.stat
%8 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
%9 = load i32, i32* %4
%10 = call i64 @fstat(i32 %9, %struct.stat* %7)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
br label %39
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* %5
%17 = add i64 %16, %15
store i64 %17, i64* %5
%18 = load i32, i32* %4
%19 = load i64, i64* %5
%20 = call i64 @ftruncate(i32 %18, i64 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %13
br label %39
23: ; preds = %13
store i32 0, i32* %8
br label %24
24: ; preds = %33, %23
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = load i64, i64* %5
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i32, i32* %4
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 4096)
br label %33
33: ; preds = %29
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = add i64 %35, 4096
%37 = trunc i64 %36 to i32
store i32 %37, i32* %8
br label %24
38: ; preds = %24
store i32 0, i32* %3
br label %40
39: ; preds = %22, %12
store i32 -1, i32* %3
br label %40
40: ; preds = %39, %38
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.stat = type { i64 }
define i32 @alloc_pagecache(i32 %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i64 @fstat(i32 %0, %struct.stat* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = load i64, i64* %10
%12 = add i64 %11, %1
%13 = tail call i64 @ftruncate(i32 %0, i64 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %9
%16 = icmp eq i64 %12, 0
br i1 %16, label %24, label %17
17: ; preds = %15, %17
%18 = phi i64 [ %22, %17 ], [ 0, %15 ]
%19 = call i32 @read(i32 %0, i8* %5, i32 4096)
%20 = shl nsw i64 %18, 32
%21 = add i64 %20, 17592186044416
%22 = ashr exact i64 %21, 32
%23 = icmp ult i64 %22, %12
br i1 %23, label %17, label %24
24: ; preds = %17, %15, %2, %9
%25 = phi i32 [ -1, %9 ], [ -1, %2 ], [ 0, %15 ], [ 0, %17 ]
ret i32 %25
}