#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 */
unsigned int *LOG_wrk(char *, ...);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned int *malloc(int);
int open(char const *, int /*<<< orphan*/);
unsigned int *perror(char const *);
int read(int, unsigned int *, int);
char *strerror(int /*<<< orphan*/);
unsigned int *tlog_rf(const char *fname) {
  int fd = open(fname, O_RDONLY);
  if (fd < 0)
    return perror(fname), NULL;
  int n = lseek(fd, 0, SEEK_END);
  if (n < 0)
    return perror("lseek/e"), close(fd), NULL;
  if (n < 8)
    return LOG_wrk("tlog_rf: len=%d", n), close(fd), NULL;
  else
    n >>= 3;
  int r = lseek(fd, 0, SEEK_SET);
  if (r)
    return perror("lseek/0"), NULL;
  int siz = n + (n >> 6) + 3;
  unsigned int *buf = malloc(8 * siz);
  if ((r = read(fd, buf + 2, 8 * n)) != 8 * n)
    return LOG_wrk("read(%s): %d/%d: %s", fname, r, 8 * n,
                   r < 0 ? strerror(errno) : "???"),
           close(fd), NULL;
  buf[0] = 2 * n;
  return buf;
}


x86 assembly (clang, -O0):
<tlog_rf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c1 <tlog_rf+0x41>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <tlog_rf+0x1c8>
mov    -0x14(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1202 <tlog_rf+0x82>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <tlog_rf+0x1c8>
cmpl   $0x8,-0x18(%rbp)
jge    1232 <tlog_rf+0xb2>
mov    -0x18(%rbp),%esi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1060 <LOG_wrk@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <tlog_rf+0x1c8>
mov    -0x18(%rbp),%eax
sar    $0x3,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1274 <tlog_rf+0xf4>
lea    0xdb6(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <tlog_rf+0x1c8>
mov    -0x18(%rbp),%eax
mov    -0x18(%rbp),%ecx
sar    $0x6,%ecx
add    %ecx,%eax
add    $0x3,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
shl    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%edx
shl    $0x3,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
shl    $0x3,%ecx
cmp    %ecx,%eax
je     1334 <tlog_rf+0x1b4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x3,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12f3 <tlog_rf+0x173>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,-0x40(%rbp)
jmp    1303 <tlog_rf+0x183>
lea    0xd3a(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    1303 <tlog_rf+0x183>
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%edx
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%r8
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1060 <LOG_wrk@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1348 <tlog_rf+0x1c8>
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlog_rf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1287 <tlog_rf+0x107>
mov    %eax,%r13d
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
test   %eax,%eax
js     128c <tlog_rf+0x10c>
mov    %eax,%ebp
cmp    $0x7,%eax
ja     11e1 <tlog_rf+0x61>
lea    0xe38(%rip),%rdi
xor    %r15d,%r15d
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <LOG_wrk@plt>
jmp    126d <tlog_rf+0xed>
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
test   %eax,%eax
jne    12a5 <tlog_rf+0x125>
mov    %ebp,%ebx
shr    $0x3,%ebx
mov    %ebp,%eax
shr    $0x9,%eax
add    %ebx,%eax
lea    0x18(,%rax,8),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0x8(%rax),%rsi
and    $0xfffffff8,%ebp
mov    %r13d,%edi
mov    %ebp,%edx
call   1050 <read@plt>
cmp    %ebp,%eax
jne    1233 <tlog_rf+0xb3>
add    %ebx,%ebx
mov    %ebx,(%r15)
jmp    1275 <tlog_rf+0xf5>
mov    %eax,%r12d
test   %eax,%eax
js     1243 <tlog_rf+0xc3>
lea    0xdf3(%rip),%r8
jmp    1254 <tlog_rf+0xd4>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%r8
lea    0xdc5(%rip),%rdi
xor    %r15d,%r15d
mov    %r14,%rsi
mov    %r12d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1060 <LOG_wrk@plt>
mov    %r13d,%edi
call   1040 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
jmp    12ac <tlog_rf+0x12c>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
mov    %r13d,%edi
call   1040 <close@plt>
xor    %r15d,%r15d
jmp    1275 <tlog_rf+0xf5>
lea    0xd6c(%rip),%rdi
call   1090 <perror@plt>
xor    %r15d,%r15d
jmp    1275 <tlog_rf+0xf5>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@.str = [8 x i8] c"lseek/e\00"
@.str.1 = [16 x i8] c"tlog_rf: len=%d\00"
@SEEK_SET = global i32 0
@.str.2 = [8 x i8] c"lseek/0\00"
@.str.3 = [20 x i8] c"read(%s): %d/%d: %s\00"
@errno = global i32 0
@.str.4 = [4 x i8] c"???\00"
define i32* @tlog_rf(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32* @perror(i8* %15)
store i32* null, i32** %2
br label %86
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = call i32* @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32* null, i32** %2
br label %86
27: ; preds = %17
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 8
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %5
%32 = call i32* (i8*, ...) @LOG_wrk(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
store i32* null, i32** %2
br label %86
35: ; preds = %27
%36 = load i32, i32* %5
%37 = ashr i32 %36, 3
store i32 %37, i32* %5
br label %38
38: ; preds = %35
%39 = load i32, i32* %4
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @lseek(i32 %39, i32 0, i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32* @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32* null, i32** %2
br label %86
46: ; preds = %38
%47 = load i32, i32* %5
%48 = load i32, i32* %5
%49 = ashr i32 %48, 6
%50 = add nsw i32 %47, %49
%51 = add nsw i32 %50, 3
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = mul nsw i32 8, %52
%54 = call i32* @malloc(i32 %53)
store i32* %54, i32** %8
%55 = load i32, i32* %4
%56 = load i32*, i32** %8
%57 = getelementptr inbounds i32, i32* %56, i64 2
%58 = load i32, i32* %5
%59 = mul nsw i32 8, %58
%60 = call i32 @read(i32 %55, i32* %57, i32 %59)
store i32 %60, i32* %6
%61 = load i32, i32* %5
%62 = mul nsw i32 8, %61
%63 = icmp ne i32 %60, %62
br i1 %63, label %64, label %80
64: ; preds = %46
%65 = load i8*, i8** %3
%66 = load i32, i32* %6
%67 = load i32, i32* %5
%68 = mul nsw i32 8, %67
%69 = load i32, i32* %6
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %64
%72 = load i32, i32* @errno
%73 = call i8* @strerror(i32 %72)
br label %75
74: ; preds = %64
br label %75
75: ; preds = %74, %71
%76 = phi i8* [ %73, %71 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), %74 ]
%77 = call i32* (i8*, ...) @LOG_wrk(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %65, i32 %66, i32 %68, i8* %76)
%78 = load i32, i32* %4
%79 = call i32 @close(i32 %78)
store i32* null, i32** %2
br label %86
80: ; preds = %46
%81 = load i32, i32* %5
%82 = mul nsw i32 2, %81
%83 = load i32*, i32** %8
%84 = getelementptr inbounds i32, i32* %83, i64 0
store i32 %82, i32* %84
%85 = load i32*, i32** %8
store i32* %85, i32** %2
br label %86
86: ; preds = %80, %75, %44, %30, %23, %14
%87 = load i32*, i32** %2
ret i32* %87
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@.str = [8 x i8] c"lseek/e\00"
@.str.1 = [16 x i8] c"tlog_rf: len=%d\00"
@SEEK_SET = global i32 0
@.str.2 = [8 x i8] c"lseek/0\00"
@.str.3 = [20 x i8] c"read(%s): %d/%d: %s\00"
@errno = global i32 0
@.str.4 = [4 x i8] c"???\00"
define noalias i32* @tlog_rf(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32* @perror(i8* %0)
br label %47
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @lseek(i32 %3, i32 0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32* @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @close(i32 %3)
br label %47
14: ; preds = %7
%15 = icmp ult i32 %9, 8
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = tail call i32* (i8*, ...) @LOG_wrk(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%18 = tail call i32 @close(i32 %3)
br label %47
19: ; preds = %14
%20 = lshr i32 %9, 3
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %3, i32 0, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = tail call i32* @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %47
26: ; preds = %19
%27 = lshr i32 %9, 9
%28 = add nuw nsw i32 %20, %27
%29 = shl nuw i32 %28, 3
%30 = add nuw i32 %29, 24
%31 = tail call i32* @malloc(i32 %30)
%32 = getelementptr inbounds i32, i32* %31, i64 2
%33 = and i32 %9, -8
%34 = tail call i32 @read(i32 %3, i32* %32, i32 %33)
%35 = icmp eq i32 %34, %33
br i1 %35, label %45, label %36
36: ; preds = %26
%37 = icmp slt i32 %34, 0
br i1 %37, label %38, label %41
38: ; preds = %36
%39 = load i32, i32* @errno
%40 = tail call i8* @strerror(i32 %39)
br label %41
41: ; preds = %36, %38
%42 = phi i8* [ %40, %38 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), %36 ]
%43 = tail call i32* (i8*, ...) @LOG_wrk(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %34, i32 %33, i8* %42)
%44 = tail call i32 @close(i32 %3)
br label %47
45: ; preds = %26
%46 = shl nuw nsw i32 %20, 1
store i32 %46, i32* %31
br label %47
47: ; preds = %11, %16, %41, %45, %24, %5
%48 = phi i32* [ null, %5 ], [ null, %11 ], [ null, %16 ], [ null, %24 ], [ null, %41 ], [ %31, %45 ]
ret i32* %48
}