#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ putchar(unsigned int);
unsigned int read16(unsigned int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write16(unsigned int, unsigned int);
void write8(unsigned int addr, unsigned int data) {
  unsigned int content;
  switch (addr) {
  case 0x40013804:
    putchar(data & 0xFF);
    fflush(stdout);
    return;
  }
  content = read16(addr & (~1));
  if (addr & 1) {
    content &= 0x00FF;
    content |= data << 8;
  } else {
    content &= 0xFF00;
    content |= data & 0x00FF;
  }
  write16(addr & (~1), content & 0xFFFF);
}


x86 assembly (clang, -O0):
<write8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x40013804,%eax
jne    1182 <write8+0x42>
jmp    1161 <write8+0x21>
mov    -0x8(%rbp),%edi
and    $0xff,%edi
call   1030 <putchar@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    11e8 <write8+0xa8>
mov    -0x4(%rbp),%edi
and    $0xfffffffe,%edi
call   1040 <read16@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11bb <write8+0x7b>
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x8,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11d4 <write8+0x94>
mov    -0xc(%rbp),%eax
and    $0xff00,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
and    $0xfffffffe,%edi
mov    -0xc(%rbp),%esi
and    $0xffff,%esi
call   1060 <write16@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write8>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
cmp    $0x40013804,%edi
jne    1168 <write8+0x28>
movzbl %bl,%edi
call   1030 <putchar@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <fflush@plt>
mov    %edi,%ebp
mov    %edi,%r14d
and    $0xfffffffe,%r14d
mov    %r14d,%edi
call   1040 <read16@plt>
test   $0x1,%bpl
jne    1189 <write8+0x49>
and    $0xff00,%eax
movzbl %bl,%ebx
jmp    118f <write8+0x4f>
movzbl %al,%eax
shl    $0x8,%ebx
or     %eax,%ebx
movzwl %bx,%esi
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <write16@plt>

LLVM IR (-O0):
@stdout = global i32 0
define void @write8(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
switch i32 %6, label %13 [
i32 1073821700, label %7
]
7: ; preds = %2
%8 = load i32, i32* %4
%9 = and i32 %8, 255
%10 = call i32 @putchar(i32 %9)
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
br label %40
13: ; preds = %2
%14 = load i32, i32* %3
%15 = and i32 %14, -2
%16 = call i32 @read16(i32 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %3
%18 = and i32 %17, 1
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = load i32, i32* %5
%22 = and i32 %21, 255
store i32 %22, i32* %5
%23 = load i32, i32* %4
%24 = shl i32 %23, 8
%25 = load i32, i32* %5
%26 = or i32 %25, %24
store i32 %26, i32* %5
br label %34
27: ; preds = %13
%28 = load i32, i32* %5
%29 = and i32 %28, 65280
store i32 %29, i32* %5
%30 = load i32, i32* %4
%31 = and i32 %30, 255
%32 = load i32, i32* %5
%33 = or i32 %32, %31
store i32 %33, i32* %5
br label %34
34: ; preds = %27, %20
%35 = load i32, i32* %3
%36 = and i32 %35, -2
%37 = load i32, i32* %5
%38 = and i32 %37, 65535
%39 = call i32 @write16(i32 %36, i32 %38)
br label %40
40: ; preds = %34, %7
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
define void @write8(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, 1073821700
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = and i32 %1, 255
%6 = tail call i32 @putchar(i32 %5)
%7 = load i32, i32* @stdout
%8 = tail call i32 @fflush(i32 %7)
br label %26
9: ; preds = %2
%10 = and i32 %0, -2
%11 = tail call i32 @read16(i32 %10)
%12 = and i32 %0, 1
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = and i32 %11, 255
%16 = shl i32 %1, 8
%17 = or i32 %15, %16
br label %22
18: ; preds = %9
%19 = and i32 %11, 65280
%20 = and i32 %1, 255
%21 = or i32 %19, %20
br label %22
22: ; preds = %18, %14
%23 = phi i32 [ %17, %14 ], [ %21, %18 ]
%24 = and i32 %23, 65535
%25 = tail call i32 @write16(i32 %10, i32 %24)
br label %26
26: ; preds = %22, %4
ret void
}