#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 bus_readbyte(unsigned int);
int /*<<< orphan*/ bus_writebyte(unsigned int, unsigned int);
char getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, unsigned int *);
void write_register() {
  printf("Select destination...\n");
  printf("1. LPT0_DATA\n");
  printf("2. LPT0_CTRL\n");
  printf("3. LPT1_DATA\n");
  printf("4. LPT1_CTRL\n");
  char c = getchar();
  while (c == '\n')
    c = getchar();
  unsigned reg = 0;
  switch (c) {
  case '1':
    reg = 0x78;
    break;
  case '2':
    reg = 0x7A;
    break;
  case '3':
    reg = 0xBC;
    break;
  case '4':
    reg = 0xBE;
    break;
  default:
    printf("Error\n");
    return;
  }
  printf("Current value: 0x%X, enter new value (hex) 0x: ", bus_readbyte(reg));
  unsigned v = 0;
  scanf("%x", &v);
  bus_writebyte(reg, v);
}


x86 assembly (clang, -O0):
<write_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
cmp    $0xa,%eax
jne    11c4 <write_register+0x74>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x1(%rbp)
jmp    11a8 <write_register+0x58>
movl   $0x0,-0x8(%rbp)
movsbl -0x1(%rbp),%eax
add    $0xffffffcf,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x3,%eax
ja     1225 <write_register+0xd5>
mov    -0x18(%rbp),%rax
lea    0xe14(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x78,-0x8(%rbp)
jmp    1238 <write_register+0xe8>
movl   $0x7a,-0x8(%rbp)
jmp    1238 <write_register+0xe8>
movl   $0xbc,-0x8(%rbp)
jmp    1238 <write_register+0xe8>
movl   $0xbe,-0x8(%rbp)
jmp    1238 <write_register+0xe8>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1272 <write_register+0x122>
mov    -0x8(%rbp),%edi
call   1060 <bus_readbyte@plt>
mov    %eax,%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
lea    0xe38(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <bus_writebyte@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_register>:
push   %rbx
sub    $0x10,%rsp
lea    0xec7(%rip),%rdi
call   1040 <puts@plt>
lea    0xed1(%rip),%rdi
call   1040 <puts@plt>
lea    0xed2(%rip),%rdi
call   1040 <puts@plt>
lea    0xed3(%rip),%rdi
call   1040 <puts@plt>
lea    0xed4(%rip),%rdi
call   1040 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0xa,%al
je     11b0 <write_register+0x50>
movsbl %al,%eax
add    $0xffffffcf,%eax
cmp    $0x4,%eax
jae    1213 <write_register+0xb3>
cltq
lea    0xeb5(%rip),%rcx
mov    (%rcx,%rax,4),%ebx
mov    %ebx,%edi
call   1070 <bus_readbyte@plt>
lea    0xe20(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x0,0xc(%rsp)
lea    0xe38(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <scanf@plt>
mov    0xc(%rsp),%esi
mov    %ebx,%edi
call   1030 <bus_writebyte@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe63(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1040 <puts@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Select destination...\0A\00"
@.str.1 = [14 x i8] c"1. LPT0_DATA\0A\00"
@.str.2 = [14 x i8] c"2. LPT0_CTRL\0A\00"
@.str.3 = [14 x i8] c"3. LPT1_DATA\0A\00"
@.str.4 = [14 x i8] c"4. LPT1_CTRL\0A\00"
@.str.5 = [7 x i8] c"Error\0A\00"
@.str.6 = [48 x i8] c"Current value: 0x%X, enter new value (hex) 0x: \00"
@.str.7 = [3 x i8] c"%x\00"
define void @write_register() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%9 = call signext i8 (...) @getchar()
store i8 %9, i8* %1
br label %10
10: ; preds = %14, %0
%11 = load i8, i8* %1
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 10
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call signext i8 (...) @getchar()
store i8 %15, i8* %1
br label %10
16: ; preds = %10
store i32 0, i32* %2
%17 = load i8, i8* %1
%18 = sext i8 %17 to i32
switch i32 %18, label %23 [
i32 49, label %19
i32 50, label %20
i32 51, label %21
i32 52, label %22
]
19: ; preds = %16
store i32 120, i32* %2
br label %25
20: ; preds = %16
store i32 122, i32* %2
br label %25
21: ; preds = %16
store i32 188, i32* %2
br label %25
22: ; preds = %16
store i32 190, i32* %2
br label %25
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %33
25: ; preds = %22, %21, %20, %19
%26 = load i32, i32* %2
%27 = call i32 @bus_readbyte(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0), i32 %27)
store i32 0, i32* %3
%29 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %3)
%30 = load i32, i32* %2
%31 = load i32, i32* %3
%32 = call i32 @bus_writebyte(i32 %30, i32 %31)
br label %33
33: ; preds = %25, %23
ret void
}

LLVM IR (-O2):
@.str.6 = [48 x i8] c"Current value: 0x%X, enter new value (hex) 0x: \00"
@.str.7 = [3 x i8] c"%x\00"
@str = [22 x i8] c"Select destination...\00"
@str.8 = [13 x i8] c"1. LPT0_DATA\00"
@str.9 = [13 x i8] c"2. LPT0_CTRL\00"
@str.10 = [13 x i8] c"3. LPT1_DATA\00"
@str.11 = [13 x i8] c"4. LPT1_CTRL\00"
@str.12 = [6 x i8] c"Error\00"
@switch.table.write_register = [4 x i32] [i32 120, i32 122, i32 188, i32 190]
define void @write_register() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.11, i64 0, i64 0))
%7 = tail call signext i8 (...) @getchar()
%8 = icmp eq i8 %7, 10
br i1 %8, label %9, label %12
9: ; preds = %0, %9
%10 = tail call signext i8 (...) @getchar()
%11 = icmp eq i8 %10, 10
br i1 %11, label %9, label %12
12: ; preds = %9, %0
%13 = phi i8 [ %7, %0 ], [ %10, %9 ]
%14 = sext i8 %13 to i32
%15 = add nsw i32 %14, -49
%16 = icmp ult i32 %15, 4
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.12, i64 0, i64 0))
br label %29
19: ; preds = %12
%20 = sext i32 %15 to i64
%21 = getelementptr inbounds [4 x i32], [4 x i32]* @switch.table.write_register, i64 0, i64 %20
%22 = load i32, i32* %21
%23 = tail call i32 @bus_readbyte(i32 %22)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %1)
%27 = load i32, i32* %1
%28 = call i32 @bus_writebyte(i32 %22, i32 %27)
br label %29
29: ; preds = %19, %17
ret void
}