#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 */
int /*<<< orphan*/ errx(int, char *, int);
unsigned long readmem(void *addr, int access) {
  switch (access) {
  case 'b':
    return *((unsigned char *)addr);
  case 'h':
    return *((unsigned short *)addr);
  case 'w':
    return *((unsigned int *)addr);
  case 'd':
    return *((unsigned long *)addr);
  }
  errx(1, "unknown data type '%c'\n", access);
}


x86 assembly (clang, -O0):
<readmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffff9e,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x15,%eax
ja     1187 <readmem+0x77>
mov    -0x20(%rbp),%rax
lea    0xec1(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %rax,-0x8(%rbp)
jmp    119b <readmem+0x8b>
mov    -0x10(%rbp),%rax
movzwl (%rax),%eax
mov    %rax,-0x8(%rbp)
jmp    119b <readmem+0x8b>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %rax,-0x8(%rbp)
jmp    119b <readmem+0x8b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    119b <readmem+0x8b>
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
lea    0xec2(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readmem>:
push   %rax
mov    %esi,%edx
lea    -0x62(%rdx),%eax
cmp    $0x15,%eax
ja     1130 <readmem+0x20>
lea    0xede(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movzbl (%rdi),%eax
pop    %rcx
ret
lea    0xf21(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
pop    %rcx
ret
mov    (%rdi),%rax
pop    %rcx
ret
movzwl (%rdi),%eax
pop    %rcx
ret
mov    (%rdi),%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"unknown data type '%c'\0A\00"
define i64 @readmem(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
switch i32 %6, label %25 [
i32 98, label %7
i32 104, label %11
i32 119, label %16
i32 100, label %21
]
7: ; preds = %2
%8 = load i8*, i8** %4
%9 = load i8, i8* %8
%10 = zext i8 %9 to i64
store i64 %10, i64* %3
br label %28
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = bitcast i8* %12 to i16*
%14 = load i16, i16* %13
%15 = zext i16 %14 to i64
store i64 %15, i64* %3
br label %28
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = bitcast i8* %17 to i32*
%19 = load i32, i32* %18
%20 = zext i32 %19 to i64
store i64 %20, i64* %3
br label %28
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = bitcast i8* %22 to i64*
%24 = load i64, i64* %23
store i64 %24, i64* %3
br label %28
25: ; preds = %2
%26 = load i32, i32* %5
%27 = call i32 @errx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %21, %16, %11, %7
%29 = load i64, i64* %3
ret i64 %29
}

LLVM IR (-O2):
@.str = [24 x i8] c"unknown data type '%c'\0A\00"
define i64 @readmem(i8* nocapture %0, i32 %1) {
switch i32 %1, label %17 [
i32 98, label %3
i32 104, label %6
i32 119, label %10
i32 100, label %14
]
3: ; preds = %2
%4 = load i8, i8* %0
%5 = zext i8 %4 to i64
br label %19
6: ; preds = %2
%7 = bitcast i8* %0 to i16*
%8 = load i16, i16* %7
%9 = zext i16 %8 to i64
br label %19
10: ; preds = %2
%11 = bitcast i8* %0 to i32*
%12 = load i32, i32* %11
%13 = zext i32 %12 to i64
br label %19
14: ; preds = %2
%15 = bitcast i8* %0 to i64*
%16 = load i64, i64* %15
br label %19
17: ; preds = %2
%18 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %19
19: ; preds = %17, %14, %10, %6, %3
%20 = phi i64 [ undef, %17 ], [ %16, %14 ], [ %13, %10 ], [ %9, %6 ], [ %5, %3 ]
ret i64 %20
}