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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
int read8(int fd, off_t addr, uint8_t *val) {
  if (lseek(fd, addr, SEEK_SET) != addr) {
    perror("persist: read8: lseek");
    return (1);
  }
  uint8_t buf;
  if (read(fd, &buf, 1) < 1) {
    fprintf(stderr, "persist: read8: short read\n");
    return (1);
  }
  if (val)
    *val = buf;
  return (0);
}


x86 assembly (clang, -O0):
<read8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    -0x10(%rbp),%rax
je     118a <read8+0x4a>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <read8+0xa0>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jge    11c5 <read8+0x85>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <read8+0xa0>
cmpq   $0x0,-0x18(%rbp)
je     11d9 <read8+0x99>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read8>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    %rbx,%rax
jne    11ad <read8+0x6d>
lea    0xc(%rsp),%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    118d <read8+0x4d>
xor    %ebx,%ebx
test   %r14,%r14
je     11a2 <read8+0x62>
mov    0xc(%rsp),%eax
mov    %eax,(%r14)
jmp    11a2 <read8+0x62>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    11a2 <read8+0x62>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [22 x i8] c"persist: read8: lseek\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"persist: read8: short read\0A\00"
define i32 @read8(i32 %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = load i64, i64* %6
%11 = load i32, i32* @SEEK_SET
%12 = call i64 @lseek(i32 %9, i64 %10, i32 %11)
%13 = load i64, i64* %6
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %31
17: ; preds = %3
%18 = load i32, i32* %5
%19 = call i32 @read(i32 %18, i32* %8, i32 1)
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %31
24: ; preds = %17
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %8
%29 = load i32*, i32** %7
store i32 %28, i32* %29
br label %30
30: ; preds = %27, %24
store i32 0, i32* %4
br label %31
31: ; preds = %30, %21, %15
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [22 x i8] c"persist: read8: lseek\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"persist: read8: short read\0A\00"
define i32 @read8(i32 %0, i64 %1, i32* writeonly %2) {
%4 = alloca i32
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i64 %1, i32 %5)
%7 = icmp eq i64 %6, %1
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %3
%11 = bitcast i32* %4 to i8*
%12 = call i32 @read(i32 %0, i32* %4, i32 1)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %21
17: ; preds = %10
%18 = icmp eq i32* %2, null
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = load i32, i32* %4
store i32 %20, i32* %2
br label %21
21: ; preds = %17, %19, %14
%22 = phi i32 [ 1, %14 ], [ 0, %19 ], [ 0, %17 ]
br label %23
23: ; preds = %21, %8
%24 = phi i32 [ 1, %8 ], [ %22, %21 ]
ret i32 %24
}