#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(int);
int /*<<< orphan*/ bus_writebyte(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ test_port(int, int, int);
int /*<<< orphan*/ usleep(int);
void bist() {
  int i, j;
  printf("Testing LPT0\n");
  test_port(0x78, 0x7A, 0x79);
  printf("Testing LPT1\n");
  test_port(0xBC, 0xBE, 0xBD);
  for (j = 0; j < 1024 * 1024; j++) {
    bus_writebyte(0x78, 0xFF);
  }
  for (j = 0; j < 1024 * 1024; j++) {
    bus_writebyte(0x78, 0);
  }
  bus_writebyte(0x7A, 1 << 5);
  for (i = 0; i < 8; i++) {
    bus_writebyte(0x78, 1 << i);
    printf("LPT0_DATA: %X\n", bus_readbyte(0x78));
  }
  bus_writebyte(0x7A, 0x2);
  bus_writebyte(0xBE, 0x0);
  usleep(100);
  bus_writebyte(0xBE, 0x2);
}


x86 assembly (clang, -O0):
<bist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x78,%edi
mov    $0x7a,%esi
mov    $0x79,%edx
call   1040 <test_port@plt>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xbc,%edi
mov    $0xbe,%esi
mov    $0xbd,%edx
call   1040 <test_port@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x100000,-0x8(%rbp)
jge    11cd <bist+0x7d>
mov    $0x78,%edi
mov    $0xff,%esi
call   1030 <bus_writebyte@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a3 <bist+0x53>
movl   $0x0,-0x8(%rbp)
cmpl   $0x100000,-0x8(%rbp)
jge    11fb <bist+0xab>
mov    $0x78,%edi
xor    %esi,%esi
call   1030 <bus_writebyte@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11d4 <bist+0x84>
mov    $0x7a,%edi
mov    $0x20,%esi
call   1030 <bus_writebyte@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x8,-0x4(%rbp)
jge    1257 <bist+0x107>
mov    -0x4(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
mov    $0x78,%edi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1060 <bus_readbyte@plt>
mov    %eax,%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1211 <bist+0xc1>
mov    $0x7a,%edi
mov    $0x2,%esi
call   1030 <bus_writebyte@plt>
mov    $0xbe,%edi
xor    %esi,%esi
call   1030 <bus_writebyte@plt>
mov    $0x64,%edi
call   1070 <usleep@plt>
mov    $0xbe,%edi
mov    $0x2,%esi
call   1030 <bus_writebyte@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bist>:
push   %rbx
lea    0xea7(%rip),%rdi
call   1040 <puts@plt>
mov    $0x78,%edi
mov    $0x7a,%esi
mov    $0x79,%edx
call   1050 <test_port@plt>
lea    0xe94(%rip),%rdi
call   1040 <puts@plt>
mov    $0xbc,%edi
mov    $0xbe,%esi
mov    $0xbd,%edx
call   1050 <test_port@plt>
mov    $0x100000,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0x78,%edi
mov    $0xff,%esi
call   1030 <bus_writebyte@plt>
add    $0xffffffff,%ebx
jne    11b0 <bist+0x50>
mov    $0x100000,%ebx
nopl   0x0(%rax)
mov    $0x78,%edi
xor    %esi,%esi
call   1030 <bus_writebyte@plt>
add    $0xffffffff,%ebx
jne    11d0 <bist+0x70>
mov    $0x7a,%edi
mov    $0x20,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
mov    $0x1,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
lea    0xdf0(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x2,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x4,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x8,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x10,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x20,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x40,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x78,%edi
mov    $0x80,%esi
call   1030 <bus_writebyte@plt>
mov    $0x78,%edi
call   1070 <bus_readbyte@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x7a,%edi
mov    $0x2,%esi
call   1030 <bus_writebyte@plt>
mov    $0xbe,%edi
xor    %esi,%esi
call   1030 <bus_writebyte@plt>
mov    $0x64,%edi
call   1080 <usleep@plt>
mov    $0xbe,%edi
mov    $0x2,%esi
pop    %rbx
jmp    1030 <bus_writebyte@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Testing LPT0\0A\00"
@.str.1 = [14 x i8] c"Testing LPT1\0A\00"
@.str.2 = [15 x i8] c"LPT0_DATA: %X\0A\00"
define void @bist() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @test_port(i32 120, i32 122, i32 121)
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @test_port(i32 188, i32 190, i32 189)
store i32 0, i32* %2
br label %7
7: ; preds = %12, %0
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 1048576
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = call i32 @bus_writebyte(i32 120, i32 255)
br label %12
12: ; preds = %10
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
store i32 %14, i32* %2
br label %7
15: ; preds = %7
store i32 0, i32* %2
br label %16
16: ; preds = %21, %15
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 1048576
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = call i32 @bus_writebyte(i32 120, i32 0)
br label %21
21: ; preds = %19
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %16
24: ; preds = %16
%25 = call i32 @bus_writebyte(i32 122, i32 32)
store i32 0, i32* %1
br label %26
26: ; preds = %35, %24
%27 = load i32, i32* %1
%28 = icmp slt i32 %27, 8
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i32, i32* %1
%31 = shl i32 1, %30
%32 = call i32 @bus_writebyte(i32 120, i32 %31)
%33 = call i32 @bus_readbyte(i32 120)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %29
%36 = load i32, i32* %1
%37 = add nsw i32 %36, 1
store i32 %37, i32* %1
br label %26
38: ; preds = %26
%39 = call i32 @bus_writebyte(i32 122, i32 2)
%40 = call i32 @bus_writebyte(i32 190, i32 0)
%41 = call i32 @usleep(i32 100)
%42 = call i32 @bus_writebyte(i32 190, i32 2)
ret void
}

LLVM IR (-O2):
@.str.2 = [15 x i8] c"LPT0_DATA: %X\0A\00"
@str = [13 x i8] c"Testing LPT0\00"
@str.3 = [13 x i8] c"Testing LPT1\00"
define void @bist() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @test_port(i32 120, i32 122, i32 121)
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.3, i64 0, i64 0))
%4 = tail call i32 @test_port(i32 188, i32 190, i32 189)
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %8, %5 ]
%7 = tail call i32 @bus_writebyte(i32 120, i32 255)
%8 = add nuw nsw i32 %6, 1
%9 = icmp eq i32 %8, 1048576
br i1 %9, label %10, label %5
10: ; preds = %5, %10
%11 = phi i32 [ %13, %10 ], [ 0, %5 ]
%12 = tail call i32 @bus_writebyte(i32 120, i32 0)
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i32 %13, 1048576
br i1 %14, label %15, label %10
15: ; preds = %10
%16 = tail call i32 @bus_writebyte(i32 122, i32 32)
%17 = tail call i32 @bus_writebyte(i32 120, i32 1)
%18 = tail call i32 @bus_readbyte(i32 120)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = tail call i32 @bus_writebyte(i32 120, i32 2)
%21 = tail call i32 @bus_readbyte(i32 120)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = tail call i32 @bus_writebyte(i32 120, i32 4)
%24 = tail call i32 @bus_readbyte(i32 120)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = tail call i32 @bus_writebyte(i32 120, i32 8)
%27 = tail call i32 @bus_readbyte(i32 120)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = tail call i32 @bus_writebyte(i32 120, i32 16)
%30 = tail call i32 @bus_readbyte(i32 120)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = tail call i32 @bus_writebyte(i32 120, i32 32)
%33 = tail call i32 @bus_readbyte(i32 120)
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = tail call i32 @bus_writebyte(i32 120, i32 64)
%36 = tail call i32 @bus_readbyte(i32 120)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = tail call i32 @bus_writebyte(i32 120, i32 128)
%39 = tail call i32 @bus_readbyte(i32 120)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = tail call i32 @bus_writebyte(i32 122, i32 2)
%42 = tail call i32 @bus_writebyte(i32 190, i32 0)
%43 = tail call i32 @usleep(i32 100)
%44 = tail call i32 @bus_writebyte(i32 190, i32 2)
ret void
}