#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 log_header_v1 {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int errno;
int log_scan(void *, scalar_t__, long long, int /*<<< orphan*/, int,
             struct log_header_v1 *, void **, int *, unsigned int *);
scalar_t__ pread(int, void *, int, long long);
int /*<<< orphan*/ printf(char *, ...);
int read_record(struct log_header_v1 *lh, void *buf, int maxlen, int fh,
                long long pos, void **payload, int *payload_len,
                unsigned int *seq_nr) {
  ssize_t status;
  status = pread(fh, buf, maxlen, pos);
  if (status < 0) {
    printf("cannot pread() %d bytes, status = %d\n", maxlen, (int)status);
    return -errno;
  }
  if (!status) {
    printf("got EOF\n");
    return 0;
  }
  return log_scan(buf, status, pos, 0, 1, lh, payload, payload_len, seq_nr);
}


x86 assembly (clang, -O0):
<read_record>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1050 <pread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11a2 <read_record+0x72>
mov    -0x1c(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    %eax,%edx
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2e48(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <read_record+0xd1>
cmpq   $0x0,-0x38(%rbp)
jne    11c7 <read_record+0x97>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1201 <read_record+0xd1>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%r9
mov    -0x30(%rbp),%r11
mov    0x10(%rbp),%r10
mov    0x18(%rbp),%rax
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1030 <log_scan@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_record>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9,%r14
mov    %r8,%r12
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %ecx,%edi
mov    %r8,%rcx
call   1060 <pread@plt>
test   %rax,%rax
js     1194 <read_record+0x54>
je     11b3 <read_record+0x73>
sub    $0x8,%rsp
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    %r15,%r9
push   0x40(%rsp)
push   0x40(%rsp)
push   %r14
call   1040 <log_scan@plt>
add    $0x20,%rsp
jmp    11c3 <read_record+0x83>
lea    0xe65(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e31(%rip),%rax
sub    (%rax),%ebx
jmp    11c1 <read_record+0x81>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.log_header_v1 = type { i32 }
@.str = [38 x i8] c"cannot pread() %d bytes, status = %d\0A\00"
@errno = global i32 0
@.str.1 = [9 x i8] c"got EOF\0A\00"
define i32 @read_record(%struct.log_header_v1* %0, i8* %1, i32 %2, i32 %3, i64 %4, i8** %5, i32* %6, i32* %7) {
%9 = alloca i32
%10 = alloca %struct.log_header_v1*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8**
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i64
store %struct.log_header_v1* %0, %struct.log_header_v1** %10
store i8* %1, i8** %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store i64 %4, i64* %14
store i8** %5, i8*** %15
store i32* %6, i32** %16
store i32* %7, i32** %17
%19 = load i32, i32* %13
%20 = load i8*, i8** %11
%21 = load i32, i32* %12
%22 = load i64, i64* %14
%23 = call i64 @pread(i32 %19, i8* %20, i32 %21, i64 %22)
store i64 %23, i64* %18
%24 = load i64, i64* %18
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %8
%27 = load i32, i32* %12
%28 = load i64, i64* %18
%29 = trunc i64 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %29)
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %9
br label %47
33: ; preds = %8
%34 = load i64, i64* %18
%35 = icmp ne i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %9
br label %47
38: ; preds = %33
%39 = load i8*, i8** %11
%40 = load i64, i64* %18
%41 = load i64, i64* %14
%42 = load %struct.log_header_v1*, %struct.log_header_v1** %10
%43 = load i8**, i8*** %15
%44 = load i32*, i32** %16
%45 = load i32*, i32** %17
%46 = call i32 @log_scan(i8* %39, i64 %40, i64 %41, i32 0, i32 1, %struct.log_header_v1* %42, i8** %43, i32* %44, i32* %45)
store i32 %46, i32* %9
br label %47
47: ; preds = %38, %36, %26
%48 = load i32, i32* %9
ret i32 %48
}

LLVM IR (-O2):
%struct.log_header_v1 = type { i32 }
@.str = [38 x i8] c"cannot pread() %d bytes, status = %d\0A\00"
@errno = global i32 0
@str = [8 x i8] c"got EOF\00"
define i32 @read_record(%struct.log_header_v1* %0, i8* %1, i32 %2, i32 %3, i64 %4, i8** %5, i32* %6, i32* %7) {
%9 = tail call i64 @pread(i32 %3, i8* %1, i32 %2, i64 %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = trunc i64 %9 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %12)
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
br label %22
16: ; preds = %8
%17 = icmp eq i64 %9, 0
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %22
20: ; preds = %16
%21 = tail call i32 @log_scan(i8* %1, i64 %9, i64 %4, i32 0, i32 1, %struct.log_header_v1* %0, i8** %5, i32* %6, i32* %7)
br label %22
22: ; preds = %20, %18, %11
%23 = phi i32 [ %15, %11 ], [ %21, %20 ], [ 0, %18 ]
ret i32 %23
}