#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 */
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ WaitForReception(int);
int /*<<< orphan*/ exit(int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
unsigned char ReceiveHeader(int serial_if) {
  struct pollfd poll_data;
  unsigned char header;
  int signaled_count;
  poll_data.fd = serial_if;
  poll_data.events = POLLIN;
  signaled_count = poll(&poll_data, 1, 1000);
  if (signaled_count == 0) {
    printf("It has taken an unreasonable amount of time to receive data\n");
    exit(-1);
  }
  header = 'X';
  while (1) {
    int read_size;
    char read_value;
    read_size = read(serial_if, &read_value, 1);
    if (read_size == -1) {
      printf("Error reading from serial port\n");
      exit(-1);
    } else if (read_size == 0) {
      WaitForReception(1);
      continue;
    }
    if (read_value == 'A')
      header = read_value;
    else if (read_value == 'M')
      header = read_value;
    else if (read_value == 'G')
      header = read_value;
    else if (read_value == '\n')
      break;
    else if (read_value != '\r') {
      printf("Unexpected data received\n");
      exit(-1);
    }
  }
  return header;
}


x86 assembly (clang, -O0):
<ReceiveHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1050 <poll@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11a3 <ReceiveHeader+0x53>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movb   $0x58,-0x11(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11db <ReceiveHeader+0x8b>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11f4 <ReceiveHeader+0xa4>
mov    $0x1,%edi
call   1060 <WaitForReception@plt>
jmp    11a7 <ReceiveHeader+0x57>
jmp    11f9 <ReceiveHeader+0xa9>
movsbl -0x1d(%rbp),%eax
cmp    $0x41,%eax
jne    1211 <ReceiveHeader+0xc1>
mov    -0x1d(%rbp),%al
mov    %al,-0x11(%rbp)
jmp    128a <ReceiveHeader+0x13a>
movsbl -0x1d(%rbp),%eax
cmp    $0x4d,%eax
jne    1229 <ReceiveHeader+0xd9>
mov    -0x1d(%rbp),%al
mov    %al,-0x11(%rbp)
jmp    1285 <ReceiveHeader+0x135>
movsbl -0x1d(%rbp),%eax
cmp    $0x47,%eax
jne    1241 <ReceiveHeader+0xf1>
mov    -0x1d(%rbp),%al
mov    %al,-0x11(%rbp)
jmp    1280 <ReceiveHeader+0x130>
movsbl -0x1d(%rbp),%eax
cmp    $0xa,%eax
jne    1253 <ReceiveHeader+0x103>
jmp    128f <ReceiveHeader+0x13f>
movsbl -0x1d(%rbp),%eax
cmp    $0xd,%eax
je     1276 <ReceiveHeader+0x126>
lea    0xdf6(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    127b <ReceiveHeader+0x12b>
jmp    1280 <ReceiveHeader+0x130>
jmp    1285 <ReceiveHeader+0x135>
jmp    128a <ReceiveHeader+0x13a>
jmp    11a7 <ReceiveHeader+0x57>
movzbl -0x11(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReceiveHeader>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %edi,0x8(%rsp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     1208 <ReceiveHeader+0xb8>
mov    $0x58,%bl
lea    0x7(%rsp),%r14
jmp    119e <ReceiveHeader+0x4e>
mov    $0x1,%edi
call   1060 <WaitForReception@plt>
mov    %ebx,%eax
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1190 <ReceiveHeader+0x40>
cmp    $0xffffffff,%eax
je     11e9 <ReceiveHeader+0x99>
movzbl 0x7(%rsp),%eax
cmp    $0x46,%al
jg     11d0 <ReceiveHeader+0x80>
cmp    $0xd,%al
je     119a <ReceiveHeader+0x4a>
cmp    $0x41,%al
je     119c <ReceiveHeader+0x4c>
jmp    11da <ReceiveHeader+0x8a>
nopl   0x0(%rax)
cmp    $0x47,%al
je     119c <ReceiveHeader+0x4c>
cmp    $0x4d,%al
je     119c <ReceiveHeader+0x4c>
jmp    11ff <ReceiveHeader+0xaf>
cmp    $0xa,%al
jne    11ff <ReceiveHeader+0xaf>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xdfa(%rip),%rdi
jmp    11f0 <ReceiveHeader+0xa0>
lea    0xe29(%rip),%rdi
jmp    11f0 <ReceiveHeader+0xa0>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@.str = [61 x i8] c"It has taken an unreasonable amount of time to receive data\0A\00"
@.str.1 = [32 x i8] c"Error reading from serial port\0A\00"
@.str.2 = [26 x i8] c"Unexpected data received\0A\00"
define zeroext i8 @ReceiveHeader(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.pollfd
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @POLLIN
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
store i32 %10, i32* %11
%12 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 1000)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %1
store i8 88, i8* %4
br label %19
19: ; preds = %18, %30, %67
%20 = load i32, i32* %2
%21 = call i32 @read(i32 %20, i8* %7, i32 1)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %19
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @WaitForReception(i32 1)
br label %19
32: ; preds = %27
br label %33
33: ; preds = %32
%34 = load i8, i8* %7
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 65
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = load i8, i8* %7
store i8 %38, i8* %4
br label %67
39: ; preds = %33
%40 = load i8, i8* %7
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 77
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = load i8, i8* %7
store i8 %44, i8* %4
br label %66
45: ; preds = %39
%46 = load i8, i8* %7
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 71
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = load i8, i8* %7
store i8 %50, i8* %4
br label %65
51: ; preds = %45
%52 = load i8, i8* %7
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 10
br i1 %54, label %55, label %56
55: ; preds = %51
br label %68
56: ; preds = %51
%57 = load i8, i8* %7
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 13
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%62 = call i32 @exit(i32 -1)
unreachable
63: ; preds = %56
br label %64
64: ; preds = %63
br label %65
65: ; preds = %64, %49
br label %66
66: ; preds = %65, %43
br label %67
67: ; preds = %66, %37
br label %19
68: ; preds = %55
%69 = load i8, i8* %4
ret i8 %69
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@str = [25 x i8] c"Unexpected data received\00"
@str.3 = [31 x i8] c"Error reading from serial port\00"
@str.4 = [60 x i8] c"It has taken an unreasonable amount of time to receive data\00"
define zeroext i8 @ReceiveHeader(i32 %0) {
%2 = alloca %struct.pollfd
%3 = alloca i8
%4 = bitcast %struct.pollfd* %2 to i8*
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = load i32, i32* @POLLIN
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 %6, i32* %7
%8 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 1000)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.4, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %1, %27
%14 = phi i8 [ %28, %27 ], [ 88, %1 ]
%15 = call i32 @read(i32 %0, i8* %3, i32 1)
switch i32 %15, label %21 [
i32 -1, label %16
i32 0, label %19
]
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %13
%20 = call i32 @WaitForReception(i32 1)
br label %27
21: ; preds = %13
%22 = load i8, i8* %3
switch i8 %22, label %23 [
i8 65, label %27
i8 77, label %27
i8 71, label %27
i8 10, label %29
i8 13, label %26
]
23: ; preds = %21
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %21
br label %27
27: ; preds = %26, %21, %21, %21, %19
%28 = phi i8 [ %14, %19 ], [ %22, %21 ], [ %22, %21 ], [ %22, %21 ], [ %14, %26 ]
br label %13
29: ; preds = %21
ret i8 %14
}