#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);
void devmem_write(off_t address, int width, uint32_t value) {
  void *virt_addr = devmem_update(address);
  switch (width) {
  case 8:
    *((uint8_t *)virt_addr) = (uint8_t)value;
    break;
  case 16:
    *((uint16_t *)virt_addr) = (uint16_t)value;
    break;
  case 32:
    *((uint32_t *)virt_addr) = value;
    break;
  default:
    errx(EXIT_FAILURE, "Bad write width %d", width);
  }
}


x86 assembly (clang, -O0):
<devmem_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <devmem_update@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x8,%eax
je     1173 <devmem_write+0x53>
jmp    1151 <devmem_write+0x31>
mov    -0x1c(%rbp),%eax
sub    $0x10,%eax
je     1181 <devmem_write+0x61>
jmp    1162 <devmem_write+0x42>
mov    -0x1c(%rbp),%eax
sub    $0x20,%eax
je     118f <devmem_write+0x6f>
jmp    119d <devmem_write+0x7d>
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11b5 <devmem_write+0x95>
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11b5 <devmem_write+0x95>
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11b5 <devmem_write+0x95>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1030 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<devmem_write>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%ebx
call   1040 <devmem_update@plt>
cmp    $0x20,%ebx
je     113b <devmem_write+0x1b>
cmp    $0x10,%ebx
je     113b <devmem_write+0x1b>
cmp    $0x8,%ebx
jne    1144 <devmem_write+0x24>
mov    %ebp,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
lea    0xeac(%rip),%rsi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <errx@plt>

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

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