#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 */
unsigned int __builtin_ldwio(int volatile *);
int /*<<< orphan*/ __builtin_stwio(int volatile *, unsigned char);
unsigned int alt_up_rs232_check_parity(unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int);
int rx_Handshake(void) {
  unsigned int data_reg;
  unsigned char *data;
  unsigned int pe = 0;
  int retVal;
  int messageRecieved = 0;
  unsigned char tx_handshake[] = {'t', 'r', 'a', 'n',  's',
                                  'm', 'i', 't', '\n', '\0'};
  unsigned char rx_handshake[] = {'r', 'e', 'c', 'e', 'i', 'v', 'e'};
  int count = 0;
  while (messageRecieved == 0) {
    data_reg = __builtin_ldwio(((volatile int *)0x10001010));
    *data = (data_reg & 0x000001FF);
    retVal = ((data_reg & 0x00008000) >> 15) - 1;
    pe = alt_up_rs232_check_parity(data_reg);
    if (retVal != -1) {
      if (((char)rx_handshake[count]) == ((char)*data)) {
        count++;
        if (count == 7)
          messageRecieved = 1;
      } else {
        count = 0;
      }
      printf("data_reg: %h\n", data_reg);
      printf("character: %c\n", (char)(*data));
      printf("parity: %d\n", pe);
    }
  }
  data = tx_handshake;
  count = 0;
  while (*data) {
    __builtin_stwio(((volatile int *)0x10001010), (*data));
    *data++;
    count++;
    if (count >= 9)
      break;
  }
}


x86 assembly (clang, -O0):
<rx_Handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0xea3(%rip),%rax
mov    %rax,-0x26(%rbp)
mov    0xea0(%rip),%ax
mov    %ax,-0x1e(%rbp)
mov    0xec1(%rip),%eax
mov    %eax,-0x2d(%rbp)
mov    0xebb(%rip),%ax
mov    %ax,-0x29(%rbp)
mov    0xeb3(%rip),%al
mov    %al,-0x27(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1251 <rx_Handshake+0x111>
mov    $0x10001010,%edi
call   1040 <__builtin_ldwio@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1ff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
and    $0x8000,%eax
shr    $0xf,%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <alt_up_rs232_check_parity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     124c <rx_Handshake+0x10c>
movslq -0x34(%rbp),%rax
movsbl -0x2d(%rbp,%rax,1),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1214 <rx_Handshake+0xd4>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x7,-0x34(%rbp)
jne    120f <rx_Handshake+0xcf>
movl   $0x1,-0x1c(%rbp)
jmp    121b <rx_Handshake+0xdb>
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%esi
lea    0xde5(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xde0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xde0(%rip),%rdi
call   1030 <printf@plt>
jmp    1190 <rx_Handshake+0x50>
lea    -0x26(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     12a7 <rx_Handshake+0x167>
mov    -0x10(%rbp),%rax
mov    $0x10001010,%edi
movzbl (%rax),%esi
call   1060 <__builtin_stwio@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x9,-0x34(%rbp)
jl     12a2 <rx_Handshake+0x162>
jmp    12a7 <rx_Handshake+0x167>
jmp    1260 <rx_Handshake+0x120>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rx_Handshake>:
push   %rax
mov    $0x10001010,%edi
call   1030 <__builtin_ldwio@plt>

LLVM IR (-O0):
@__const.rx_Handshake.tx_handshake = [10 x i8] c"transmit\0A\00"
@__const.rx_Handshake.rx_handshake = [7 x i8] c"receive"
@.str = [14 x i8] c"data_reg: %h\0A\00"
@.str.1 = [15 x i8] c"character: %c\0A\00"
@.str.2 = [12 x i8] c"parity: %d\0A\00"
define i32 @rx_Handshake() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca [7 x i8]
%9 = alloca i32
store i32 0, i32* %4
store i32 0, i32* %6
%10 = bitcast [10 x i8]* %7 to i8*
%11 = bitcast [7 x i8]* %8 to i8*
store i32 0, i32* %9
br label %12
12: ; preds = %56, %0
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %57
15: ; preds = %12
%16 = call i32 @__builtin_ldwio(i32* inttoptr (i64 268439568 to i32*))
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = and i32 %17, 511
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %3
store i8 %19, i8* %20
%21 = load i32, i32* %2
%22 = and i32 %21, 32768
%23 = lshr i32 %22, 15
%24 = sub i32 %23, 1
store i32 %24, i32* %5
%25 = load i32, i32* %2
%26 = call i32 @alt_up_rs232_check_parity(i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %56
29: ; preds = %15
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i8*, i8** %3
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %34, %37
br i1 %38, label %39, label %46
39: ; preds = %29
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 7
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 1, i32* %6
br label %45
45: ; preds = %44, %39
br label %47
46: ; preds = %29
store i32 0, i32* %9
br label %47
47: ; preds = %46, %45
%48 = load i32, i32* %2
%49 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %48)
%50 = load i8*, i8** %3
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %4
%55 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %47, %15
br label %12
57: ; preds = %12
%58 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
store i8* %58, i8** %3
store i32 0, i32* %9
br label %59
59: ; preds = %75, %57
%60 = load i8*, i8** %3
%61 = load i8, i8* %60
%62 = icmp ne i8 %61, 0
br i1 %62, label %63, label %76
63: ; preds = %59
%64 = load i8*, i8** %3
%65 = load i8, i8* %64
%66 = call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext %65)
%67 = load i8*, i8** %3
%68 = getelementptr inbounds i8, i8* %67, i32 1
store i8* %68, i8** %3
%69 = load i8, i8* %67
%70 = load i32, i32* %9
%71 = add nsw i32 %70, 1
store i32 %71, i32* %9
%72 = load i32, i32* %9
%73 = icmp sge i32 %72, 9
br i1 %73, label %74, label %75
74: ; preds = %63
br label %76
75: ; preds = %63
br label %59
76: ; preds = %74, %59
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
define i32 @rx_Handshake() {
%1 = tail call i32 @__builtin_ldwio(i32* inttoptr (i64 268439568 to i32*))
unreachable
}