#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 tx_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;
  data = tx_handshake;
  while (*data) {
    __builtin_stwio(((volatile int *)0x10001010), (*data));
    *data++;
    count++;
    if (count >= 9)
      break;
  }
  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);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<tx_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)
lea    -0x26(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11df <tx_Handshake+0x9f>
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     11da <tx_Handshake+0x9a>
jmp    11df <tx_Handshake+0x9f>
jmp    1198 <tx_Handshake+0x58>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12a7 <tx_Handshake+0x167>
mov    $0x10001010,%edi
call   1040 <__builtin_ldwio@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1ff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
and    $0x8000,%eax
shr    $0xf,%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <alt_up_rs232_check_parity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12a2 <tx_Handshake+0x162>
movslq -0x34(%rbp),%rax
movsbl -0x2d(%rbp,%rax,1),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    126a <tx_Handshake+0x12a>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x7,-0x34(%rbp)
jne    1265 <tx_Handshake+0x125>
movl   $0x1,-0x1c(%rbp)
jmp    1271 <tx_Handshake+0x131>
movl   $0x0,-0x34(%rbp)
mov    -0x4(%rbp),%esi
lea    0xd8f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xd8a(%rip),%rdi
call   1030 <printf@plt>
jmp    11e6 <tx_Handshake+0xa6>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tx_Handshake>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x10001010,%edi
mov    $0x74,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x72,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x61,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x6e,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x73,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x6d,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x69,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0x74,%esi
call   1060 <__builtin_stwio@plt>
mov    $0x10001010,%edi
mov    $0xa,%esi
call   1060 <__builtin_stwio@plt>
lea    0xe3c(%rip),%r15
lea    0xe44(%rip),%r12
xor    %r13d,%r13d
jmp    1244 <tx_Handshake+0x104>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%ebp
movslq %r13d,%rax
lea    0x1(%rax),%r13d
cmp    $0x7,%r13d
sete   %cl
lea    0xdf9(%rip),%rdx
cmp    %bl,(%rax,%rdx,1)
sete   %al
mov    $0x0,%edx
cmovne %edx,%r13d
and    %cl,%al
movzbl %al,%r14d
lea    0xde4(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
movsbl %bl,%esi
mov    %r15,%rdi
call   1030 <printf@plt>
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
test   %r14d,%r14d
jne    1267 <tx_Handshake+0x127>
mov    $0x10001010,%edi
call   1040 <__builtin_ldwio@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <alt_up_rs232_check_parity@plt>
test   $0x8000,%ebx
jne    11f0 <tx_Handshake+0xb0>
xor    %r14d,%r14d
test   %r14d,%r14d
je     1244 <tx_Handshake+0x104>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@__const.tx_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 @tx_Handshake() {
%1 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 116)
%2 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 114)
%3 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 97)
%4 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 110)
%5 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 115)
%6 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 109)
%7 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 105)
%8 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 116)
%9 = tail call i32 @__builtin_stwio(i32* inttoptr (i64 268439568 to i32*), i8 zeroext 10)
br label %10
10: ; preds = %0, %32
%11 = phi i32 [ 0, %0 ], [ %34, %32 ]
%12 = tail call i32 @__builtin_ldwio(i32* inttoptr (i64 268439568 to i32*))
%13 = tail call i32 @alt_up_rs232_check_parity(i32 %12)
%14 = and i32 %12, 32768
%15 = icmp eq i32 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %10
%17 = trunc i32 %12 to i8
%18 = sext i32 %11 to i64
%19 = getelementptr inbounds [7 x i8], [7 x i8]* @__const.tx_Handshake.rx_handshake, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, %17
%22 = add nsw i32 %11, 1
%23 = icmp eq i32 %22, 7
%24 = select i1 %21, i1 %23, i1 false
%25 = zext i1 %24 to i32
%26 = select i1 %21, i32 %22, i32 0
%27 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12)
%28 = shl i32 %12, 24
%29 = ashr exact i32 %28, 24
%30 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %32
32: ; preds = %16, %10
%33 = phi i32 [ %25, %16 ], [ 0, %10 ]
%34 = phi i32 [ %26, %16 ], [ %11, %10 ]
%35 = icmp eq i32 %33, 0
br i1 %35, label %10, label %36
36: ; preds = %32
ret i32 1
}