#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 port_in(unsigned int);
int /*<<< orphan*/ port_out(unsigned int, int);
int /*<<< orphan*/ timing_uPause(int);
int stv5730_detect(unsigned int port) {
  int i;
  for (i = 0; i < 10; i++) {
    port_out(port, 0x01);
    timing_uPause(400);
    if ((port_in(port + 1) & 0x40) == 0)
      return -1;
    port_out(port, 0);
    timing_uPause(400);
    if ((port_in(port + 1) & 0x40) != 0)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<stv5730_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11d0 <stv5730_detect+0xa0>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1050 <port_out@plt>
mov    $0x190,%edi
call   1030 <timing_uPause@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1040 <port_in@plt>
and    $0x40,%eax
cmp    $0x0,%eax
jne    1186 <stv5730_detect+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <stv5730_detect+0xa7>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <port_out@plt>
mov    $0x190,%edi
call   1030 <timing_uPause@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1040 <port_in@plt>
and    $0x40,%eax
cmp    $0x0,%eax
je     11bd <stv5730_detect+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <stv5730_detect+0xa7>
jmp    11c2 <stv5730_detect+0x92>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1142 <stv5730_detect+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stv5730_detect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    0x1(%r15),%r14d
mov    $0xa,%ebx
mov    $0xffffffff,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    $0x1,%esi
call   1050 <port_out@plt>
mov    $0x190,%edi
call   1030 <timing_uPause@plt>
mov    %r14d,%edi
call   1040 <port_in@plt>
test   $0x40,%al
je     119a <stv5730_detect+0x6a>
mov    %r15d,%edi
xor    %esi,%esi
call   1050 <port_out@plt>
mov    $0x190,%edi
call   1030 <timing_uPause@plt>
mov    %r14d,%edi
call   1040 <port_in@plt>
test   $0x40,%al
jne    119a <stv5730_detect+0x6a>
add    $0xffffffff,%ebx
jne    1150 <stv5730_detect+0x20>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stv5730_detect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %29, %1
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 10
br i1 %7, label %8, label %32
8: ; preds = %5
%9 = load i32, i32* %3
%10 = call i32 @port_out(i32 %9, i32 1)
%11 = call i32 @timing_uPause(i32 400)
%12 = load i32, i32* %3
%13 = add i32 %12, 1
%14 = call i32 @port_in(i32 %13)
%15 = and i32 %14, 64
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %8
store i32 -1, i32* %2
br label %33
18: ; preds = %8
%19 = load i32, i32* %3
%20 = call i32 @port_out(i32 %19, i32 0)
%21 = call i32 @timing_uPause(i32 400)
%22 = load i32, i32* %3
%23 = add i32 %22, 1
%24 = call i32 @port_in(i32 %23)
%25 = and i32 %24, 64
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %18
store i32 -1, i32* %2
br label %33
28: ; preds = %18
br label %29
29: ; preds = %28
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %5
32: ; preds = %5
store i32 0, i32* %2
br label %33
33: ; preds = %32, %27, %17
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
define i32 @stv5730_detect(i32 %0) {
%2 = add i32 %0, 1
br label %6
3: ; preds = %13
%4 = add nuw nsw i32 %7, 1
%5 = icmp eq i32 %4, 10
br i1 %5, label %19, label %6
6: ; preds = %1, %3
%7 = phi i32 [ 0, %1 ], [ %4, %3 ]
%8 = tail call i32 @port_out(i32 %0, i32 1)
%9 = tail call i32 @timing_uPause(i32 400)
%10 = tail call i32 @port_in(i32 %2)
%11 = and i32 %10, 64
%12 = icmp eq i32 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %6
%14 = tail call i32 @port_out(i32 %0, i32 0)
%15 = tail call i32 @timing_uPause(i32 400)
%16 = tail call i32 @port_in(i32 %2)
%17 = and i32 %16, 64
%18 = icmp eq i32 %17, 0
br i1 %18, label %3, label %19
19: ; preds = %3, %13, %6
%20 = phi i32 [ -1, %6 ], [ -1, %13 ], [ 0, %3 ]
ret i32 %20
}