#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 */
typedef int /*<<< orphan*/ detect_response;
typedef int /*<<< orphan*/ detect_cmd;

/* Variables and functions */
int irlink_read(int const, unsigned char *, int);
int /*<<< orphan*/ irlink_read_flush(int const);
int irlink_write(int const, unsigned char *, int);
scalar_t__ waitfordata(int);
int irlink_detect(const int port) {
  unsigned char detect_cmd[] = {0x81};
  if (port == -1) {
    return -1;
  }
  irlink_read_flush(port);
  if (irlink_write(port, detect_cmd, sizeof(detect_cmd)) ==
      sizeof(detect_cmd)) {
    unsigned char detect_response = 0;
    if (waitfordata(500000) &&
        irlink_read(port, &detect_response, sizeof(detect_response)) ==
            sizeof(detect_response)) {
      if (detect_response == 0x81) {
        return 0;
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<irlink_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0xeaf(%rip),%al
mov    %al,-0x9(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    116a <irlink_detect+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <irlink_detect+0xb0>
mov    -0x8(%rbp),%edi
call   1060 <irlink_read_flush@plt>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <irlink_write@plt>
cltq
cmp    $0x1,%rax
jne    11e9 <irlink_detect+0xa9>
movb   $0x0,-0xa(%rbp)
mov    $0x7a120,%edi
call   1050 <waitfordata@plt>
cmp    $0x0,%rax
je     11e4 <irlink_detect+0xa4>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1030 <irlink_read@plt>
cltq
cmp    $0x1,%rax
jne    11e4 <irlink_detect+0xa4>
movzbl -0xa(%rbp),%eax
cmp    $0x81,%eax
jne    11df <irlink_detect+0x9f>
movl   $0x0,-0x4(%rbp)
jmp    11f0 <irlink_detect+0xb0>
jmp    11e4 <irlink_detect+0xa4>
jmp    11e9 <irlink_detect+0xa9>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<irlink_detect>:
push   %rbp
push   %rbx
push   %rax
movb   $0x81,0x7(%rsp)
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%edi
je     11a2 <irlink_detect+0x62>
mov    %edi,%ebp
call   1060 <irlink_read_flush@plt>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <irlink_write@plt>
cmp    $0x1,%eax
jne    11a2 <irlink_detect+0x62>
movb   $0x0,0x6(%rsp)
mov    $0x7a120,%edi
call   1050 <waitfordata@plt>
test   %rax,%rax
je     11a2 <irlink_detect+0x62>
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <irlink_read@plt>
cmp    $0x1,%eax
jne    11a2 <irlink_detect+0x62>
cmpb   $0x81,0x6(%rsp)
jne    11a2 <irlink_detect+0x62>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.irlink_detect.detect_cmd = [1 x i8] c"\81"
define i32 @irlink_detect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1 x i8]
%5 = alloca i8
store i32 %0, i32* %3
%6 = bitcast [1 x i8]* %4 to i8*
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %34
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i32 @irlink_read_flush(i32 %11)
%13 = load i32, i32* %3
%14 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%15 = call i32 @irlink_write(i32 %13, i8* %14, i32 1)
%16 = sext i32 %15 to i64
%17 = icmp eq i64 %16, 1
br i1 %17, label %18, label %33
18: ; preds = %10
store i8 0, i8* %5
%19 = call i64 @waitfordata(i32 500000)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = load i32, i32* %3
%23 = call i32 @irlink_read(i32 %22, i8* %5, i32 1)
%24 = sext i32 %23 to i64
%25 = icmp eq i64 %24, 1
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i8, i8* %5
%28 = zext i8 %27 to i32
%29 = icmp eq i32 %28, 129
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 0, i32* %2
br label %34
31: ; preds = %26
br label %32
32: ; preds = %31, %21, %18
br label %33
33: ; preds = %32, %10
store i32 -1, i32* %2
br label %34
34: ; preds = %33, %30, %9
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
define i32 @irlink_detect(i32 %0) {
%2 = alloca [1 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
store i8 -127, i8* %4
%5 = icmp eq i32 %0, -1
br i1 %5, label %21, label %6
6: ; preds = %1
%7 = tail call i32 @irlink_read_flush(i32 %0)
%8 = call i32 @irlink_write(i32 %0, i8* %4, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %21
10: ; preds = %6
store i8 0, i8* %3
%11 = call i64 @waitfordata(i32 500000)
%12 = icmp eq i64 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = call i32 @irlink_read(i32 %0, i8* %3, i32 1)
%15 = icmp eq i32 %14, 1
%16 = load i8, i8* %3
%17 = icmp eq i8 %16, -127
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %20
19: ; preds = %13
br label %21
20: ; preds = %10, %13
br label %21
21: ; preds = %6, %20, %19, %1
%22 = phi i32 [ 0, %19 ], [ -1, %1 ], [ -1, %20 ], [ -1, %6 ]
ret i32 %22
}