#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, int);
int /*<<< orphan*/ wait_ticks(int);
int main(void) {
  int value, i;
  unsigned char data[0x1000];
  FILE *file;
  ioperm(0x3e0, 8, 1);
  out_port(0, 0);
  out_port(8, 1);
  out_port(0, 2);
  out_port(0, 6);
  out_port(4, 3);
  wait_ticks(100000);
  for (i = 0; i < 0x1000; i++) {
    out_addr(i);
    wait_ticks(100);
    value = inb(0x3e0 + 4);
    data[i] = value;
    printf("%.4x %.2x\n", i, value);
  }
  out_port(0xf8, 2);
  out_port(0, 3);
  out_port(0, 6);
  out_port(0, 0);
  file = fopen("6332.bin", "wb");
  fwrite(data, 1, sizeof(data), file);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%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>
mov    $0x8,%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   $0x1000,-0xc(%rbp)
jge    1262 <main+0xd2>
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,-0x1010(%rbp,%rax,1)
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xdb1(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1206 <main+0x76>
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    0xd72(%rip),%rdi
lea    0xd74(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x1018(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   10b0 <fwrite@plt>
mov    -0x1018(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    $0x3e0,%edi
mov    $0x8,%esi
mov    $0x1,%edx
call   1060 <ioperm@plt>
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   1090 <out_port@plt>
mov    $0x8,%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>
lea    0xe00(%rip),%r14
mov    %ebx,%edi
call   1050 <out_addr@plt>
mov    $0x64,%edi
call   1080 <wait_ticks@plt>
mov    $0x3e4,%edi
call   1070 <inb@plt>
mov    %al,(%rsp,%rbx,1)
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x1000,%rbx
jne    1200 <main+0x70>
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    %edi,%edi
xor    %esi,%esi
call   1090 <out_port@plt>
lea    0xd9d(%rip),%rdi
lea    0xd9f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%.4x %.2x\0A\00"
@.str.1 = [9 x i8] c"6332.bin\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4096 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 8, 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 = %29, %0
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 4096
br i1 %15, label %16, label %32
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 [4096 x i8], [4096 x i8]* %4, i64 0, i64 %24
store i8 %22, i8* %25
%26 = load i32, i32* %3
%27 = load i32, i32* %2
%28 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %27)
br label %29
29: ; preds = %16
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
br label %13
32: ; preds = %13
%33 = call i32 @out_port(i32 248, i32 2)
%34 = call i32 @out_port(i32 0, i32 3)
%35 = call i32 @out_port(i32 0, i32 6)
%36 = call i32 @out_port(i32 0, i32 0)
%37 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %5
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%39 = load i32*, i32** %5
%40 = call i32 @fwrite(i8* %38, i32 1, i32 4096, i32* %39)
%41 = load i32*, i32** %5
%42 = call i32 @fclose(i32* %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"%.4x %.2x\0A\00"
@.str.1 = [9 x i8] c"6332.bin\00"
@.str.2 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 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 8, 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, %10
%11 = phi i64 [ 0, %0 ], [ %19, %10 ]
%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 [4096 x i8], [4096 x i8]* %1, i64 0, i64 %11
store i8 %16, i8* %17
%18 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15)
%19 = add nuw nsw i64 %11, 1
%20 = icmp eq i64 %19, 4096
br i1 %20, label %21, label %10
21: ; preds = %10
%22 = tail call i32 @out_port(i32 248, i32 2)
%23 = tail call i32 @out_port(i32 0, i32 3)
%24 = tail call i32 @out_port(i32 0, i32 6)
%25 = tail call i32 @out_port(i32 0, i32 0)
%26 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @fwrite(i8* %2, i32 1, i32 4096, i32* %26)
%28 = tail call i32 @fclose(i32* %26)
ret i32 0
}