#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int, char *, int);
int conn_com(int fd) {
  char inbuf[10], outbuf[10];
  int ret;
  ret = read(fd, inbuf, 1);
  if (inbuf[0] == 0x35) {
    outbuf[0] = 'A';
    ret = write(fd, outbuf, 1);
    ret = read(fd, inbuf, 1);
    if (inbuf[0] == 0x35) {
      outbuf[0] = 'U';
      outbuf[1] = '3';
      ret = write(fd, outbuf, 2);
      ret = read(fd, inbuf, 1);
      if (inbuf[0] == 'R') {
        printf("Connect board success!\n");
        return 0;
      } else {
        perror("Hardware 3 Error");
        return -1;
      }
    } else {
      perror("Hardware 2 Error");
      return -1;
    }
  } else {
    perror("Hardware 2 Error");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<conn_com>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
movsbl -0x12(%rbp),%eax
cmp    $0x35,%eax
jne    122a <conn_com+0xea>
movb   $0x41,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
movsbl -0x12(%rbp),%eax
cmp    $0x35,%eax
jne    1212 <conn_com+0xd2>
movb   $0x55,-0x1c(%rbp)
movb   $0x33,-0x1b(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
movsbl -0x12(%rbp),%eax
cmp    $0x52,%eax
jne    11fa <conn_com+0xba>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    123d <conn_com+0xfd>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <conn_com+0xfd>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <conn_com+0xfd>
lea    0xdf8(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn_com>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmpb   $0x35,0xc(%rsp)
jne    11cf <conn_com+0x8f>
movb   $0x41,0x16(%rsp)
lea    0x16(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmpb   $0x35,0xc(%rsp)
jne    11cf <conn_com+0x8f>
movw   $0x3355,0x16(%rsp)
lea    0x16(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <write@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmpb   $0x52,0xc(%rsp)
jne    11d8 <conn_com+0x98>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe3b(%rip),%rdi
jmp    11df <conn_com+0x9f>
lea    0xe21(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c9 <conn_com+0x89>

LLVM IR (-O0):
@.str = [24 x i8] c"Connect board success!\0A\00"
@.str.1 = [17 x i8] c"Hardware 3 Error\00"
@.str.2 = [17 x i8] c"Hardware 2 Error\00"
define i32 @conn_com(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [10 x i8]
%5 = alloca [10 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 1)
store i32 %9, i32* %6
%10 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 53
br i1 %13, label %14, label %45
14: ; preds = %1
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
store i8 65, i8* %15
%16 = load i32, i32* %3
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 1)
store i32 %18, i32* %6
%19 = load i32, i32* %3
%20 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1)
store i32 %21, i32* %6
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 53
br i1 %25, label %26, label %43
26: ; preds = %14
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
store i8 85, i8* %27
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 1
store i8 51, i8* %28
%29 = load i32, i32* %3
%30 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 2)
store i32 %31, i32* %6
%32 = load i32, i32* %3
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 1)
store i32 %34, i32* %6
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 82
br i1 %38, label %39, label %41
39: ; preds = %26
%40 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %47
41: ; preds = %26
%42 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
43: ; preds = %14
%44 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
45: ; preds = %1
%46 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %47
47: ; preds = %45, %43, %41, %39
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"Hardware 3 Error\00"
@.str.2 = [17 x i8] c"Hardware 2 Error\00"
@str = [23 x i8] c"Connect board success!\00"
define i32 @conn_com(i32 %0) {
%2 = alloca [10 x i8]
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %4, i32 1)
%7 = load i8, i8* %4
%8 = icmp eq i8 %7, 53
br i1 %8, label %9, label %26
9: ; preds = %1
store i8 65, i8* %5
%10 = call i32 @write(i32 %0, i8* %5, i32 1)
%11 = call i32 @read(i32 %0, i8* %4, i32 1)
%12 = load i8, i8* %4
%13 = icmp eq i8 %12, 53
br i1 %13, label %14, label %24
14: ; preds = %9
store i8 85, i8* %5
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 1
store i8 51, i8* %15
%16 = call i32 @write(i32 %0, i8* %5, i32 2)
%17 = call i32 @read(i32 %0, i8* %4, i32 1)
%18 = load i8, i8* %4
%19 = icmp eq i8 %18, 82
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %28
22: ; preds = %14
%23 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %28
24: ; preds = %9
%25 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %28
26: ; preds = %1
%27 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %24, %22, %20
%29 = phi i32 [ 0, %20 ], [ -1, %22 ], [ -1, %24 ], [ -1, %26 ]
ret i32 %29
}