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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int inb(int);
int /*<<< orphan*/ ioperm(int, int, int);
int /*<<< orphan*/ out_addr(int);
int /*<<< orphan*/ out_port(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait_ticks(int);
int main(void) {
  int value, i;
  unsigned char data[0x4000];
  FILE *file;
  ioperm(0x3e0, 8, 1);
  out_port(0, 0);
  out_port(0, 1);
  out_port(0, 2);
  out_port(0, 6);
  out_port(4, 3);
  wait_ticks(100000);
  for (i = 0; i < 0x4000; i++) {
    out_addr(i);
    wait_ticks(100);
    value = inb(0x3e0 + 4);
    data[i] = value;
    if ((i & 0xf) == 0)
      printf("%.4x:", i);
    printf(" %.2x", value);
    if ((i & 0xf) == 0xf)
      printf("\n");
  }
  out_port(0xf8, 2);
  out_port(0, 3);
  out_port(0, 6);
  out_port(0, 0);
  file = fopen("dil24.bin", "wb");
  fwrite(data, 1, sizeof(data), file);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x3e0,%edi
mov    $0x8,%esi
mov    $0x1,%edx
call   1060 <ioperm@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1090 <out_port@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   1090 <out_port@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   1090 <out_port@plt>
xor    %edi,%edi
mov    $0x6,%esi
call   1090 <out_port@plt>
mov    $0x4,%edi
mov    $0x3,%esi
call   1090 <out_port@plt>
mov    $0x186a0,%edi
call   1080 <wait_ticks@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x4000,-0xc(%rbp)
jge    12a0 <main+0x110>
mov    -0xc(%rbp),%edi
call   1050 <out_addr@plt>
mov    $0x64,%edi
call   1080 <wait_ticks@plt>
mov    $0x3e4,%edi
call   1070 <inb@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %al,%cl
movslq -0xc(%rbp),%rax
mov    %cl,-0x4010(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    125f <main+0xcf>
mov    -0xc(%rbp),%esi
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
and    $0xf,%eax
cmp    $0xf,%eax
jne    128d <main+0xfd>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1292 <main+0x102>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1203 <main+0x73>
mov    $0xf8,%edi
mov    $0x2,%esi
call   1090 <out_port@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   1090 <out_port@plt>
xor    %edi,%edi
mov    $0x6,%esi
call   1090 <out_port@plt>
xor    %esi,%esi
mov    %esi,%edi
call   1090 <out_port@plt>
lea    0xd37(%rip),%rdi
lea    0xd3a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x4018(%rbp)
lea    -0x4010(%rbp),%rdi
mov    -0x4018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   10b0 <fwrite@plt>
mov    -0x4018(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4000,%rsp
mov    $0x3e0,%edi
mov    $0x8,%esi
mov    $0x1,%edx
call   1070 <ioperm@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <out_port@plt>
xor    %edi,%edi
mov    $0x1,%esi
call   10a0 <out_port@plt>
xor    %edi,%edi
mov    $0x2,%esi
call   10a0 <out_port@plt>
xor    %edi,%edi
mov    $0x6,%esi
call   10a0 <out_port@plt>
mov    $0x4,%edi
mov    $0x3,%esi
call   10a0 <out_port@plt>
mov    $0x186a0,%edi
call   1090 <wait_ticks@plt>
lea    0xdf0(%rip),%r14
lea    0xdef(%rip),%r15
xor    %ebx,%ebx
jmp    1246 <main+0xa6>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    $0x4000,%rbx
je     128d <main+0xed>
mov    %ebx,%edi
call   1060 <out_addr@plt>
mov    $0x64,%edi
call   1090 <wait_ticks@plt>
mov    $0x3e4,%edi
call   1080 <inb@plt>
mov    %eax,%r12d
mov    %r12b,(%rsp,%rbx,1)
mov    %ebx,%ebp
and    $0xf,%ebp
je     1220 <main+0x80>
mov    %r15,%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xf,%ebp
jne    1239 <main+0x99>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1239 <main+0x99>
mov    $0xf8,%edi
mov    $0x2,%esi
call   10a0 <out_port@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   10a0 <out_port@plt>
xor    %edi,%edi
mov    $0x6,%esi
call   10a0 <out_port@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10a0 <out_port@plt>
lea    0xd48(%rip),%rdi
lea    0xd4b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.4x:\00"
@.str.1 = [6 x i8] c" %.2x\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [10 x i8] c"dil24.bin\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16384 x i8]
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32 @ioperm(i32 992, i32 8, i32 1)
%7 = call i32 @out_port(i32 0, i32 0)
%8 = call i32 @out_port(i32 0, i32 1)
%9 = call i32 @out_port(i32 0, i32 2)
%10 = call i32 @out_port(i32 0, i32 6)
%11 = call i32 @out_port(i32 4, i32 3)
%12 = call i32 @wait_ticks(i32 100000)
store i32 0, i32* %3
br label %13
13: ; preds = %41, %0
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 16384
br i1 %15, label %16, label %44
16: ; preds = %13
%17 = load i32, i32* %3
%18 = call i32 @out_addr(i32 %17)
%19 = call i32 @wait_ticks(i32 100)
%20 = call i32 @inb(i32 996)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = trunc i32 %21 to i8
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 %24
store i8 %22, i8* %25
%26 = load i32, i32* %3
%27 = and i32 %26, 15
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %16
%30 = load i32, i32* %3
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %29, %16
%33 = load i32, i32* %2
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %3
%36 = and i32 %35, 15
%37 = icmp eq i32 %36, 15
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %32
br label %41
41: ; preds = %40
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
store i32 %43, i32* %3
br label %13
44: ; preds = %13
%45 = call i32 @out_port(i32 248, i32 2)
%46 = call i32 @out_port(i32 0, i32 3)
%47 = call i32 @out_port(i32 0, i32 6)
%48 = call i32 @out_port(i32 0, i32 0)
%49 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %49, i32** %5
%50 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%51 = load i32*, i32** %5
%52 = call i32 @fwrite(i8* %50, i32 1, i32 16384, i32* %51)
%53 = load i32*, i32** %5
%54 = call i32 @fclose(i32* %53)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.4x:\00"
@.str.1 = [6 x i8] c" %.2x\00"
@.str.3 = [10 x i8] c"dil24.bin\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca [16384 x i8]
%2 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @ioperm(i32 992, i32 8, i32 1)
%4 = tail call i32 @out_port(i32 0, i32 0)
%5 = tail call i32 @out_port(i32 0, i32 1)
%6 = tail call i32 @out_port(i32 0, i32 2)
%7 = tail call i32 @out_port(i32 0, i32 6)
%8 = tail call i32 @out_port(i32 4, i32 3)
%9 = tail call i32 @wait_ticks(i32 100000)
br label %10
10: ; preds = %0, %28
%11 = phi i64 [ 0, %0 ], [ %29, %28 ]
%12 = trunc i64 %11 to i32
%13 = tail call i32 @out_addr(i32 %12)
%14 = tail call i32 @wait_ticks(i32 100)
%15 = tail call i32 @inb(i32 996)
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 %11
store i8 %16, i8* %17
%18 = and i32 %12, 15
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %10
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %12)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %28
23: ; preds = %10
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%25 = icmp eq i32 %18, 15
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @putchar(i32 10)
br label %28
28: ; preds = %20, %23, %26
%29 = add nuw nsw i64 %11, 1
%30 = icmp eq i64 %29, 16384
br i1 %30, label %31, label %10
31: ; preds = %28
%32 = tail call i32 @out_port(i32 248, i32 2)
%33 = tail call i32 @out_port(i32 0, i32 3)
%34 = tail call i32 @out_port(i32 0, i32 6)
%35 = tail call i32 @out_port(i32 0, i32 0)
%36 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @fwrite(i8* %2, i32 1, i32 16384, i32* %36)
%38 = tail call i32 @fclose(i32* %36)
ret i32 0
}