#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 uint8_t;
typedef int uint32_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 *, int);
int /*<<< orphan*/ stderr;
int read32(int fd, off_t addr, uint32_t *val) {
  if (lseek(fd, addr, SEEK_SET) != addr) {
    perror("persist: read32: lseek");
    return (1);
  }
  uint8_t buf[4];
  if (read(fd, buf, 4) < 4) {
    fprintf(stderr, "persist: read32: short read\n");
    return (1);
  }
  if (val)
    *val = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  return (0);
}


x86 assembly (clang, -O0):
<read32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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 <read32+0x4a>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f6 <read32+0xb6>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jge    11c5 <read32+0x85>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f6 <read32+0xb6>
cmpq   $0x0,-0x18(%rbp)
je     11ef <read32+0xaf>
mov    -0x30(%rbp),%ecx
shl    $0x18,%ecx
mov    -0x2c(%rbp),%eax
shl    $0x10,%eax
or     %eax,%ecx
mov    -0x28(%rbp),%eax
shl    $0x8,%eax
or     %eax,%ecx
or     -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read32>:
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    11c2 <read32+0x82>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x3,%eax
jg     1193 <read32+0x53>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    11b9 <read32+0x79>
xor    %eax,%eax
test   %r14,%r14
je     11b9 <read32+0x79>
mov    (%rsp),%ecx
mov    0x4(%rsp),%edx
shl    $0x18,%ecx
shl    $0x10,%edx
or     %ecx,%edx
mov    0x8(%rsp),%ecx
shl    $0x8,%ecx
or     %edx,%ecx
or     0xc(%rsp),%ecx
mov    %ecx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    11b9 <read32+0x79>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [23 x i8] c"persist: read32: lseek\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"persist: read32: short read\0A\00"
define i32 @read32(i32 %0, i64 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32*
%8 = alloca [4 x 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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %45
17: ; preds = %3
%18 = load i32, i32* %5
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%20 = call i32 @read(i32 %18, i32* %19, i32 4)
%21 = icmp slt i32 %20, 4
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %45
25: ; preds = %17
%26 = load i32*, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %44
28: ; preds = %25
%29 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 0
%30 = load i32, i32* %29
%31 = shl i32 %30, 24
%32 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 1
%33 = load i32, i32* %32
%34 = shl i32 %33, 16
%35 = or i32 %31, %34
%36 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 2
%37 = load i32, i32* %36
%38 = shl i32 %37, 8
%39 = or i32 %35, %38
%40 = getelementptr inbounds [4 x i32], [4 x i32]* %8, i64 0, i64 3
%41 = load i32, i32* %40
%42 = or i32 %39, %41
%43 = load i32*, i32** %7
store i32 %42, i32* %43
br label %44
44: ; preds = %28, %25
store i32 0, i32* %4
br label %45
45: ; preds = %44, %22, %15
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [23 x i8] c"persist: read32: lseek\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"persist: read32: short read\0A\00"
define i32 @read32(i32 %0, i64 %1, i32* writeonly %2) {
%4 = alloca [4 x 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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %36
10: ; preds = %3
%11 = bitcast [4 x i32]* %4 to i8*
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%13 = call i32 @read(i32 %0, i32* %12, i32 4)
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %34
18: ; preds = %10
%19 = icmp eq i32* %2, null
br i1 %19, label %34, label %20
20: ; preds = %18
%21 = load i32, i32* %12
%22 = shl i32 %21, 24
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = shl i32 %24, 16
%26 = or i32 %25, %22
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%28 = load i32, i32* %27
%29 = shl i32 %28, 8
%30 = or i32 %26, %29
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%32 = load i32, i32* %31
%33 = or i32 %30, %32
store i32 %33, i32* %2
br label %34
34: ; preds = %18, %20, %15
%35 = phi i32 [ 1, %15 ], [ 0, %20 ], [ 0, %18 ]
br label %36
36: ; preds = %34, %8
%37 = phi i32 [ 1, %8 ], [ %35, %34 ]
ret i32 %37
}