#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 */
struct resource {
  int dummy;
};

/* Variables and functions */
unsigned char inb_p(unsigned int);
int /*<<< orphan*/ msleep(unsigned int);
int /*<<< orphan*/ printk(char *, int);
int /*<<< orphan*/ release_region(unsigned long, unsigned long);
struct resource *request_region(unsigned long, unsigned long, char *);
int pcwd_checkcard(int base_addr) {
  int port0;
  int last_port0;
  int port1;
  int last_port1;
  int i;
  int retval;
  struct resource *tmp;
  unsigned char tmp___0;
  unsigned char tmp___1;
  unsigned char tmp___2;
  unsigned char tmp___3;
  {
    tmp = request_region((unsigned long)base_addr, 4UL, "PCWD");
    if (!tmp) {
      printk("<6>pcwd: Port 0x%04x unavailable\n", base_addr);
      return (0);
    }
    retval = 0;
    tmp___0 = inb_p((unsigned int)base_addr);
    port0 = (int)tmp___0;
    tmp___1 = inb_p((unsigned int)(base_addr + 1));
    port1 = (int)tmp___1;
    if (port0 != 255) {
      goto _L;
    } else if (port1 != 255) {
    _L:
      i = 0;
      while (i < 4) {
        msleep(500U);
        last_port0 = port0;
        last_port1 = port1;
        tmp___2 = inb_p((unsigned int)base_addr);
        port0 = (int)tmp___2;
        tmp___3 = inb_p((unsigned int)(base_addr + 1));
        port1 = (int)tmp___3;
        if ((port0 ^ last_port0) & 4) {
          retval = 1;
          break;
        } else if ((port1 ^ last_port1) & 2) {
          retval = 1;
          break;
        }
        i++;
      }
    }
    release_region((unsigned long)base_addr, 4UL);
    return (retval);
  }
}


x86 assembly (clang, -O0):
<pcwd_checkcard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movslq -0x8(%rbp),%rdi
mov    $0x4,%esi
lea    0xe95(%rip),%rdx
call   1050 <request_region@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119a <pcwd_checkcard+0x4a>
mov    -0x8(%rbp),%esi
lea    0xe7c(%rip),%rdi
call   1030 <printk@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <pcwd_checkcard+0x157>
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <inb_p@plt>
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1070 <inb_p@plt>
mov    %al,-0x2a(%rbp)
movzbl -0x2a(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xff,-0xc(%rbp)
je     11da <pcwd_checkcard+0x8a>
jmp    11ec <pcwd_checkcard+0x9c>
cmpl   $0xff,-0x14(%rbp)
je     128e <pcwd_checkcard+0x13e>
jmp    11ec <pcwd_checkcard+0x9c>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jge    1289 <pcwd_checkcard+0x139>
mov    $0x1f4,%edi
call   1040 <msleep@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <inb_p@plt>
mov    %al,-0x2b(%rbp)
movzbl -0x2b(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1070 <inb_p@plt>
mov    %al,-0x2c(%rbp)
movzbl -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
xor    -0x10(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1258 <pcwd_checkcard+0x108>
movl   $0x1,-0x20(%rbp)
jmp    1289 <pcwd_checkcard+0x139>
mov    -0x14(%rbp),%eax
xor    -0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1276 <pcwd_checkcard+0x126>
movl   $0x1,-0x20(%rbp)
jmp    1289 <pcwd_checkcard+0x139>
jmp    127b <pcwd_checkcard+0x12b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f3 <pcwd_checkcard+0xa3>
jmp    128e <pcwd_checkcard+0x13e>
jmp    1293 <pcwd_checkcard+0x143>
movslq -0x8(%rbp),%rdi
mov    $0x4,%esi
call   1060 <release_region@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcwd_checkcard>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
movslq %edi,%rbp
lea    0xe95(%rip),%rdx
mov    $0x4,%esi
mov    %rbp,%rdi
call   1050 <request_region@plt>
test   %rax,%rax
je     129c <pcwd_checkcard+0x14c>
mov    %r14d,%edi
call   1070 <inb_p@plt>
mov    %eax,%ebx
lea    0x1(%r14),%r12d
mov    %r12d,%edi
call   1070 <inb_p@plt>
mov    %eax,%r15d
cmp    $0xff,%bl
jne    11ac <pcwd_checkcard+0x5c>
xor    %r13d,%r13d
cmp    $0xff,%r15b
je     12b5 <pcwd_checkcard+0x165>
mov    %rbp,0x8(%rsp)
mov    $0x1f4,%edi
call   1040 <msleep@plt>
mov    %r14d,%edi
call   1070 <inb_p@plt>
mov    %r12d,%ebp
mov    %eax,%r12d
mov    %ebp,%edi
call   1070 <inb_p@plt>
xor    %r12b,%bl
mov    $0x1,%r13d
test   $0x4,%bl
jne    12b0 <pcwd_checkcard+0x160>
mov    %ebp,0x14(%rsp)
xor    %al,%r15b
test   $0x2,%r15b
jne    12b0 <pcwd_checkcard+0x160>
mov    %eax,%ebp
mov    $0x1f4,%edi
call   1040 <msleep@plt>
mov    %r14d,%edi
call   1070 <inb_p@plt>
mov    %eax,%ebx
mov    0x14(%rsp),%edi
call   1070 <inb_p@plt>
xor    %bl,%r12b
test   $0x4,%r12b
jne    12b0 <pcwd_checkcard+0x160>
mov    %eax,%r15d
xor    %al,%bpl
test   $0x2,%bpl
jne    12b0 <pcwd_checkcard+0x160>
mov    $0x1f4,%edi
call   1040 <msleep@plt>
mov    %r14d,%edi
call   1070 <inb_p@plt>
mov    %eax,%r12d
mov    0x14(%rsp),%edi
call   1070 <inb_p@plt>
xor    %r12b,%bl
test   $0x4,%bl
jne    12b0 <pcwd_checkcard+0x160>
xor    %al,%r15b
test   $0x2,%r15b
jne    12b0 <pcwd_checkcard+0x160>
mov    %eax,%r15d
mov    $0x1f4,%edi
call   1040 <msleep@plt>
mov    %r14d,%edi
call   1070 <inb_p@plt>
mov    %eax,%ebx
mov    0x14(%rsp),%edi
call   1070 <inb_p@plt>
xor    %bl,%r12b
test   $0x4,%r12b
mov    0x8(%rsp),%rbp
jne    12b5 <pcwd_checkcard+0x165>
xor    %al,%r15b
shr    %r15b
and    $0x1,%r15b
movzbl %r15b,%r13d
jmp    12b5 <pcwd_checkcard+0x165>
lea    0xd62(%rip),%rdi
mov    %r14d,%esi
call   1030 <printk@plt>
xor    %r13d,%r13d
jmp    12c2 <pcwd_checkcard+0x172>
mov    0x8(%rsp),%rbp
mov    $0x4,%esi
mov    %rbp,%rdi
call   1060 <release_region@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.resource = type { i32 }
@.str = [5 x i8] c"PCWD\00"
@.str.1 = [34 x i8] c"<6>pcwd: Port 0x%04x unavailable\0A\00"
define i32 @pcwd_checkcard(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.resource*
%11 = alloca i8
%12 = alloca i8
%13 = alloca i8
%14 = alloca i8
store i32 %0, i32* %3
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = call %struct.resource* @request_region(i64 %16, i64 4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store %struct.resource* %17, %struct.resource** %10
%18 = load %struct.resource*, %struct.resource** %10
%19 = icmp ne %struct.resource* %18, null
br i1 %19, label %23, label %20
20: ; preds = %1
%21 = load i32, i32* %3
%22 = call i32 @printk(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 0, i32* %2
br label %81
23: ; preds = %1
store i32 0, i32* %9
%24 = load i32, i32* %3
%25 = call zeroext i8 @inb_p(i32 %24)
store i8 %25, i8* %11
%26 = load i8, i8* %11
%27 = zext i8 %26 to i32
store i32 %27, i32* %4
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
%30 = call zeroext i8 @inb_p(i32 %29)
store i8 %30, i8* %12
%31 = load i8, i8* %12
%32 = zext i8 %31 to i32
store i32 %32, i32* %6
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 255
br i1 %34, label %35, label %36
35: ; preds = %23
br label %40
36: ; preds = %23
%37 = load i32, i32* %6
%38 = icmp ne i32 %37, 255
br i1 %38, label %39, label %75
39: ; preds = %36
br label %40
40: ; preds = %39, %35
store i32 0, i32* %8
br label %41
41: ; preds = %71, %40
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 4
br i1 %43, label %44, label %74
44: ; preds = %41
%45 = call i32 @msleep(i32 500)
%46 = load i32, i32* %4
store i32 %46, i32* %5
%47 = load i32, i32* %6
store i32 %47, i32* %7
%48 = load i32, i32* %3
%49 = call zeroext i8 @inb_p(i32 %48)
store i8 %49, i8* %13
%50 = load i8, i8* %13
%51 = zext i8 %50 to i32
store i32 %51, i32* %4
%52 = load i32, i32* %3
%53 = add nsw i32 %52, 1
%54 = call zeroext i8 @inb_p(i32 %53)
store i8 %54, i8* %14
%55 = load i8, i8* %14
%56 = zext i8 %55 to i32
store i32 %56, i32* %6
%57 = load i32, i32* %4
%58 = load i32, i32* %5
%59 = xor i32 %57, %58
%60 = and i32 %59, 4
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %44
store i32 1, i32* %9
br label %74
63: ; preds = %44
%64 = load i32, i32* %6
%65 = load i32, i32* %7
%66 = xor i32 %64, %65
%67 = and i32 %66, 2
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %63
store i32 1, i32* %9
br label %74
70: ; preds = %63
br label %71
71: ; preds = %70
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 1
store i32 %73, i32* %8
br label %41
74: ; preds = %69, %62, %41
br label %75
75: ; preds = %74, %36
br label %76
76: ; preds = %75
%77 = load i32, i32* %3
%78 = sext i32 %77 to i64
%79 = call i32 @release_region(i64 %78, i64 4)
%80 = load i32, i32* %9
store i32 %80, i32* %2
br label %81
81: ; preds = %76, %20
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
%struct.resource = type { i32 }
@.str = [5 x i8] c"PCWD\00"
@.str.1 = [34 x i8] c"<6>pcwd: Port 0x%04x unavailable\0A\00"
define i32 @pcwd_checkcard(i32 %0) {
%2 = sext i32 %0 to i64
%3 = tail call %struct.resource* @request_region(i64 %2, i64 4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq %struct.resource* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @printk(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %62
7: ; preds = %1
%8 = tail call zeroext i8 @inb_p(i32 %0)
%9 = add nsw i32 %0, 1
%10 = tail call zeroext i8 @inb_p(i32 %9)
%11 = icmp ne i8 %8, -1
%12 = icmp ne i8 %10, -1
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %14, label %59
14: ; preds = %7
%15 = tail call i32 @msleep(i32 500)
%16 = tail call zeroext i8 @inb_p(i32 %0)
%17 = tail call zeroext i8 @inb_p(i32 %9)
%18 = xor i8 %8, %16
%19 = and i8 %18, 4
%20 = icmp eq i8 %19, 0
br i1 %20, label %55, label %59
21: ; preds = %55
%22 = tail call i32 @msleep(i32 500)
%23 = tail call zeroext i8 @inb_p(i32 %0)
%24 = tail call zeroext i8 @inb_p(i32 %9)
%25 = xor i8 %16, %23
%26 = and i8 %25, 4
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %59
28: ; preds = %21
%29 = xor i8 %17, %24
%30 = and i8 %29, 2
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %59
32: ; preds = %28
%33 = tail call i32 @msleep(i32 500)
%34 = tail call zeroext i8 @inb_p(i32 %0)
%35 = tail call zeroext i8 @inb_p(i32 %9)
%36 = xor i8 %23, %34
%37 = and i8 %36, 4
%38 = icmp eq i8 %37, 0
br i1 %38, label %39, label %59
39: ; preds = %32
%40 = xor i8 %24, %35
%41 = and i8 %40, 2
%42 = icmp eq i8 %41, 0
br i1 %42, label %43, label %59
43: ; preds = %39
%44 = tail call i32 @msleep(i32 500)
%45 = tail call zeroext i8 @inb_p(i32 %0)
%46 = tail call zeroext i8 @inb_p(i32 %9)
%47 = xor i8 %34, %45
%48 = and i8 %47, 4
%49 = icmp eq i8 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %43
%51 = xor i8 %35, %46
%52 = lshr i8 %51, 1
%53 = and i8 %52, 1
%54 = zext i8 %53 to i32
br label %59
55: ; preds = %14
%56 = xor i8 %10, %17
%57 = and i8 %56, 2
%58 = icmp eq i8 %57, 0
br i1 %58, label %21, label %59
59: ; preds = %50, %55, %14, %21, %28, %32, %39, %43, %7
%60 = phi i32 [ 0, %7 ], [ 1, %14 ], [ 1, %55 ], [ 1, %21 ], [ 1, %28 ], [ 1, %32 ], [ 1, %39 ], [ 1, %43 ], [ %54, %50 ]
%61 = tail call i32 @release_region(i64 %2, i64 4)
br label %62
62: ; preds = %59, %5
%63 = phi i32 [ %60, %59 ], [ 0, %5 ]
ret i32 %63
}