#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*/ close(int);
int open_port(char *);
int /*<<< orphan*/ printbytes(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int GetPosition(void) {
  char ReturnString[64];
  int num;
  char term[] = "/dev/ttyd2";
  int SerialPort;
  SerialPort = open_port(term);
  if (!SerialPort)
    return 0;
  write(SerialPort, "c", 1);
  write(SerialPort, "\r", 1);
  sleep(1);
  num = read(SerialPort, ReturnString, 13);
  if (num < 13)
    printf("Read Failed!  %s %d\n", (num < 0 ? "Error Number:" : "Bytes Read:"),
           num);
  else if (ReturnString[12] == 0xD)
    printf("Read Worked!\n");
  else
    printf("Read Failed!  Last byte not terminator!\n");
  printf("Bytes Read:\n");
  printbytes(ReturnString, num);
  close(SerialPort);
  return 0;
}


x86 assembly (clang, -O0):
<GetPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0xe81(%rip),%rax
mov    %rax,-0x5f(%rbp)
mov    0xe7e(%rip),%ax
mov    %ax,-0x57(%rbp)
mov    0xe76(%rip),%al
mov    %al,-0x55(%rbp)
lea    -0x5f(%rbp),%rdi
call   1070 <open_port@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jne    11b9 <GetPosition+0x49>
movl   $0x0,-0x4(%rbp)
jmp    1293 <GetPosition+0x123>
mov    -0x64(%rbp),%edi
lea    0xe48(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x64(%rbp),%edi
lea    0xe36(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x64(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xd,%edx
call   1060 <read@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xd,-0x54(%rbp)
jge    1237 <GetPosition+0xc7>
mov    -0x54(%rbp),%ecx
lea    0xe1f(%rip),%rsi
lea    0xe0a(%rip),%rax
cmp    $0x0,%ecx
cmovl  %rax,%rsi
mov    -0x54(%rbp),%edx
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126a <GetPosition+0xfa>
movsbl -0x44(%rbp),%eax
cmp    $0xd,%eax
jne    1257 <GetPosition+0xe7>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1265 <GetPosition+0xf5>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    126a <GetPosition+0xfa>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1090 <printbytes@plt>
mov    -0x64(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetPosition>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
movabs $0x7974742f7665642f,%rax
74 74 79
mov    %rax,(%rsp)
movl   $0x326479,0x7(%rsp)
mov    %rsp,%rdi
call   1080 <open_port@plt>
test   %eax,%eax
je     1257 <GetPosition+0xd7>
mov    %eax,%ebx
lea    0xe56(%rip),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xe45(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xd,%edx
call   1070 <read@plt>
mov    %eax,%ebp
cmp    $0xc,%eax
jg     121c <GetPosition+0x9c>
test   %ebp,%ebp
lea    0xe25(%rip),%rax
lea    0xe54(%rip),%rsi
cmovs  %rax,%rsi
lea    0xdfe(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1238 <GetPosition+0xb8>
cmpb   $0xd,0x1c(%rsp)
jne    122c <GetPosition+0xac>
lea    0xe3c(%rip),%rdi
jmp    1233 <GetPosition+0xb3>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
mov    %ebp,%esi
call   10a0 <printbytes@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.GetPosition.term = [11 x i8] c"/dev/ttyd2\00"
@.str = [2 x i8] c"c\00"
@.str.1 = [2 x i8] c"\0D\00"
@.str.2 = [21 x i8] c"Read Failed! %s %d\0A\00"
@.str.3 = [14 x i8] c"Error Number:\00"
@.str.4 = [12 x i8] c"Bytes Read:\00"
@.str.5 = [14 x i8] c"Read Worked!\0A\00"
@.str.6 = [41 x i8] c"Read Failed! Last byte not terminator!\0A\00"
@.str.7 = [13 x i8] c"Bytes Read:\0A\00"
define i32 @GetPosition() {
%1 = alloca i32
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca [11 x i8]
%5 = alloca i32
%6 = bitcast [11 x i8]* %4 to i8*
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%8 = call i32 @open_port(i8* %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %0
store i32 0, i32* %1
br label %47
12: ; preds = %0
%13 = load i32, i32* %5
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%15 = load i32, i32* %5
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = call i32 @sleep(i32 1)
%18 = load i32, i32* %5
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 13)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 13
br i1 %22, label %23, label %30
23: ; preds = %12
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 0
%26 = zext i1 %25 to i64
%27 = select i1 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0)
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %28)
br label %40
30: ; preds = %12
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 12
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 13
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %37, %35
br label %40
40: ; preds = %39, %23
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%43 = load i32, i32* %3
%44 = call i32 @printbytes(i8* %42, i32 %43)
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
store i32 0, i32* %1
br label %47
47: ; preds = %40, %11
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@__const.GetPosition.term = [11 x i8] c"/dev/ttyd2\00"
@.str = [2 x i8] c"c\00"
@.str.1 = [2 x i8] c"\0D\00"
@.str.2 = [21 x i8] c"Read Failed! %s %d\0A\00"
@.str.3 = [14 x i8] c"Error Number:\00"
@str = [40 x i8] c"Read Failed! Last byte not terminator!\00"
@str.8 = [12 x i8] c"Bytes Read:\00"
@str.9 = [13 x i8] c"Read Worked!\00"
define i32 @GetPosition() {
%1 = alloca [64 x i8]
%2 = alloca [11 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%5 = call i32 @open_port(i8* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %0
%8 = call i32 @write(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%9 = call i32 @write(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%10 = call i32 @sleep(i32 1)
%11 = call i32 @read(i32 %5, i8* %3, i32 13)
%12 = icmp slt i32 %11, 13
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = icmp slt i32 %11, 0
%15 = select i1 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %15, i32 %11)
br label %25
17: ; preds = %7
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 12
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 13
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
br label %25
23: ; preds = %17
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %21, %23, %13
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
%27 = call i32 @printbytes(i8* %3, i32 %11)
%28 = call i32 @close(i32 %5)
br label %29
29: ; preds = %0, %25
ret i32 0
}