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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
void *devmem_update(int /*<<< orphan*/);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, int);
uint32_t devmem_read(off_t address, int width) {
  void *virt_addr = devmem_update(address);
  switch (width) {
  case 8:
    return *((uint8_t *)virt_addr);
  case 16:
    return *((uint16_t *)virt_addr);
  case 32:
    return *((uint32_t *)virt_addr);
  default:
    errx(EXIT_FAILURE, "Bad read width %d", width);
  }
}


x86 assembly (clang, -O0):
<devmem_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <devmem_update@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x8,%eax
je     1170 <devmem_read+0x50>
jmp    114e <devmem_read+0x2e>
mov    -0x1c(%rbp),%eax
sub    $0x10,%eax
je     117e <devmem_read+0x5e>
jmp    115f <devmem_read+0x3f>
mov    -0x1c(%rbp),%eax
sub    $0x20,%eax
je     118c <devmem_read+0x6c>
jmp    119a <devmem_read+0x7a>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <devmem_read+0x92>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <devmem_read+0x92>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <devmem_read+0x92>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe53(%rip),%rsi
call   1030 <errx@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devmem_read>:
push   %rbx
mov    %esi,%ebx
call   1040 <devmem_update@plt>
cmp    $0x20,%ebx
je     1137 <devmem_read+0x17>
cmp    $0x10,%ebx
je     1137 <devmem_read+0x17>
cmp    $0x8,%ebx
jne    113b <devmem_read+0x1b>
mov    (%rax),%eax
pop    %rbx
ret
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
lea    0xeb5(%rip),%rsi
mov    %ebx,%edx
call   1030 <errx@plt>
pop    %rbx
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [18 x i8] c"Bad read width %d\00"
define i32 @devmem_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i8* @devmem_update(i32 %7)
store i8* %8, i8** %6
%9 = load i32, i32* %5
switch i32 %9, label %22 [
i32 8, label %10
i32 16, label %14
i32 32, label %18
]
10: ; preds = %2
%11 = load i8*, i8** %6
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
store i32 %13, i32* %3
br label %26
14: ; preds = %2
%15 = load i8*, i8** %6
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
store i32 %17, i32* %3
br label %26
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = bitcast i8* %19 to i32*
%21 = load i32, i32* %20
store i32 %21, i32* %3
br label %26
22: ; preds = %2
%23 = load i32, i32* @EXIT_FAILURE
%24 = load i32, i32* %5
%25 = call i32 @errx(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %10, %14, %18, %22
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [18 x i8] c"Bad read width %d\00"
define i32 @devmem_read(i32 %0, i32 %1) {
%3 = tail call i8* @devmem_update(i32 %0)
switch i32 %1, label %13 [
i32 8, label %4
i32 16, label %7
i32 32, label %10
]
4: ; preds = %2
%5 = bitcast i8* %3 to i32*
%6 = load i32, i32* %5
br label %16
7: ; preds = %2
%8 = bitcast i8* %3 to i32*
%9 = load i32, i32* %8
br label %16
10: ; preds = %2
%11 = bitcast i8* %3 to i32*
%12 = load i32, i32* %11
br label %16
13: ; preds = %2
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @errx(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %16
16: ; preds = %13, %10, %7, %4
%17 = phi i32 [ undef, %13 ], [ %12, %10 ], [ %9, %7 ], [ %6, %4 ]
ret i32 %17
}