#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*/ io_delay();
int rdfs32(int);
int rdgs32(int);
int /*<<< orphan*/ set_fs(int);
int /*<<< orphan*/ set_gs(int);
int /*<<< orphan*/ wrfs32(int, int);
int a20_test(int loops) {
  int ok = 0;
  int saved, ctr;
  set_fs(0x0000);
  set_gs(0xffff);
  saved = ctr = rdfs32((4 * 0x80));
  while (loops--) {
    wrfs32(++ctr, (4 * 0x80));
    io_delay();
    ok = rdgs32((4 * 0x80) + 0x10) ^ ctr;
    if (ok)
      break;
  }
  wrfs32(saved, (4 * 0x80));
  return ok;
}


x86 assembly (clang, -O0):
<a20_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
xor    %edi,%edi
call   1070 <set_fs@plt>
mov    $0xffff,%edi
call   1050 <set_gs@plt>
mov    $0x200,%edi
call   1080 <rdfs32@plt>
mov    %eax,-0x10(%rbp)
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     11e5 <a20_test+0x85>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
mov    %edi,-0x10(%rbp)
mov    $0x200,%esi
call   1060 <wrfs32@plt>
mov    $0x0,%al
call   1040 <io_delay@plt>
mov    $0x210,%edi
call   1030 <rdgs32@plt>
xor    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11e0 <a20_test+0x80>
jmp    11e5 <a20_test+0x85>
jmp    1193 <a20_test+0x33>
mov    -0xc(%rbp),%edi
mov    $0x200,%esi
call   1060 <wrfs32@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<a20_test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
xor    %edi,%edi
call   1070 <set_fs@plt>
mov    $0xffff,%edi
call   1050 <set_gs@plt>
mov    $0x200,%edi
call   1080 <rdfs32@plt>
mov    %eax,%r15d
neg    %r14d
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%eax
cmp    $0x1,%eax
je     11d7 <a20_test+0x77>
lea    (%r15,%rbx,1),%r12d
mov    %r12d,%edi
mov    $0x200,%esi
call   1060 <wrfs32@plt>
xor    %eax,%eax
call   1040 <io_delay@plt>
mov    $0x210,%edi
call   1030 <rdgs32@plt>
mov    %eax,%ebp
add    $0x1,%ebx
xor    %r12d,%ebp
je     11a0 <a20_test+0x40>
jmp    11d9 <a20_test+0x79>
xor    %ebp,%ebp
mov    %r15d,%edi
mov    $0x200,%esi
call   1060 <wrfs32@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @a20_test(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%6 = call i32 @set_fs(i32 0)
%7 = call i32 @set_gs(i32 65535)
%8 = call i32 @rdfs32(i32 512)
store i32 %8, i32* %5
store i32 %8, i32* %4
br label %9
9: ; preds = %24, %1
%10 = load i32, i32* %2
%11 = add nsw i32 %10, -1
store i32 %11, i32* %2
%12 = icmp ne i32 %10, 0
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
%16 = call i32 @wrfs32(i32 %15, i32 512)
%17 = call i32 (...) @io_delay()
%18 = call i32 @rdgs32(i32 528)
%19 = load i32, i32* %5
%20 = xor i32 %18, %19
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %13
br label %25
24: ; preds = %13
br label %9
25: ; preds = %23, %9
%26 = load i32, i32* %4
%27 = call i32 @wrfs32(i32 %26, i32 512)
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
define i32 @a20_test(i32 %0) {
%2 = tail call i32 @set_fs(i32 0)
%3 = tail call i32 @set_gs(i32 65535)
%4 = tail call i32 @rdfs32(i32 512)
br label %5
5: ; preds = %9, %1
%6 = phi i32 [ %0, %1 ], [ %10, %9 ]
%7 = phi i32 [ %4, %1 ], [ %11, %9 ]
%8 = icmp eq i32 %6, 0
br i1 %8, label %17, label %9
9: ; preds = %5
%10 = add nsw i32 %6, -1
%11 = add nsw i32 %7, 1
%12 = tail call i32 @wrfs32(i32 %11, i32 512)
%13 = tail call i32 (...) @io_delay()
%14 = tail call i32 @rdgs32(i32 528)
%15 = xor i32 %14, %11
%16 = icmp eq i32 %15, 0
br i1 %16, label %5, label %17
17: ; preds = %9, %5
%18 = phi i32 [ %15, %9 ], [ 0, %5 ]
%19 = tail call i32 @wrfs32(i32 %4, i32 512)
ret i32 %18
}