#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct termios {
  int c_cflag;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int B2400;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int read_gp2y() {
  struct termios term;
  struct timeval timeout;
  unsigned char buff[32];
  fd_set rd_set;
  int fd, rc, i, total = 0;
  fd = open("/dev/ttyAMA0", O_RDONLY | O_NOCTTY);
  if (fd < 0) {
    printf("failed to open UART port: %s\n", strerror(errno));
    return -5;
  }
  tcgetattr(fd, &term);
  term.c_cflag = B2400 | CS8 | CLOCAL | CREAD;
  term.c_iflag = IGNPAR;
  term.c_oflag = 0;
  term.c_lflag = 0;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &term);
  timeout.tv_sec = 0;
  timeout.tv_usec = 100000;
  FD_ZERO(&rd_set);
  FD_SET(fd, &rd_set);
  rc = select(fd + 1, &rd_set, NULL, NULL, &timeout);
  if (rc <= 0) {
    printf("no response from dust sensor, please check "
           "your hardware settings.\n");
    close(fd);
    return -1;
  } else {
    if (!FD_ISSET(fd, &rd_set)) {
      printf("Ooops, something weird happend ...\n");
      close(fd);
      return -1;
    }
  }
  while (total < sizeof(buff)) {
    rc = read(fd, buff + total, sizeof(buff) - total);
    if (rc <= 0) {
      printf("failed to read data from UART: %s\n", strerror(errno));
      close(fd);
      return -1;
    }
    total += rc;
  }
  close(fd);
  if (total < 8)
    return -2;
  i = 0;
loop:
  while (buff[i] != 0xaa) {
    if (++i > (total - 7))
      return -2;
  }
  if (buff[++i + 5] != 0xff)
    goto loop;
  if (((buff[i] + buff[i + 1] + buff[i + 2] + buff[i + 3]) & 0xff) !=
      buff[i + 4])
    goto loop;
  return ((buff[i] << 8) | buff[i + 1]);
}


x86 assembly (clang, -O0):
<read_gp2y>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x74(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
lea    0xe15(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jge    1228 <read_gp2y+0x68>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    14c9 <read_gp2y+0x309>
mov    -0x68(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    0x2d8c(%rip),%rcx
or     (%rcx),%eax
mov    0x2d43(%rip),%rcx
or     (%rcx),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x68(%rbp),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x68(%rbp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   10b0 <tcsetattr@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x186a0,-0x38(%rbp)
lea    -0x64(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x68(%rbp),%edi
lea    -0x64(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x68(%rbp),%edi
add    $0x1,%edi
lea    -0x64(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jg     130a <read_gp2y+0x14a>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x68(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14c9 <read_gp2y+0x309>
mov    -0x68(%rbp),%edi
lea    -0x64(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1341 <read_gp2y+0x181>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x68(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14c9 <read_gp2y+0x309>
jmp    1346 <read_gp2y+0x186>
jmp    134b <read_gp2y+0x18b>
movslq -0x74(%rbp),%rax
cmp    $0x20,%rax
jae    13c8 <read_gp2y+0x208>
mov    -0x68(%rbp),%edi
lea    -0x60(%rbp),%rsi
movslq -0x74(%rbp),%rax
add    %rax,%rsi
movslq -0x74(%rbp),%rcx
mov    $0x20,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jg     13ba <read_gp2y+0x1fa>
mov    0x2c1a(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x68(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14c9 <read_gp2y+0x309>
mov    -0x6c(%rbp),%eax
add    -0x74(%rbp),%eax
mov    %eax,-0x74(%rbp)
jmp    134b <read_gp2y+0x18b>
mov    -0x68(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x8,-0x74(%rbp)
jge    13e6 <read_gp2y+0x226>
movl   $0xfffffffe,-0x4(%rbp)
jmp    14c9 <read_gp2y+0x309>
movl   $0x0,-0x70(%rbp)
jmp    13f2 <read_gp2y+0x232>
movslq -0x70(%rbp),%rax
movzbl -0x60(%rbp,%rax,1),%eax
cmp    $0xaa,%eax
je     142e <read_gp2y+0x26e>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
mov    -0x74(%rbp),%ecx
sub    $0x7,%ecx
cmp    %ecx,%eax
jle    1429 <read_gp2y+0x269>
movl   $0xfffffffe,-0x4(%rbp)
jmp    14c9 <read_gp2y+0x309>
jmp    13f2 <read_gp2y+0x232>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
add    $0x5,%eax
cltq
movzbl -0x60(%rbp,%rax,1),%eax
cmp    $0xff,%eax
je     1451 <read_gp2y+0x291>
jmp    13ed <read_gp2y+0x22d>
movslq -0x70(%rbp),%rax
movzbl -0x60(%rbp,%rax,1),%eax
mov    -0x70(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl -0x60(%rbp,%rcx,1),%ecx
add    %ecx,%eax
mov    -0x70(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movzbl -0x60(%rbp,%rcx,1),%ecx
add    %ecx,%eax
mov    -0x70(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movzbl -0x60(%rbp,%rcx,1),%ecx
add    %ecx,%eax
and    $0xff,%eax
mov    -0x70(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
movzbl -0x60(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
je     14aa <read_gp2y+0x2ea>
jmp    13ed <read_gp2y+0x22d>
movslq -0x70(%rbp),%rax
movzbl -0x60(%rbp,%rax,1),%eax
shl    $0x8,%eax
mov    -0x70(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl -0x60(%rbp,%rcx,1),%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_gp2y>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    0x2e05(%rip),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0b(%rip),%rdi
call   10d0 <open@plt>
test   %eax,%eax
js     13ed <read_gp2y+0x21d>
mov    %eax,%ebx
lea    0x40(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   10b0 <tcgetattr@plt>
mov    0x2d96(%rip),%rax
mov    0x2daf(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d64(%rip),%rax
or     (%rax),%ecx
mov    0x2da3(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x40(%rsp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x58(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x48(%rsp)
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <tcflush@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   10c0 <tcsetattr@plt>
movq   $0x0,0x38(%rsp)
movl   $0x186a0,0x30(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10f0 <FD_SET@plt>
mov    %ebx,%edi
add    $0x1,%edi
lea    0x30(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <select@plt>
test   %eax,%eax
jle    1413 <read_gp2y+0x243>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     141c <read_gp2y+0x24c>
xor    %ebp,%ebp
nop
mov    %ebp,%eax
lea    (%rsp,%rax,1),%rsi
add    $0x10,%rsi
mov    $0x20,%edx
sub    %ebp,%edx
mov    %ebx,%edi
call   1080 <read@plt>
test   %eax,%eax
jle    142a <read_gp2y+0x25a>
add    %eax,%ebp
cmp    $0x1f,%ebp
jbe    12d0 <read_gp2y+0x100>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xfffffffe,%eax
cmp    $0x8,%ebp
jl     1455 <read_gp2y+0x285>
add    $0xfffffff9,%ebp
xor    %esi,%esi
movabs $0x600000000,%r14
movabs $0x200000000,%r8
movabs $0x300000000,%r9
movabs $0x400000000,%r10
movabs $0x500000000,%r11
movabs $0x100000000,%r15
movslq %esi,%rdi
cmp    %ebp,%edi
mov    %ebp,%ebx
cmovg  %edi,%ebx
lea    0x1(%rdi),%esi
lea    (%rsp,%rdi,1),%rcx
add    $0x10,%rcx
shl    $0x20,%rdi
neg    %ebx
nopl   0x0(%rax,%rax,1)
cmpb   $0xaa,(%rcx)
je     138f <read_gp2y+0x1bf>
add    %r15,%rdi
add    $0x1,%rcx
lea    (%rbx,%rsi,1),%edx
add    $0x1,%edx
add    $0x1,%esi
cmp    $0x2,%edx
jne    1370 <read_gp2y+0x1a0>
jmp    1455 <read_gp2y+0x285>
lea    (%rdi,%r14,1),%rcx
sar    $0x20,%rcx
cmpb   $0xff,0x10(%rsp,%rcx,1)
jne    134d <read_gp2y+0x17d>
movslq %esi,%rcx
movzbl 0x10(%rsp,%rcx,1),%r12d
lea    (%rdi,%r8,1),%rcx
sar    $0x20,%rcx
movzbl 0x10(%rsp,%rcx,1),%ecx
lea    (%rcx,%r12,1),%edx
lea    (%rdi,%r9,1),%rbx
sar    $0x20,%rbx
add    0x10(%rsp,%rbx,1),%dl
lea    (%rdi,%r10,1),%rbx
sar    $0x20,%rbx
add    0x10(%rsp,%rbx,1),%dl
add    %r11,%rdi
sar    $0x20,%rdi
cmp    %dl,0x10(%rsp,%rdi,1)
jne    134d <read_gp2y+0x17d>
shl    $0x8,%r12d
or     %ecx,%r12d
mov    %r12d,%eax
jmp    1455 <read_gp2y+0x285>
mov    0x2bb4(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xc0b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xfffffffb,%eax
jmp    1455 <read_gp2y+0x285>
lea    0xc57(%rip),%rdi
jmp    1423 <read_gp2y+0x253>
lea    0xc2b(%rip),%rdi
call   1030 <puts@plt>
jmp    1449 <read_gp2y+0x279>
mov    0x2b77(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xbec(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
%struct.timeval = type { i32, i64 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [30 x i8] c"failed to open UART port: %s\0A\00"
@errno = global i32 0
@B2400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [68 x i8] c"no response from dust sensor, please check your hardware settings.\0A\00"
@.str.3 = [36 x i8] c"Ooops, something weird happend ...\0A\00"
@.str.4 = [35 x i8] c"failed to read data from UART: %s\0A\00"
define i32 @read_gp2y() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.timeval
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %9
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -5, i32* %1
br label %175
20: ; preds = %0
%21 = load i32, i32* %6
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %2)
%23 = load i32, i32* @B2400
%24 = load i32, i32* @CS8
%25 = or i32 %23, %24
%26 = load i32, i32* @CLOCAL
%27 = or i32 %25, %26
%28 = load i32, i32* @CREAD
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @IGNPAR
%32 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 3
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 2
store i64 0, i64* %33
%34 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
store i64 0, i64* %34
%35 = load i32, i32* %6
%36 = load i32, i32* @TCIFLUSH
%37 = call i32 @tcflush(i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %38, i32 %39, %struct.termios* %2)
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %41
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 100000, i32* %42
%43 = call i32 @FD_ZERO(i32* %5)
%44 = load i32, i32* %6
%45 = call i32 @FD_SET(i32 %44, i32* %5)
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
%48 = call i32 @select(i32 %47, i32* %5, i32* null, i32* null, %struct.timeval* %3)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp sle i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %20
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
store i32 -1, i32* %1
br label %175
55: ; preds = %20
%56 = load i32, i32* %6
%57 = call i32 @FD_ISSET(i32 %56, i32* %5)
%58 = icmp ne i32 %57, 0
br i1 %58, label %63, label %59
59: ; preds = %55
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %1
br label %175
63: ; preds = %55
br label %64
64: ; preds = %63
br label %65
65: ; preds = %88, %64
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = icmp ult i64 %67, 32
br i1 %68, label %69, label %92
69: ; preds = %65
%70 = load i32, i32* %6
%71 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%72 = load i32, i32* %9
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = load i32, i32* %9
%76 = sext i32 %75 to i64
%77 = sub i64 32, %76
%78 = trunc i64 %77 to i32
%79 = call i32 @read(i32 %70, i8* %74, i32 %78)
store i32 %79, i32* %7
%80 = load i32, i32* %7
%81 = icmp sle i32 %80, 0
br i1 %81, label %82, label %88
82: ; preds = %69
%83 = load i32, i32* @errno
%84 = call i8* @strerror(i32 %83)
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %84)
%86 = load i32, i32* %6
%87 = call i32 @close(i32 %86)
store i32 -1, i32* %1
br label %175
88: ; preds = %69
%89 = load i32, i32* %7
%90 = load i32, i32* %9
%91 = add nsw i32 %90, %89
store i32 %91, i32* %9
br label %65
92: ; preds = %65
%93 = load i32, i32* %6
%94 = call i32 @close(i32 %93)
%95 = load i32, i32* %9
%96 = icmp slt i32 %95, 8
br i1 %96, label %97, label %98
97: ; preds = %92
store i32 -2, i32* %1
br label %175
98: ; preds = %92
store i32 0, i32* %8
br label %99
99: ; preds = %160, %124, %98
br label %100
100: ; preds = %114, %99
%101 = load i32, i32* %8
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %102
%104 = load i8, i8* %103
%105 = zext i8 %104 to i32
%106 = icmp ne i32 %105, 170
br i1 %106, label %107, label %115
107: ; preds = %100
%108 = load i32, i32* %8
%109 = add nsw i32 %108, 1
store i32 %109, i32* %8
%110 = load i32, i32* %9
%111 = sub nsw i32 %110, 7
%112 = icmp sgt i32 %109, %111
br i1 %112, label %113, label %114
113: ; preds = %107
store i32 -2, i32* %1
br label %175
114: ; preds = %107
br label %100
115: ; preds = %100
%116 = load i32, i32* %8
%117 = add nsw i32 %116, 1
store i32 %117, i32* %8
%118 = add nsw i32 %117, 5
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %119
%121 = load i8, i8* %120
%122 = zext i8 %121 to i32
%123 = icmp ne i32 %122, 255
br i1 %123, label %124, label %125
124: ; preds = %115
br label %99
125: ; preds = %115
%126 = load i32, i32* %8
%127 = sext i32 %126 to i64
%128 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %127
%129 = load i8, i8* %128
%130 = zext i8 %129 to i32
%131 = load i32, i32* %8
%132 = add nsw i32 %131, 1
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %133
%135 = load i8, i8* %134
%136 = zext i8 %135 to i32
%137 = add nsw i32 %130, %136
%138 = load i32, i32* %8
%139 = add nsw i32 %138, 2
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %140
%142 = load i8, i8* %141
%143 = zext i8 %142 to i32
%144 = add nsw i32 %137, %143
%145 = load i32, i32* %8
%146 = add nsw i32 %145, 3
%147 = sext i32 %146 to i64
%148 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %147
%149 = load i8, i8* %148
%150 = zext i8 %149 to i32
%151 = add nsw i32 %144, %150
%152 = and i32 %151, 255
%153 = load i32, i32* %8
%154 = add nsw i32 %153, 4
%155 = sext i32 %154 to i64
%156 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %155
%157 = load i8, i8* %156
%158 = zext i8 %157 to i32
%159 = icmp ne i32 %152, %158
br i1 %159, label %160, label %161
160: ; preds = %125
br label %99
161: ; preds = %125
%162 = load i32, i32* %8
%163 = sext i32 %162 to i64
%164 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %163
%165 = load i8, i8* %164
%166 = zext i8 %165 to i32
%167 = shl i32 %166, 8
%168 = load i32, i32* %8
%169 = add nsw i32 %168, 1
%170 = sext i32 %169 to i64
%171 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %170
%172 = load i8, i8* %171
%173 = zext i8 %172 to i32
%174 = or i32 %167, %173
store i32 %174, i32* %1
br label %175
175: ; preds = %161, %113, %97, %82, %59, %51, %16
%176 = load i32, i32* %1
ret i32 %176
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
%struct.timeval = type { i32, i64 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [30 x i8] c"failed to open UART port: %s\0A\00"
@errno = global i32 0
@B2400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.4 = [35 x i8] c"failed to read data from UART: %s\0A\00"
@str = [35 x i8] c"Ooops, something weird happend ...\00"
@str.5 = [67 x i8] c"no response from dust sensor, please check your hardware settings.\00"
define i32 @read_gp2y() {
%1 = alloca %struct.termios
%2 = alloca %struct.timeval
%3 = alloca [32 x i8]
%4 = alloca i32
%5 = bitcast %struct.termios* %1 to i8*
%6 = bitcast %struct.timeval* %2 to i8*
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %129
18: ; preds = %0
%19 = call i32 @tcgetattr(i32 %12, %struct.termios* %1)
%20 = load i32, i32* @B2400
%21 = load i32, i32* @CS8
%22 = or i32 %21, %20
%23 = load i32, i32* @CLOCAL
%24 = or i32 %22, %23
%25 = load i32, i32* @CREAD
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @IGNPAR
%29 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%31 = bitcast i64* %30 to i8*
%32 = load i32, i32* @TCIFLUSH
%33 = call i32 @tcflush(i32 %12, i32 %32)
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 %12, i32 %34, %struct.termios* %1)
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 100000, i32* %37
%38 = call i32 @FD_ZERO(i32* %4)
%39 = call i32 @FD_SET(i32 %12, i32* %4)
%40 = add nuw nsw i32 %12, 1
%41 = call i32 @select(i32 %40, i32* %4, i32* null, i32* null, %struct.timeval* %2)
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %18
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str.5, i64 0, i64 0))
%45 = call i32 @close(i32 %12)
br label %129
46: ; preds = %18
%47 = call i32 @FD_ISSET(i32 %12, i32* %4)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%51 = call i32 @close(i32 %12)
br label %129
52: ; preds = %54
%53 = icmp ult i32 %61, 32
br i1 %53, label %54, label %67
54: ; preds = %46, %52
%55 = phi i32 [ %61, %52 ], [ 0, %46 ]
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %56
%58 = sub nuw nsw i32 32, %55
%59 = call i32 @read(i32 %12, i8* %57, i32 %58)
%60 = icmp slt i32 %59, 1
%61 = add nuw nsw i32 %59, %55
br i1 %60, label %62, label %52
62: ; preds = %54
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = call i32 @close(i32 %12)
br label %129
67: ; preds = %52
%68 = call i32 @close(i32 %12)
%69 = icmp slt i32 %61, 8
br i1 %69, label %129, label %70
70: ; preds = %67
%71 = add nsw i32 %61, -7
br label %72
72: ; preds = %95, %70
%73 = phi i32 [ 0, %70 ], [ %88, %95 ]
%74 = sext i32 %73 to i64
%75 = call i32 @llvm.smax.i32(i32 %73, i32 %71)
%76 = add i32 %75, 1
br label %77
77: ; preds = %82, %72
%78 = phi i64 [ %83, %82 ], [ %74, %72 ]
%79 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %78
%80 = load i8, i8* %79
%81 = icmp eq i8 %80, -86
br i1 %81, label %86, label %82
82: ; preds = %77
%83 = add nsw i64 %78, 1
%84 = trunc i64 %83 to i32
%85 = icmp eq i32 %76, %84
br i1 %85, label %129, label %77
86: ; preds = %77
%87 = trunc i64 %78 to i32
%88 = add nsw i32 %87, 1
%89 = shl i64 %78, 32
%90 = add i64 %89, 25769803776
%91 = ashr exact i64 %90, 32
%92 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %91
%93 = load i8, i8* %92
%94 = icmp eq i8 %93, -1
br i1 %94, label %96, label %95
95: ; preds = %86, %96
br label %72
96: ; preds = %86
%97 = sext i32 %88 to i64
%98 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %97
%99 = load i8, i8* %98
%100 = shl i64 %78, 32
%101 = add i64 %100, 8589934592
%102 = ashr exact i64 %101, 32
%103 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %102
%104 = load i8, i8* %103
%105 = add i8 %104, %99
%106 = shl i64 %78, 32
%107 = add i64 %106, 12884901888
%108 = ashr exact i64 %107, 32
%109 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %108
%110 = load i8, i8* %109
%111 = add i8 %105, %110
%112 = shl i64 %78, 32
%113 = add i64 %112, 17179869184
%114 = ashr exact i64 %113, 32
%115 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %114
%116 = load i8, i8* %115
%117 = add i8 %111, %116
%118 = shl i64 %78, 32
%119 = add i64 %118, 21474836480
%120 = ashr exact i64 %119, 32
%121 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %120
%122 = load i8, i8* %121
%123 = icmp eq i8 %122, %117
br i1 %123, label %124, label %95
124: ; preds = %96
%125 = zext i8 %104 to i32
%126 = zext i8 %99 to i32
%127 = shl nuw nsw i32 %126, 8
%128 = or i32 %127, %125
br label %129
129: ; preds = %82, %67, %124, %62, %49, %43, %14
%130 = phi i32 [ -5, %14 ], [ -1, %43 ], [ -1, %62 ], [ %128, %124 ], [ -1, %49 ], [ -2, %67 ], [ -2, %82 ]
ret i32 %130
}