#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 EXIT_FAILURE;
int EXIT_SUCCESS;
unsigned char inb(unsigned long);
scalar_t__ ioperm(unsigned long, unsigned long, int);
int /*<<< orphan*/ outb(unsigned char, unsigned long);
int /*<<< orphan*/ printf(char *, unsigned char, unsigned long);
int /*<<< orphan*/ usleep(int);
int do_ioperm(unsigned long addr, unsigned long nports) {
  unsigned char zero = 0, readout = 0;
  if (ioperm(addr, nports, 1))
    return EXIT_FAILURE;
  printf("Writing: %6d  to  %lx\n", zero, addr);
  outb(zero, addr);
  usleep(1000);
  readout = inb(addr + 1);
  printf("Reading: %6d from %lx\n", readout, addr + 1);
  if (ioperm(addr, nports, 0))
    return EXIT_FAILURE;
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<do_ioperm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movb   $0x0,-0x1a(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1050 <ioperm@plt>
cmp    $0x0,%rax
je     1195 <do_ioperm+0x45>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <do_ioperm+0xd4>
mov    -0x19(%rbp),%al
mov    -0x10(%rbp),%rdx
lea    0xe5d(%rip),%rdi
movzbl %al,%esi
call   1030 <printf@plt>
mov    -0x19(%rbp),%al
mov    -0x10(%rbp),%rsi
movzbl %al,%edi
call   1040 <outb@plt>
mov    $0x3e8,%edi
call   1070 <usleep@plt>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1060 <inb@plt>
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
mov    -0x10(%rbp),%rdx
add    $0x1,%rdx
lea    0xe31(%rip),%rdi
movzbl %al,%esi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1050 <ioperm@plt>
cmp    $0x0,%rax
je     1218 <do_ioperm+0xc8>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <do_ioperm+0xd4>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ioperm>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1,%edx
call   1050 <ioperm@plt>
test   %rax,%rax
jne    11c8 <do_ioperm+0x78>
lea    0xe8f(%rip),%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <outb@plt>
mov    $0x3e8,%edi
call   1070 <usleep@plt>
lea    0x1(%rbx),%r15
mov    %r15,%rdi
call   1060 <inb@plt>
movzbl %al,%esi
lea    0xe72(%rip),%rdi
mov    %r15,%rdx
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <ioperm@plt>
test   %rax,%rax
jne    11c8 <do_ioperm+0x78>
mov    0x2e12(%rip),%rax
jmp    11cf <do_ioperm+0x7f>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [23 x i8] c"Writing: %6d to %lx\0A\00"
@.str.1 = [23 x i8] c"Reading: %6d from %lx\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @do_ioperm(i64 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8
%7 = alloca i8
store i64 %0, i64* %4
store i64 %1, i64* %5
store i8 0, i8* %6
store i8 0, i8* %7
%8 = load i64, i64* %4
%9 = load i64, i64* %5
%10 = call i64 @ioperm(i64 %8, i64 %9, i32 1)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %3
br label %37
14: ; preds = %2
%15 = load i8, i8* %6
%16 = load i64, i64* %4
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 zeroext %15, i64 %16)
%18 = load i8, i8* %6
%19 = load i64, i64* %4
%20 = call i32 @outb(i8 zeroext %18, i64 %19)
%21 = call i32 @usleep(i32 1000)
%22 = load i64, i64* %4
%23 = add i64 %22, 1
%24 = call zeroext i8 @inb(i64 %23)
store i8 %24, i8* %7
%25 = load i8, i8* %7
%26 = load i64, i64* %4
%27 = add i64 %26, 1
%28 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %25, i64 %27)
%29 = load i64, i64* %4
%30 = load i64, i64* %5
%31 = call i64 @ioperm(i64 %29, i64 %30, i32 0)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %14
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %3
br label %37
35: ; preds = %14
%36 = load i32, i32* @EXIT_SUCCESS
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %33, %12
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [23 x i8] c"Writing: %6d to %lx\0A\00"
@.str.1 = [23 x i8] c"Reading: %6d from %lx\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @do_ioperm(i64 %0, i64 %1) {
%3 = tail call i64 @ioperm(i64 %0, i64 %1, i32 1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %15
5: ; preds = %2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 zeroext 0, i64 %0)
%7 = tail call i32 @outb(i8 zeroext 0, i64 %0)
%8 = tail call i32 @usleep(i32 1000)
%9 = add i64 %0, 1
%10 = tail call zeroext i8 @inb(i64 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %10, i64 %9)
%12 = tail call i64 @ioperm(i64 %0, i64 %1, i32 0)
%13 = icmp eq i64 %12, 0
%14 = select i1 %13, i32* @EXIT_SUCCESS, i32* @EXIT_FAILURE
br label %15
15: ; preds = %5, %2
%16 = phi i32* [ @EXIT_FAILURE, %2 ], [ %14, %5 ]
%17 = load i32, i32* %16
ret i32 %17
}