#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 uint64_t;
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *);
int read(int, int *, int);
uint64_t read_endian(int fd, size_t count, int endian) {
  ssize_t i;
  uint8_t data;
  uint64_t value = 0;
  if (count > 8)
    errx(1, "Max width exceeded in read_endian()");
  for (i = 0; i < count; i++) {
    if (read(fd, &data, 1) == -1)
      errx(1, "read() in read_endian() failed");
    if (endian)
      value += data << 8 * (count - i - 1);
    else
      value += data << 8 * i;
  }
  return value;
}


x86 assembly (clang, -O0):
<read_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x8,-0x10(%rbp)
jbe    1155 <read_endian+0x35>
mov    $0x1,%edi
lea    0xeb0(%rip),%rsi
call   1030 <errx@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11e9 <read_endian+0xc9>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1196 <read_endian+0x76>
mov    $0x1,%edi
lea    0xe93(%rip),%rsi
call   1030 <errx@plt>
cmpl   $0x0,-0x14(%rbp)
je     11c0 <read_endian+0xa0>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
sub    -0x20(%rbp),%rcx
sub    $0x1,%rcx
shl    $0x3,%rcx
shl    %cl,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11d3 <read_endian+0xb3>
mov    -0x24(%rbp),%eax
mov    -0x20(%rbp),%rcx
shl    $0x3,%rcx
shl    %cl,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11d8 <read_endian+0xb8>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    115d <read_endian+0x3d>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_endian>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
cmp    $0x9,%rsi
jb     114d <read_endian+0x2d>
lea    0xebf(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
jmp    1152 <read_endian+0x32>
test   %rbx,%rbx
je     11be <read_endian+0x9e>
mov    %rbx,%r13
neg    %r13
add    $0xffffffffffffffff,%rbx
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    1194 <read_endian+0x74>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %r14d,%r14d
mov    0x4(%rsp),%eax
mov    %ebx,%ecx
cmove  %r12d,%ecx
shl    $0x3,%ecx
shl    %cl,%eax
add    %eax,%ebp
add    $0x1,%r12
add    $0xffffffffffffffff,%rbx
mov    %r13,%rax
add    %r12,%rax
je     11c0 <read_endian+0xa0>
mov    %r15d,%edi
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1170 <read_endian+0x50>
mov    $0x1,%edi
lea    0xe6d(%rip),%rsi
call   1030 <errx@plt>
jmp    1170 <read_endian+0x50>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Max width exceeded in read_endian()\00"
@.str.1 = [31 x i8] c"read() in read_endian() failed\00"
define i32 @read_endian(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i32 0, i32* %9
%10 = load i64, i64* %5
%11 = icmp ugt i64 %10, 8
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
store i64 0, i64* %7
br label %15
15: ; preds = %48, %14
%16 = load i64, i64* %7
%17 = load i64, i64* %5
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %51
19: ; preds = %15
%20 = load i32, i32* %4
%21 = call i32 @read(i32 %20, i32* %8, i32 1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %25
%29 = load i32, i32* %8
%30 = load i64, i64* %5
%31 = load i64, i64* %7
%32 = sub i64 %30, %31
%33 = sub i64 %32, 1
%34 = mul i64 8, %33
%35 = trunc i64 %34 to i32
%36 = shl i32 %29, %35
%37 = load i32, i32* %9
%38 = add nsw i32 %37, %36
store i32 %38, i32* %9
br label %47
39: ; preds = %25
%40 = load i32, i32* %8
%41 = load i64, i64* %7
%42 = mul i64 8, %41
%43 = trunc i64 %42 to i32
%44 = shl i32 %40, %43
%45 = load i32, i32* %9
%46 = add nsw i32 %45, %44
store i32 %46, i32* %9
br label %47
47: ; preds = %39, %28
br label %48
48: ; preds = %47
%49 = load i64, i64* %7
%50 = add i64 %49, 1
store i64 %50, i64* %7
br label %15
51: ; preds = %15
%52 = load i32, i32* %9
ret i32 %52
}

LLVM IR (-O2):
@.str = [36 x i8] c"Max width exceeded in read_endian()\00"
@.str.1 = [31 x i8] c"read() in read_endian() failed\00"
define i32 @read_endian(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp ugt i64 %1, 8
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %11
9: ; preds = %3
%10 = icmp eq i64 %1, 0
br i1 %10, label %31, label %11
11: ; preds = %7, %9
%12 = icmp eq i32 %2, 0
br label %13
13: ; preds = %11, %20
%14 = phi i32 [ 0, %11 ], [ %28, %20 ]
%15 = phi i64 [ 0, %11 ], [ %29, %20 ]
%16 = call i32 @read(i32 %0, i32* %4, i32 1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = load i32, i32* %4
%22 = xor i64 %15, -1
%23 = add i64 %22, %1
%24 = select i1 %12, i64 %15, i64 %23
%25 = trunc i64 %24 to i32
%26 = shl i32 %25, 3
%27 = shl i32 %21, %26
%28 = add nsw i32 %27, %14
%29 = add nuw i64 %15, 1
%30 = icmp eq i64 %29, %1
br i1 %30, label %31, label %13
31: ; preds = %20, %9
%32 = phi i32 [ 0, %9 ], [ %28, %20 ]
ret i32 %32
}