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

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sim_failure(char *);
int /*<<< orphan*/ sim_stop();
int /*<<< orphan*/ sim_warning(char *);
void io_out(uint16_t port, uint16_t val) {
  static FILE *hexfile;
  switch (port) {
  case 0x11:
    if (!hexfile) {
      hexfile = fopen("results.hex", "w");
      if (!hexfile) {
        sim_failure("Could not open results.hex");
      }
    }
    fprintf(hexfile, "%04x\n", val);
    break;
  case 0x13:
    printf("Stopping simulation due to write to port 0x13\n");
    sim_stop();
    break;
  default:
    sim_warning("Writing to unknown I/O port");
    break;
  }
}


x86 assembly (clang, -O0):
<io_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
sub    $0x11,%eax
je     1193 <io_out+0x33>
jmp    1182 <io_out+0x22>
mov    -0xc(%rbp),%eax
sub    $0x13,%eax
je     11f5 <io_out+0x95>
jmp    120d <io_out+0xad>
cmpq   $0x0,0x2ebd(%rip)
jne    11da <io_out+0x7a>
lea    0xe69(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,0x2e9d(%rip)
cmpq   $0x0,0x2e95(%rip)
jne    11d5 <io_out+0x75>
lea    0xe32(%rip),%rdi
call   1040 <sim_failure@plt>
jmp    11da <io_out+0x7a>
mov    0x2e77(%rip),%rdi
mov    -0x8(%rbp),%edx
lea    0xe32(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1219 <io_out+0xb9>
lea    0xe27(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <sim_stop@plt>
jmp    1219 <io_out+0xb9>
lea    0xe3e(%rip),%rdi
call   1070 <sim_warning@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_out>:
push   %rbx
cmp    $0x13,%edi
je     11bd <io_out+0x5d>
cmp    $0x11,%edi
jne    11d1 <io_out+0x71>
mov    %esi,%ebx
mov    0x2ee4(%rip),%rdi
test   %rdi,%rdi
jne    11ae <io_out+0x4e>
lea    0xe91(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rdi
mov    %rax,0x2ec2(%rip)
test   %rax,%rax
jne    11ae <io_out+0x4e>
lea    0xe60(%rip),%rdi
call   1040 <sim_failure@plt>
mov    0x2eaa(%rip),%rdi
lea    0xe68(%rip),%rsi
mov    %ebx,%edx
pop    %rbx
jmp    1050 <fprintf@plt>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
jmp    1080 <sim_stop@plt>
lea    0xe4b(%rip),%rdi
pop    %rbx
jmp    1070 <sim_warning@plt>

LLVM IR (-O0):
@io_out.hexfile = internal global i32* null
@.str = [12 x i8] c"results.hex\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [27 x i8] c"Could not open results.hex\00"
@.str.3 = [6 x i8] c"%04x\0A\00"
@.str.4 = [47 x i8] c"Stopping simulation due to write to port 0x13\0A\00"
@.str.5 = [28 x i8] c"Writing to unknown I/O port\00"
define void @io_out(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%5 = load i32, i32* %3
switch i32 %5, label %23 [
i32 17, label %6
i32 19, label %20
]
6: ; preds = %2
%7 = load i32*, i32** @io_out.hexfile
%8 = icmp ne i32* %7, null
br i1 %8, label %16, label %9
9: ; preds = %6
%10 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** @io_out.hexfile
%11 = load i32*, i32** @io_out.hexfile
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @sim_failure(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
br label %16
16: ; preds = %15, %6
%17 = load i32*, i32** @io_out.hexfile
%18 = load i32, i32* %4
%19 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %25
20: ; preds = %2
%21 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 (...) @sim_stop()
br label %25
23: ; preds = %2
%24 = call i32 @sim_warning(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %23, %20, %16
ret void
}

LLVM IR (-O2):
@io_out.hexfile = internal unnamed_addr global i32* null
@.str = [12 x i8] c"results.hex\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [27 x i8] c"Could not open results.hex\00"
@.str.3 = [6 x i8] c"%04x\0A\00"
@.str.5 = [28 x i8] c"Writing to unknown I/O port\00"
@str = [46 x i8] c"Stopping simulation due to write to port 0x13\00"
define void @io_out(i32 %0, i32 %1) {
switch i32 %0, label %18 [
i32 17, label %3
i32 19, label %15
]
3: ; preds = %2
%4 = load i32*, i32** @io_out.hexfile
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %3
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** @io_out.hexfile
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @sim_failure(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%11 = load i32*, i32** @io_out.hexfile
br label %12
12: ; preds = %6, %9, %3
%13 = phi i32* [ %7, %6 ], [ %11, %9 ], [ %4, %3 ]
%14 = tail call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %1)
br label %20
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 (...) @sim_stop()
br label %20
18: ; preds = %2
%19 = tail call i32 @sim_warning(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %20
20: ; preds = %18, %15, %12
ret void
}