#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 */
typedef int off_t;

/* Variables and functions */
scalar_t__ EOVERFLOW;
scalar_t__ errno;
int eslib_file_read_full(char *, char *, size_t, size_t *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *);
char *realloc(char *, size_t);
char *strerror(scalar_t__);
off_t eslib_procfs_readfile(char *path, char **out) {
  size_t size;
  size_t len = 0;
  char *buf;
  int r;
  size = 4096;
  buf = malloc(size);
  if (buf == NULL) {
    printf("malloc(): %s\n", strerror(errno));
    return -1;
  }
  while (size <= (4096 * 100000)) {
    r = eslib_file_read_full(path, buf, size - 1, &len);
    if (r == 0) {
      break;
    } else {
      char *re_buf = NULL;
      if (errno != EOVERFLOW) {
        printf("file_read_full(): %s\n", strerror(errno));
        goto err_free;
      }
      if (size * 2 <= size || size * 2 > (4096 * 100000)) {
        errno = EOVERFLOW;
        goto err_free;
      }
      size *= 2;
      re_buf = realloc(buf, size);
      if (re_buf == NULL) {
        printf("realloc(): %s\n", strerror(errno));
        goto err_free;
      }
      buf = re_buf;
    }
  }
  if (len == 0)
    goto err_free;
  buf[len] = '\0';
  *out = buf;
  return (off_t)len;
err_free:
  free(buf);
  return -1;
}


x86 assembly (clang, -O0):
<eslib_procfs_readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x1000,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c2 <eslib_procfs_readfile+0x62>
mov    0x2e39(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131f <eslib_procfs_readfile+0x1bf>
jmp    11c7 <eslib_procfs_readfile+0x67>
cmpq   $0x186a0000,-0x20(%rbp)
18
ja     12dc <eslib_procfs_readfile+0x17c>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
lea    -0x28(%rbp),%rcx
call   1050 <eslib_file_read_full@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1200 <eslib_procfs_readfile+0xa0>
jmp    12dc <eslib_procfs_readfile+0x17c>
movq   $0x0,-0x40(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
je     1245 <eslib_procfs_readfile+0xe5>
mov    0x2daf(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdd3(%rip),%rdi
call   1040 <printf@plt>
jmp    130f <eslib_procfs_readfile+0x1af>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
cmp    -0x20(%rbp),%rax
jbe    126b <eslib_procfs_readfile+0x10b>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
cmp    $0x186a0000,%rax
jbe    1284 <eslib_procfs_readfile+0x124>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rcx
mov    0x2d5c(%rip),%rax
mov    %rcx,(%rax)
jmp    130f <eslib_procfs_readfile+0x1af>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12cf <eslib_procfs_readfile+0x16f>
mov    0x2d25(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd5f(%rip),%rdi
call   1040 <printf@plt>
jmp    130f <eslib_procfs_readfile+0x1af>
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11c7 <eslib_procfs_readfile+0x67>
cmpq   $0x0,-0x28(%rbp)
jne    12ec <eslib_procfs_readfile+0x18c>
jmp    130f <eslib_procfs_readfile+0x1af>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    131f <eslib_procfs_readfile+0x1bf>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_procfs_readfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
mov    $0x1000,%r12d
mov    $0x1000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11fe <eslib_procfs_readfile+0x9e>
mov    %rbx,0x10(%rsp)
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
mov    %rax,%rbx
lea    -0x1(%r12),%rdx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r13,%rcx
call   1050 <eslib_file_read_full@plt>
test   %eax,%eax
je     121e <eslib_procfs_readfile+0xbe>
mov    0x2e17(%rip),%rax
mov    (%rax),%rbp
mov    0x2e25(%rip),%rax
cmp    (%rax),%rbp
jne    1236 <eslib_procfs_readfile+0xd6>
cmp    $0xc350000,%r12
ja     1250 <eslib_procfs_readfile+0xf0>
lea    (%r12,%r12,1),%r14
cmp    %r12,%r14
jbe    1250 <eslib_procfs_readfile+0xf0>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <realloc@plt>
mov    %r14,%r12
test   %rax,%rax
jne    11a0 <eslib_procfs_readfile+0x40>
lea    0xe28(%rip),%r14
jmp    123d <eslib_procfs_readfile+0xdd>
mov    0x2dd3(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdec(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    1258 <eslib_procfs_readfile+0xf8>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1250 <eslib_procfs_readfile+0xf0>
movb   $0x0,(%rbx,%rax,1)
mov    0x10(%rsp),%rcx
mov    %rbx,(%rcx)
jmp    125d <eslib_procfs_readfile+0xfd>
lea    0xdd1(%rip),%r14
mov    %rbp,%rdi
call   1080 <strerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"malloc(): %s\0A\00"
@errno = global i64 0
@EOVERFLOW = global i64 0
@.str.1 = [22 x i8] c"file_read_full(): %s\0A\00"
@.str.2 = [15 x i8] c"realloc(): %s\0A\00"
define i32 @eslib_procfs_readfile(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%5 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i64, i64* @errno
%9 = tail call i8* @strerror(i64 %8)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %41
11: ; preds = %2, %26
%12 = phi i64 [ %22, %26 ], [ 4096, %2 ]
%13 = phi i8* [ %27, %26 ], [ %5, %2 ]
%14 = add nsw i64 %12, -1
%15 = call i32 @eslib_file_read_full(i8* %0, i8* %13, i64 %14, i64* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EOVERFLOW
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = shl nuw nsw i64 %12, 1
%23 = icmp ule i64 %22, %12
%24 = icmp ugt i64 %12, 204800000
%25 = or i1 %24, %23
br i1 %25, label %39, label %26
26: ; preds = %21
%27 = call i8* @realloc(i8* %13, i64 %22)
%28 = icmp eq i8* %27, null
br i1 %28, label %35, label %11
29: ; preds = %11
%30 = load i64, i64* %3
%31 = icmp eq i64 %30, 0
br i1 %31, label %39, label %32
32: ; preds = %29
%33 = getelementptr inbounds i8, i8* %13, i64 %30
store i8 0, i8* %33
store i8* %13, i8** %1
%34 = trunc i64 %30 to i32
br label %41
35: ; preds = %26, %17
%36 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %26 ]
%37 = call i8* @strerror(i64 %18)
%38 = call i32 @printf(i8* %36, i8* %37)
br label %39
39: ; preds = %21, %35, %29
%40 = call i32 @free(i8* %13)
br label %41
41: ; preds = %39, %32, %7
%42 = phi i32 [ -1, %7 ], [ -1, %39 ], [ %34, %32 ]
ret i32 %42
}