#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 uint16_t;

/* Variables and functions */
int read_word(int, int);
int /*<<< orphan*/ write_byte(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ write_word(int, int, int);
unsigned int enqueue_key(uint8_t scan_code, uint8_t ascii_code) {
  uint16_t buffer_start, buffer_end, buffer_head, buffer_tail, temp_tail;
  buffer_start = 0x001E;
  buffer_end = 0x003E;
  buffer_head = read_word(0x0040, 0x001A);
  buffer_tail = read_word(0x0040, 0x001C);
  temp_tail = buffer_tail;
  buffer_tail += 2;
  if (buffer_tail >= buffer_end)
    buffer_tail = buffer_start;
  if (buffer_tail == buffer_head)
    return (0);
  write_byte(0x0040, temp_tail, ascii_code);
  write_byte(0x0040, temp_tail + 1, scan_code);
  write_word(0x0040, 0x001C, buffer_tail);
  return (1);
}


x86 assembly (clang, -O0):
<enqueue_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1e,-0x10(%rbp)
movl   $0x3e,-0x14(%rbp)
mov    $0x40,%edi
mov    $0x1a,%esi
call   1030 <read_word@plt>
mov    %eax,-0x18(%rbp)
mov    $0x40,%edi
mov    $0x1c,%esi
call   1030 <read_word@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1191 <enqueue_key+0x61>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    11a9 <enqueue_key+0x79>
movl   $0x0,-0x4(%rbp)
jmp    11e5 <enqueue_key+0xb5>
mov    -0x20(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    $0x40,%edi
call   1050 <write_byte@plt>
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x8(%rbp),%edx
mov    $0x40,%edi
call   1050 <write_byte@plt>
mov    -0x1c(%rbp),%edx
mov    $0x40,%edi
mov    $0x1c,%esi
call   1040 <write_word@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enqueue_key>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %edi,%r14d
mov    $0x40,%edi
mov    $0x1a,%esi
call   1030 <read_word@plt>
mov    %eax,%r12d
mov    $0x40,%edi
mov    $0x1c,%esi
call   1030 <read_word@plt>
mov    %eax,%ebp
lea    0x2(%rbp),%eax
cmp    $0x3c,%ebp
mov    $0x1e,%ebx
cmovl  %eax,%ebx
xor    %eax,%eax
cmp    %r12d,%ebx
je     11ad <enqueue_key+0x7d>
mov    $0x40,%edi
mov    %ebp,%esi
mov    %r15d,%edx
call   1050 <write_byte@plt>
add    $0x1,%ebp
mov    $0x40,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1050 <write_byte@plt>
mov    $0x40,%edi
mov    $0x1c,%esi
mov    %ebx,%edx
call   1040 <write_word@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @enqueue_key(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 30, i32* %6
store i32 62, i32* %7
%11 = call i32 @read_word(i32 64, i32 26)
store i32 %11, i32* %8
%12 = call i32 @read_word(i32 64, i32 28)
store i32 %12, i32* %9
%13 = load i32, i32* %9
store i32 %13, i32* %10
%14 = load i32, i32* %9
%15 = add nsw i32 %14, 2
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = load i32, i32* %7
%18 = icmp sge i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = load i32, i32* %6
store i32 %20, i32* %9
br label %21
21: ; preds = %19, %2
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %3
br label %36
26: ; preds = %21
%27 = load i32, i32* %10
%28 = load i32, i32* %5
%29 = call i32 @write_byte(i32 64, i32 %27, i32 %28)
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
%32 = load i32, i32* %4
%33 = call i32 @write_byte(i32 64, i32 %31, i32 %32)
%34 = load i32, i32* %9
%35 = call i32 @write_word(i32 64, i32 28, i32 %34)
store i32 1, i32* %3
br label %36
36: ; preds = %26, %25
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
define i32 @enqueue_key(i32 %0, i32 %1) {
%3 = tail call i32 @read_word(i32 64, i32 26)
%4 = tail call i32 @read_word(i32 64, i32 28)
%5 = add nsw i32 %4, 2
%6 = icmp sgt i32 %4, 59
%7 = select i1 %6, i32 30, i32 %5
%8 = icmp eq i32 %7, %3
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = tail call i32 @write_byte(i32 64, i32 %4, i32 %1)
%11 = add nsw i32 %4, 1
%12 = tail call i32 @write_byte(i32 64, i32 %11, i32 %0)
%13 = tail call i32 @write_word(i32 64, i32 28, i32 %7)
br label %14
14: ; preds = %2, %9
%15 = phi i32 [ 1, %9 ], [ 0, %2 ]
ret i32 %15
}