#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*/ uint64_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int get_buffer(void *, size_t, int);
int /*<<< orphan*/ le16toh(int /*<<< orphan*/);
int /*<<< orphan*/ le32toh(int /*<<< orphan*/);
int /*<<< orphan*/ le64toh(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int get_buffer_endian(void *buffer, size_t size, int print_error) {
  int ret;
  ret = get_buffer(buffer, size, print_error);
  if (ret < 0)
    return ret;
  switch (size) {
  case 1:
    break;
  case 2:
    *(uint16_t *)buffer = le16toh(*(uint16_t *)buffer);
    break;
  case 4:
    *(uint32_t *)buffer = le32toh(*(uint32_t *)buffer);
    break;
  case 8:
    *(uint64_t *)buffer = le64toh(*(uint64_t *)buffer);
    break;
  default:
    fprintf(stderr, "Invalid item size %u for endianness conversion\n",
            (unsigned int)size);
    return -EINVAL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_buffer_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <get_buffer@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    118b <get_buffer_endian+0x3b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <get_buffer_endian+0xe7>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
sub    $0x7,%rax
ja     1202 <get_buffer_endian+0xb2>
mov    -0x28(%rbp),%rax
lea    0xe54(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    1230 <get_buffer_endian+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <le16toh@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1230 <get_buffer_endian+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <le32toh@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1230 <get_buffer_endian+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <le64toh@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1230 <get_buffer_endian+0xe0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe08(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2db2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <get_buffer_endian+0xe7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_buffer_endian>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1070 <get_buffer@plt>
test   %eax,%eax
js     118a <get_buffer_endian+0x3a>
lea    -0x1(%r15),%rax
cmp    $0x7,%rax
ja     118e <get_buffer_endian+0x3e>
xor    %ebx,%ebx
lea    0xe89(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    (%r14),%edi
call   1060 <le16toh@plt>
jmp    11c5 <get_buffer_endian+0x75>
mov    %eax,%ebx
jmp    11c8 <get_buffer_endian+0x78>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
mov    %r15d,%edx
call   1050 <fprintf@plt>
mov    0x2e2b(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11c8 <get_buffer_endian+0x78>
mov    (%r14),%edi
call   1040 <le32toh@plt>
jmp    11c5 <get_buffer_endian+0x75>
mov    (%r14),%edi
call   1030 <le64toh@plt>
mov    %eax,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [48 x i8] c"Invalid item size %u for endianness conversion\0A\00"
@EINVAL = global i32 0
define i32 @get_buffer_endian(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = load i32, i32* %7
%12 = call i32 @get_buffer(i8* %9, i64 %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %8
store i32 %16, i32* %4
br label %49
17: ; preds = %3
%18 = load i64, i64* %6
switch i64 %18, label %41 [
i64 1, label %19
i64 2, label %20
i64 4, label %27
i64 8, label %34
]
19: ; preds = %17
br label %48
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = bitcast i8* %21 to i32*
%23 = load i32, i32* %22
%24 = call i32 @le16toh(i32 %23)
%25 = load i8*, i8** %5
%26 = bitcast i8* %25 to i32*
store i32 %24, i32* %26
br label %48
27: ; preds = %17
%28 = load i8*, i8** %5
%29 = bitcast i8* %28 to i32*
%30 = load i32, i32* %29
%31 = call i32 @le32toh(i32 %30)
%32 = load i8*, i8** %5
%33 = bitcast i8* %32 to i32*
store i32 %31, i32* %33
br label %48
34: ; preds = %17
%35 = load i8*, i8** %5
%36 = bitcast i8* %35 to i32*
%37 = load i32, i32* %36
%38 = call i32 @le64toh(i32 %37)
%39 = load i8*, i8** %5
%40 = bitcast i8* %39 to i32*
store i32 %38, i32* %40
br label %48
41: ; preds = %17
%42 = load i32, i32* @stderr
%43 = load i64, i64* %6
%44 = trunc i64 %43 to i32
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %44)
%46 = load i32, i32* @EINVAL
%47 = sub nsw i32 0, %46
store i32 %47, i32* %4
br label %49
48: ; preds = %34, %27, %20, %19
store i32 0, i32* %4
br label %49
49: ; preds = %48, %41, %15
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [48 x i8] c"Invalid item size %u for endianness conversion\0A\00"
@EINVAL = global i32 0
define i32 @get_buffer_endian(i8* %0, i64 %1, i32 %2) {
%4 = tail call i32 @get_buffer(i8* %0, i64 %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %25, label %6
6: ; preds = %3
switch i64 %1, label %19 [
i64 1, label %25
i64 2, label %7
i64 4, label %11
i64 8, label %15
]
7: ; preds = %6
%8 = bitcast i8* %0 to i32*
%9 = load i32, i32* %8
%10 = tail call i32 @le16toh(i32 %9)
store i32 %10, i32* %8
br label %25
11: ; preds = %6
%12 = bitcast i8* %0 to i32*
%13 = load i32, i32* %12
%14 = tail call i32 @le32toh(i32 %13)
store i32 %14, i32* %12
br label %25
15: ; preds = %6
%16 = bitcast i8* %0 to i32*
%17 = load i32, i32* %16
%18 = tail call i32 @le64toh(i32 %17)
store i32 %18, i32* %16
br label %25
19: ; preds = %6
%20 = load i32, i32* @stderr
%21 = trunc i64 %1 to i32
%22 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
br label %25
25: ; preds = %6, %7, %11, %15, %3, %19
%26 = phi i32 [ %24, %19 ], [ %4, %3 ], [ 0, %15 ], [ 0, %11 ], [ 0, %7 ], [ 0, %6 ]
ret i32 %26
}