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


x86 assembly (clang, -O0):
<writemem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffff9e,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x15,%eax
ja     11a1 <writemem+0x81>
mov    -0x20(%rbp),%rax
lea    0xead(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
jmp    11b5 <writemem+0x95>
mov    -0x10(%rbp),%rax
mov    %ax,%cx
mov    -0x8(%rbp),%rax
mov    %cx,(%rax)
jmp    11b5 <writemem+0x95>
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    11b5 <writemem+0x95>
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    11b5 <writemem+0x95>
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
lea    0xea8(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <readmem@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writemem>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rdi,%r14
lea    -0x62(%rbx),%eax
cmp    $0x15,%eax
ja     1146 <writemem+0x26>
lea    0xec8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %sil,(%r14)
jmp    1169 <writemem+0x49>
lea    0xf0b(%rip),%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1030 <errx@plt>
jmp    1169 <writemem+0x49>
mov    %rsi,(%r14)
jmp    1169 <writemem+0x49>
mov    %si,(%r14)
jmp    1169 <writemem+0x49>
mov    %esi,(%r14)
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <readmem@plt>

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

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