#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 long ReceiveULong(int serial_if) {
  struct pollfd poll_data;
  unsigned long received_value;
  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);
  }
  received_value = 0;
  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 >= '0' && read_value <= '9')
      received_value = received_value * 16 + (read_value - '0');
    else if (read_value >= 'a' && read_value <= 'f')
      received_value = received_value * 16 + (read_value - 'a' + 10);
    else if (read_value >= 'A' && read_value <= 'F')
      received_value = received_value * 16 + (read_value - 'A' + 10);
    else if (read_value == '\n')
      break;
    else if (read_value != '\r') {
      printf("Unexpected data received\n");
      exit(-1);
    }
  }
  return received_value;
}


x86 assembly (clang, -O0):
<ReceiveULong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a3 <ReceiveULong+0x53>
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11df <ReceiveULong+0x8f>
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jne    11f8 <ReceiveULong+0xa8>
mov    $0x1,%edi
call   1060 <WaitForReception@plt>
jmp    11ab <ReceiveULong+0x5b>
jmp    11fd <ReceiveULong+0xad>
movsbl -0x21(%rbp),%eax
cmp    $0x30,%eax
jl     1235 <ReceiveULong+0xe5>
movsbl -0x21(%rbp),%eax
cmp    $0x39,%eax
jg     1235 <ReceiveULong+0xe5>
mov    -0x18(%rbp),%rax
shl    $0x4,%rax
movsbl -0x21(%rbp),%ecx
sub    $0x30,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    12f4 <ReceiveULong+0x1a4>
movsbl -0x21(%rbp),%eax
cmp    $0x61,%eax
jl     1270 <ReceiveULong+0x120>
movsbl -0x21(%rbp),%eax
cmp    $0x66,%eax
jg     1270 <ReceiveULong+0x120>
mov    -0x18(%rbp),%rax
shl    $0x4,%rax
movsbl -0x21(%rbp),%ecx
sub    $0x61,%ecx
add    $0xa,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    12ef <ReceiveULong+0x19f>
movsbl -0x21(%rbp),%eax
cmp    $0x41,%eax
jl     12ab <ReceiveULong+0x15b>
movsbl -0x21(%rbp),%eax
cmp    $0x46,%eax
jg     12ab <ReceiveULong+0x15b>
mov    -0x18(%rbp),%rax
shl    $0x4,%rax
movsbl -0x21(%rbp),%ecx
sub    $0x41,%ecx
add    $0xa,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    12ea <ReceiveULong+0x19a>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    12bd <ReceiveULong+0x16d>
jmp    12f9 <ReceiveULong+0x1a9>
movsbl -0x21(%rbp),%eax
cmp    $0xd,%eax
je     12e0 <ReceiveULong+0x190>
lea    0xd8c(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    12e5 <ReceiveULong+0x195>
jmp    12ea <ReceiveULong+0x19a>
jmp    12ef <ReceiveULong+0x19f>
jmp    12f4 <ReceiveULong+0x1a4>
jmp    11ab <ReceiveULong+0x5b>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReceiveULong>:
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     122b <ReceiveULong+0xdb>
xor    %ebx,%ebx
lea    0x7(%rsp),%r14
jmp    119a <ReceiveULong+0x4a>
mov    $0x1,%edi
call   1060 <WaitForReception@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1190 <ReceiveULong+0x40>
cmp    $0xffffffff,%eax
je     120c <ReceiveULong+0xbc>
movsbl 0x7(%rsp),%eax
lea    -0x30(%rax),%ecx
cmp    $0x9,%cl
ja     11d0 <ReceiveULong+0x80>
shl    $0x4,%rbx
add    $0xffffffd0,%eax
add    %rax,%rbx
jmp    119a <ReceiveULong+0x4a>
nopl   0x0(%rax,%rax,1)
lea    -0x61(%rax),%ecx
cmp    $0x5,%cl
ja     11e4 <ReceiveULong+0x94>
shl    $0x4,%rbx
add    $0xffffffa9,%eax
add    %rax,%rbx
jmp    119a <ReceiveULong+0x4a>
lea    -0x41(%rax),%ecx
cmp    $0x5,%cl
ja     11f8 <ReceiveULong+0xa8>
shl    $0x4,%rbx
add    $0xffffffc9,%eax
add    %rax,%rbx
jmp    119a <ReceiveULong+0x4a>
cmp    $0xd,%al
je     119a <ReceiveULong+0x4a>
cmp    $0xa,%al
jne    1222 <ReceiveULong+0xd2>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xdd7(%rip),%rdi
jmp    1213 <ReceiveULong+0xc3>
lea    0xe06(%rip),%rdi
jmp    1213 <ReceiveULong+0xc3>

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 i64 @ReceiveULong(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.pollfd
%4 = alloca i64
%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 i64 0, i64* %4
br label %19
19: ; preds = %18, %30, %99
%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 sge i32 %35, 48
br i1 %36, label %37, label %49
37: ; preds = %33
%38 = load i8, i8* %7
%39 = sext i8 %38 to i32
%40 = icmp sle i32 %39, 57
br i1 %40, label %41, label %49
41: ; preds = %37
%42 = load i64, i64* %4
%43 = mul i64 %42, 16
%44 = load i8, i8* %7
%45 = sext i8 %44 to i32
%46 = sub nsw i32 %45, 48
%47 = sext i32 %46 to i64
%48 = add i64 %43, %47
store i64 %48, i64* %4
br label %99
49: ; preds = %37, %33
%50 = load i8, i8* %7
%51 = sext i8 %50 to i32
%52 = icmp sge i32 %51, 97
br i1 %52, label %53, label %66
53: ; preds = %49
%54 = load i8, i8* %7
%55 = sext i8 %54 to i32
%56 = icmp sle i32 %55, 102
br i1 %56, label %57, label %66
57: ; preds = %53
%58 = load i64, i64* %4
%59 = mul i64 %58, 16
%60 = load i8, i8* %7
%61 = sext i8 %60 to i32
%62 = sub nsw i32 %61, 97
%63 = add nsw i32 %62, 10
%64 = sext i32 %63 to i64
%65 = add i64 %59, %64
store i64 %65, i64* %4
br label %98
66: ; preds = %53, %49
%67 = load i8, i8* %7
%68 = sext i8 %67 to i32
%69 = icmp sge i32 %68, 65
br i1 %69, label %70, label %83
70: ; preds = %66
%71 = load i8, i8* %7
%72 = sext i8 %71 to i32
%73 = icmp sle i32 %72, 70
br i1 %73, label %74, label %83
74: ; preds = %70
%75 = load i64, i64* %4
%76 = mul i64 %75, 16
%77 = load i8, i8* %7
%78 = sext i8 %77 to i32
%79 = sub nsw i32 %78, 65
%80 = add nsw i32 %79, 10
%81 = sext i32 %80 to i64
%82 = add i64 %76, %81
store i64 %82, i64* %4
br label %97
83: ; preds = %70, %66
%84 = load i8, i8* %7
%85 = sext i8 %84 to i32
%86 = icmp eq i32 %85, 10
br i1 %86, label %87, label %88
87: ; preds = %83
br label %100
88: ; preds = %83
%89 = load i8, i8* %7
%90 = sext i8 %89 to i32
%91 = icmp ne i32 %90, 13
br i1 %91, label %92, label %95
92: ; preds = %88
%93 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%94 = call i32 @exit(i32 -1)
unreachable
95: ; preds = %88
br label %96
96: ; preds = %95
br label %97
97: ; preds = %96, %74
br label %98
98: ; preds = %97, %57
br label %99
99: ; preds = %98, %41
br label %19
100: ; preds = %87
%101 = load i64, i64* %4
ret i64 %101
}

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 i64 @ReceiveULong(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, %51
%14 = phi i64 [ %52, %51 ], [ 0, %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 %51
21: ; preds = %13
%22 = load i8, i8* %3
%23 = sext i8 %22 to i32
%24 = add i8 %22, -48
%25 = icmp ult i8 %24, 10
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = shl i64 %14, 4
%28 = add nsw i32 %23, -48
%29 = zext i32 %28 to i64
%30 = add i64 %27, %29
br label %51
31: ; preds = %21
%32 = add i8 %22, -97
%33 = icmp ult i8 %32, 6
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = shl i64 %14, 4
%36 = add nsw i32 %23, -87
%37 = zext i32 %36 to i64
%38 = add i64 %35, %37
br label %51
39: ; preds = %31
%40 = add i8 %22, -65
%41 = icmp ult i8 %40, 6
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = shl i64 %14, 4
%44 = add nsw i32 %23, -55
%45 = zext i32 %44 to i64
%46 = add i64 %43, %45
br label %51
47: ; preds = %39
switch i8 %22, label %48 [
i8 10, label %53
i8 13, label %51
]
48: ; preds = %47
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%50 = call i32 @exit(i32 -1)
unreachable
51: ; preds = %26, %42, %34, %47, %19
%52 = phi i64 [ %14, %19 ], [ %30, %26 ], [ %38, %34 ], [ %46, %42 ], [ %14, %47 ]
br label %13
53: ; preds = %47
ret i64 %14
}