#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(unsigned int);
int /*<<< orphan*/ bus_writebyte(unsigned int, int);
int /*<<< orphan*/ printf(char *, unsigned int, int, int);
int /*<<< orphan*/ usleep(int);
void test_port(unsigned data, unsigned ctrl, unsigned stat) {
  int i, r;
  for (i = 0; i < 8; i++) {
    bus_writebyte(data, 1 << i);
    usleep(2);
    r = bus_readbyte(data);
    if (r != (1 << i)) {
      printf("Error on 0x%X, expected %x, got %x\n", data, 1 << i, r);
    }
  }
  for (i = 0; i < 6; i++) {
    bus_writebyte(ctrl, 1 << i);
    usleep(2);
    r = bus_readbyte(ctrl);
    if (r != (1 << i)) {
      printf("Error on 0x%X, expected %x, got %x\n", ctrl, 1 << i, r);
    }
  }
}


x86 assembly (clang, -O0):
<test_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
jge    11cf <test_port+0x8f>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    -0x4(%rbp),%edi
call   1050 <bus_readbyte@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
je     11bc <test_port+0x7c>
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x14(%rbp),%ecx
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
jmp    11c1 <test_port+0x81>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1158 <test_port+0x18>
movl   $0x0,-0x10(%rbp)
cmpl   $0x6,-0x10(%rbp)
jge    124d <test_port+0x10d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%ecx
mov    $0x1,%esi
shl    %cl,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    -0x8(%rbp),%edi
call   1050 <bus_readbyte@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cmp    %ecx,%eax
je     123a <test_port+0xfa>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x14(%rbp),%ecx
lea    0xdcb(%rip),%rdi
call   1040 <printf@plt>
jmp    123f <test_port+0xff>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d6 <test_port+0x96>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_port>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x1,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x1,%eax
je     117c <test_port+0x3c>
lea    0xe92(%rip),%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x2,%eax
je     11b3 <test_port+0x73>
lea    0xe5b(%rip),%rdi
mov    %ebp,%esi
mov    $0x2,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x4,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x4,%eax
je     11ea <test_port+0xaa>
lea    0xe24(%rip),%rdi
mov    %ebp,%esi
mov    $0x4,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x8,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x8,%eax
je     1221 <test_port+0xe1>
lea    0xded(%rip),%rdi
mov    %ebp,%esi
mov    $0x8,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x10,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x10,%eax
je     1258 <test_port+0x118>
lea    0xdb6(%rip),%rdi
mov    %ebp,%esi
mov    $0x10,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x20,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x20,%eax
je     128f <test_port+0x14f>
lea    0xd7f(%rip),%rdi
mov    %ebp,%esi
mov    $0x20,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x40,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x40,%eax
je     12c6 <test_port+0x186>
lea    0xd48(%rip),%rdi
mov    %ebp,%esi
mov    $0x40,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebp,%edi
mov    $0x80,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebp,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x80,%eax
je     12ff <test_port+0x1bf>
lea    0xd0f(%rip),%rdi
mov    %ebp,%esi
mov    $0x80,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x1,%eax
je     1336 <test_port+0x1f6>
lea    0xcd8(%rip),%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x2,%eax
je     136d <test_port+0x22d>
lea    0xca1(%rip),%rdi
mov    %ebx,%esi
mov    $0x2,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x4,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x4,%eax
je     13a4 <test_port+0x264>
lea    0xc6a(%rip),%rdi
mov    %ebx,%esi
mov    $0x4,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x8,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x8,%eax
je     13db <test_port+0x29b>
lea    0xc33(%rip),%rdi
mov    %ebx,%esi
mov    $0x8,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x10,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x10,%eax
je     1412 <test_port+0x2d2>
lea    0xbfc(%rip),%rdi
mov    %ebx,%esi
mov    $0x10,%edx
mov    %eax,%ecx
call   1040 <printf@plt>
mov    %ebx,%edi
mov    $0x20,%esi
call   1030 <bus_writebyte@plt>
mov    $0x2,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
call   1050 <bus_readbyte@plt>
cmp    $0x20,%eax
jne    143b <test_port+0x2fb>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xbbe(%rip),%rdi
mov    %ebx,%esi
mov    $0x20,%edx
mov    %eax,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Error on 0x%X, expected %x, got %x\0A\00"
define void @test_port(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %31, %3
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 8
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = load i32, i32* %4
%14 = load i32, i32* %7
%15 = shl i32 1, %14
%16 = call i32 @bus_writebyte(i32 %13, i32 %15)
%17 = call i32 @usleep(i32 2)
%18 = load i32, i32* %4
%19 = call i32 @bus_readbyte(i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = load i32, i32* %7
%22 = shl i32 1, %21
%23 = icmp ne i32 %20, %22
br i1 %23, label %24, label %30
24: ; preds = %12
%25 = load i32, i32* %4
%26 = load i32, i32* %7
%27 = shl i32 1, %26
%28 = load i32, i32* %8
%29 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %27, i32 %28)
br label %30
30: ; preds = %24, %12
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %9
34: ; preds = %9
store i32 0, i32* %7
br label %35
35: ; preds = %57, %34
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 6
br i1 %37, label %38, label %60
38: ; preds = %35
%39 = load i32, i32* %5
%40 = load i32, i32* %7
%41 = shl i32 1, %40
%42 = call i32 @bus_writebyte(i32 %39, i32 %41)
%43 = call i32 @usleep(i32 2)
%44 = load i32, i32* %5
%45 = call i32 @bus_readbyte(i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = load i32, i32* %7
%48 = shl i32 1, %47
%49 = icmp ne i32 %46, %48
br i1 %49, label %50, label %56
50: ; preds = %38
%51 = load i32, i32* %5
%52 = load i32, i32* %7
%53 = shl i32 1, %52
%54 = load i32, i32* %8
%55 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %51, i32 %53, i32 %54)
br label %56
56: ; preds = %50, %38
br label %57
57: ; preds = %56
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %35
60: ; preds = %35
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"Error on 0x%X, expected %x, got %x\0A\00"
define void @test_port(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @bus_writebyte(i32 %0, i32 1)
%5 = tail call i32 @usleep(i32 2)
%6 = tail call i32 @bus_readbyte(i32 %0)
%7 = icmp eq i32 %6, 1
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 1, i32 %6)
br label %10
10: ; preds = %3, %8
%11 = tail call i32 @bus_writebyte(i32 %0, i32 2)
%12 = tail call i32 @usleep(i32 2)
%13 = tail call i32 @bus_readbyte(i32 %0)
%14 = icmp eq i32 %13, 2
br i1 %14, label %17, label %15
15: ; preds = %10
%16 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 2, i32 %13)
br label %17
17: ; preds = %15, %10
%18 = tail call i32 @bus_writebyte(i32 %0, i32 4)
%19 = tail call i32 @usleep(i32 2)
%20 = tail call i32 @bus_readbyte(i32 %0)
%21 = icmp eq i32 %20, 4
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 4, i32 %20)
br label %24
24: ; preds = %22, %17
%25 = tail call i32 @bus_writebyte(i32 %0, i32 8)
%26 = tail call i32 @usleep(i32 2)
%27 = tail call i32 @bus_readbyte(i32 %0)
%28 = icmp eq i32 %27, 8
br i1 %28, label %31, label %29
29: ; preds = %24
%30 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 8, i32 %27)
br label %31
31: ; preds = %29, %24
%32 = tail call i32 @bus_writebyte(i32 %0, i32 16)
%33 = tail call i32 @usleep(i32 2)
%34 = tail call i32 @bus_readbyte(i32 %0)
%35 = icmp eq i32 %34, 16
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 16, i32 %34)
br label %38
38: ; preds = %36, %31
%39 = tail call i32 @bus_writebyte(i32 %0, i32 32)
%40 = tail call i32 @usleep(i32 2)
%41 = tail call i32 @bus_readbyte(i32 %0)
%42 = icmp eq i32 %41, 32
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 32, i32 %41)
br label %45
45: ; preds = %43, %38
%46 = tail call i32 @bus_writebyte(i32 %0, i32 64)
%47 = tail call i32 @usleep(i32 2)
%48 = tail call i32 @bus_readbyte(i32 %0)
%49 = icmp eq i32 %48, 64
br i1 %49, label %52, label %50
50: ; preds = %45
%51 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 64, i32 %48)
br label %52
52: ; preds = %50, %45
%53 = tail call i32 @bus_writebyte(i32 %0, i32 128)
%54 = tail call i32 @usleep(i32 2)
%55 = tail call i32 @bus_readbyte(i32 %0)
%56 = icmp eq i32 %55, 128
br i1 %56, label %59, label %57
57: ; preds = %52
%58 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 128, i32 %55)
br label %59
59: ; preds = %57, %52
%60 = tail call i32 @bus_writebyte(i32 %1, i32 1)
%61 = tail call i32 @usleep(i32 2)
%62 = tail call i32 @bus_readbyte(i32 %1)
%63 = icmp eq i32 %62, 1
br i1 %63, label %66, label %64
64: ; preds = %59
%65 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 1, i32 %62)
br label %66
66: ; preds = %59, %64
%67 = tail call i32 @bus_writebyte(i32 %1, i32 2)
%68 = tail call i32 @usleep(i32 2)
%69 = tail call i32 @bus_readbyte(i32 %1)
%70 = icmp eq i32 %69, 2
br i1 %70, label %73, label %71
71: ; preds = %66
%72 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 2, i32 %69)
br label %73
73: ; preds = %71, %66
%74 = tail call i32 @bus_writebyte(i32 %1, i32 4)
%75 = tail call i32 @usleep(i32 2)
%76 = tail call i32 @bus_readbyte(i32 %1)
%77 = icmp eq i32 %76, 4
br i1 %77, label %80, label %78
78: ; preds = %73
%79 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 4, i32 %76)
br label %80
80: ; preds = %78, %73
%81 = tail call i32 @bus_writebyte(i32 %1, i32 8)
%82 = tail call i32 @usleep(i32 2)
%83 = tail call i32 @bus_readbyte(i32 %1)
%84 = icmp eq i32 %83, 8
br i1 %84, label %87, label %85
85: ; preds = %80
%86 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 8, i32 %83)
br label %87
87: ; preds = %85, %80
%88 = tail call i32 @bus_writebyte(i32 %1, i32 16)
%89 = tail call i32 @usleep(i32 2)
%90 = tail call i32 @bus_readbyte(i32 %1)
%91 = icmp eq i32 %90, 16
br i1 %91, label %94, label %92
92: ; preds = %87
%93 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 16, i32 %90)
br label %94
94: ; preds = %92, %87
%95 = tail call i32 @bus_writebyte(i32 %1, i32 32)
%96 = tail call i32 @usleep(i32 2)
%97 = tail call i32 @bus_readbyte(i32 %1)
%98 = icmp eq i32 %97, 32
br i1 %98, label %101, label %99
99: ; preds = %94
%100 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 32, i32 %97)
br label %101
101: ; preds = %99, %94
ret void
}