#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int rl_flock(int /*<<< orphan*/ *, int);
int rl_read_wal(const char *wal_path, unsigned char **_data, size_t *_datalen) {
  int retval = 0;
  char *data = NULL;
  size_t datalen;
  FILE *fp = fopen(wal_path, "rb");
  if (fp == NULL) {
    goto cleanup;
  }
  retval = rl_flock(fp, 2);
  if (0 != retval)
    goto cleanup;
  ;
  fseek(fp, 0, SEEK_END);
  datalen = (size_t)ftell(fp);
  fseek(fp, 0, SEEK_SET);
  data = malloc(datalen * sizeof(char));
  fread(data, datalen, 1, fp);
  fclose(fp);
cleanup:
  if (retval == 0) {
    *_data = (unsigned char *)data;
    *_datalen = datalen;
  }
  return retval;
}


x86 assembly (clang, -O0):
<rl_read_wal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b7 <rl_read_wal+0x47>
jmp    123d <rl_read_wal+0xcd>
mov    -0x38(%rbp),%rdi
mov    $0x2,%esi
call   1040 <rl_flock@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
je     11d8 <rl_read_wal+0x68>
jmp    123d <rl_read_wal+0xcd>
mov    -0x38(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    125d <rl_read_wal+0xed>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rl_read_wal>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe7a(%rip),%rsi
call   1090 <fopen@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1200 <rl_read_wal+0x90>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x2,%esi
call   1040 <rl_flock@plt>
test   %eax,%eax
jne    1208 <rl_read_wal+0x98>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1200 <rl_read_wal+0x90>
mov    %r13,(%r15)
mov    %r12,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @rl_read_wal(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
store i32 0, i32* %7
store i8* null, i8** %8
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
br label %40
16: ; preds = %3
%17 = load i32*, i32** %10
%18 = call i32 @rl_flock(i32* %17, i32 2)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 0, %19
br i1 %20, label %21, label %22
21: ; preds = %16
br label %40
22: ; preds = %16
%23 = load i32*, i32** %10
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32*, i32** %10
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %9
%28 = load i32*, i32** %10
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i64, i64* %9
%32 = mul i64 %31, 1
%33 = call i8* @malloc(i64 %32)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = load i64, i64* %9
%36 = load i32*, i32** %10
%37 = call i32 @fread(i8* %34, i64 %35, i32 1, i32* %36)
%38 = load i32*, i32** %10
%39 = call i32 @fclose(i32* %38)
br label %40
40: ; preds = %22, %21, %15
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i8*, i8** %8
%45 = load i8**, i8*** %5
store i8* %44, i8** %45
%46 = load i64, i64* %9
%47 = load i64*, i64** %6
store i64 %46, i64* %47
br label %48
48: ; preds = %43, %40
%49 = load i32, i32* %7
ret i32 %49
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @rl_read_wal(i8* nocapture %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = tail call i32 @rl_flock(i32* %4, i32 2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %6
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %4)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %4, i32 0, i32 %13)
%15 = tail call i8* @malloc(i64 %12)
%16 = tail call i32 @fread(i8* %15, i64 %12, i32 1, i32* %4)
%17 = tail call i32 @fclose(i32* %4)
br label %18
18: ; preds = %3, %9
%19 = phi i8* [ %15, %9 ], [ null, %3 ]
%20 = phi i64 [ %12, %9 ], [ undef, %3 ]
store i8* %19, i8** %1
store i64 %20, i64* %2
br label %21
21: ; preds = %6, %18
%22 = phi i32 [ 0, %18 ], [ %7, %6 ]
ret i32 %22
}