#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 */

/* Variables and functions */
unsigned long long __data2host2(unsigned short);
unsigned long long __data2host4(unsigned int);
int /*<<< orphan*/ __data2host8(unsigned long long);
int /*<<< orphan*/ memcpy(unsigned long long *, void *, int);
unsigned long long read_size(void *ptr, int size) {
  switch (size) {
  case 1:
    return *(unsigned char *)ptr;
  case 2:
    return __data2host2(*(unsigned short *)ptr);
  case 4:
    return __data2host4(*(unsigned int *)ptr);
  case 8:
    return ({
      unsigned long long __val;
      memcpy(&__val, (ptr), sizeof(unsigned long long));
      __data2host8(__val);
    });
  default:
    return 0;
  }
}


x86 assembly (clang, -O0):
<read_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x7,%eax
ja     11dc <read_size+0x9c>
mov    -0x30(%rbp),%rax
lea    0xe91(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %rax,-0x8(%rbp)
jmp    11e4 <read_size+0xa4>
mov    -0x10(%rbp),%rax
movzwl (%rax),%edi
call   1060 <__data2host2@plt>
mov    %rax,-0x8(%rbp)
jmp    11e4 <read_size+0xa4>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <__data2host4@plt>
mov    %rax,-0x8(%rbp)
jmp    11e4 <read_size+0xa4>
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <__data2host8@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <read_size+0xa4>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_size>:
push   %rax
xor    %eax,%eax
add    $0xffffffff,%esi
cmp    $0x7,%esi
ja     118f <read_size+0x4f>
mov    %rdi,%rcx
lea    0xeab(%rip),%rdx
movslq (%rdx,%rsi,4),%rsi
add    %rdx,%rsi
jmp    *%rsi
movzbl (%rcx),%eax
pop    %rcx
ret
movzwl (%rcx),%edi
pop    %rax
jmp    1060 <__data2host2@plt>
mov    (%rcx),%edi
pop    %rax
jmp    1040 <__data2host4@plt>
mov    %rsp,%rdi
mov    %rcx,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
mov    (%rsp),%rdi
call   1050 <__data2host8@plt>
cltq
pop    %rcx
ret

LLVM IR (-O0):
define i64 @read_size(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
switch i32 %8, label %30 [
i32 1, label %9
i32 2, label %13
i32 4, label %18
i32 8, label %23
]
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = zext i8 %11 to i64
store i64 %12, i64* %3
br label %31
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = bitcast i8* %14 to i16*
%16 = load i16, i16* %15
%17 = call i64 @__data2host2(i16 zeroext %16)
store i64 %17, i64* %3
br label %31
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
%22 = call i64 @__data2host4(i32 %21)
store i64 %22, i64* %3
br label %31
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = call i32 @memcpy(i64* %6, i8* %24, i32 8)
%26 = load i64, i64* %6
%27 = call i32 @__data2host8(i64 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
store i64 %29, i64* %3
br label %31
30: ; preds = %2
store i64 0, i64* %3
br label %31
31: ; preds = %30, %23, %18, %13, %9
%32 = load i64, i64* %3
ret i64 %32
}

LLVM IR (-O2):
define i64 @read_size(i8* %0, i32 %1) {
%3 = alloca i64
switch i32 %1, label %21 [
i32 1, label %4
i32 2, label %7
i32 4, label %11
i32 8, label %15
]
4: ; preds = %2
%5 = load i8, i8* %0
%6 = zext i8 %5 to i64
br label %21
7: ; preds = %2
%8 = bitcast i8* %0 to i16*
%9 = load i16, i16* %8
%10 = tail call i64 @__data2host2(i16 zeroext %9)
br label %21
11: ; preds = %2
%12 = bitcast i8* %0 to i32*
%13 = load i32, i32* %12
%14 = tail call i64 @__data2host4(i32 %13)
br label %21
15: ; preds = %2
%16 = bitcast i64* %3 to i8*
%17 = call i32 @memcpy(i64* %3, i8* %0, i32 8)
%18 = load i64, i64* %3
%19 = call i32 @__data2host8(i64 %18)
%20 = sext i32 %19 to i64
br label %21
21: ; preds = %2, %15, %11, %7, %4
%22 = phi i64 [ %20, %15 ], [ %14, %11 ], [ %10, %7 ], [ %6, %4 ], [ 0, %2 ]
ret i64 %22
}