#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 */
struct pevent {
  int dummy;
};

/* Variables and functions */
unsigned long long __data2host2(struct pevent *, unsigned short);
unsigned long long __data2host4(struct pevent *, unsigned int);
unsigned long long data2host8(struct pevent *, void const *);
unsigned long long pevent_read_number(struct pevent *pevent, const void *ptr,
                                      int size) {
  switch (size) {
  case 1:
    return *(unsigned char *)ptr;
  case 2:
    return __data2host2(pevent, *(unsigned short *)(ptr));
  case 4:
    return __data2host4(pevent, *(unsigned int *)(ptr));
  case 8:
    return data2host8(pevent, ptr);
  default:
    return 0;
  }
}


x86 assembly (clang, -O0):
<pevent_read_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x7,%eax
ja     11c3 <pevent_read_number+0x93>
mov    -0x28(%rbp),%rax
lea    0xe9d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
mov    %rax,-0x8(%rbp)
jmp    11cb <pevent_read_number+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movzwl (%rax),%esi
call   1040 <__data2host2@plt>
mov    %rax,-0x8(%rbp)
jmp    11cb <pevent_read_number+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1030 <__data2host4@plt>
mov    %rax,-0x8(%rbp)
jmp    11cb <pevent_read_number+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <data2host8@plt>
mov    %rax,-0x8(%rbp)
jmp    11cb <pevent_read_number+0x9b>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pevent_read_number>:
xor    %eax,%eax
add    $0xffffffff,%edx
cmp    $0x7,%edx
ja     114d <pevent_read_number+0x1d>
lea    0xebf(%rip),%rcx
movslq (%rcx,%rdx,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
movzbl (%rsi),%eax
ret
movzwl (%rsi),%esi
jmp    1040 <__data2host2@plt>
mov    (%rsi),%esi
jmp    1030 <__data2host4@plt>
jmp    1050 <data2host8@plt>

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

LLVM IR (-O2):
%struct.pevent = type { i32 }
define i64 @pevent_read_number(%struct.pevent* %0, i8* %1, i32 %2) {
switch i32 %2, label %17 [
i32 1, label %4
i32 2, label %7
i32 4, label %11
i32 8, label %15
]
4: ; preds = %3
%5 = load i8, i8* %1
%6 = zext i8 %5 to i64
br label %17
7: ; preds = %3
%8 = bitcast i8* %1 to i16*
%9 = load i16, i16* %8
%10 = tail call i64 @__data2host2(%struct.pevent* %0, i16 zeroext %9)
br label %17
11: ; preds = %3
%12 = bitcast i8* %1 to i32*
%13 = load i32, i32* %12
%14 = tail call i64 @__data2host4(%struct.pevent* %0, i32 %13)
br label %17
15: ; preds = %3
%16 = tail call i64 @data2host8(%struct.pevent* %0, i8* %1)
br label %17
17: ; preds = %3, %15, %11, %7, %4
%18 = phi i64 [ %16, %15 ], [ %14, %11 ], [ %10, %7 ], [ %6, %4 ], [ 0, %3 ]
ret i64 %18
}