#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 termios {
  int c_cflag;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void main() {
  int uart0_filestream = -1;
  uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY);
  if (uart0_filestream == -1) {
    printf("Error - Unable to open UART.  Ensure it is not in use by another "
           "application\n");
  }
  struct termios options;
  tcgetattr(uart0_filestream, &options);
  options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(uart0_filestream, TCIFLUSH);
  tcsetattr(uart0_filestream, TCSANOW, &options);
  if (uart0_filestream != -1) {
    while (1) {
      unsigned char rx_buffer[256];
      int rx_length = read(uart0_filestream, (void *)rx_buffer, 255);
      int idx;
      if (rx_length < 0) {
        printf("Read Error \n");
      } else if (rx_length == 0) {
        printf("No Data Waiting \n");
      } else {
        rx_buffer[rx_length] = '\0';
        printf("%i bytes read : %s\n", rx_length, rx_buffer);
      }
    }
  }
  close(uart0_filestream);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0xffffffff,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11bb <main+0x4b>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    0x2df1(%rip),%rcx
or     (%rcx),%eax
mov    0x2db8(%rip),%rcx
or     (%rcx),%eax
mov    0x2de7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmpl   $0xffffffff,-0x4(%rbp)
je     12cd <main+0x15d>
jmp    123f <main+0xcf>
mov    -0x4(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jge    1279 <main+0x109>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <main+0x158>
cmpl   $0x0,-0x134(%rbp)
jne    1299 <main+0x129>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c3 <main+0x153>
movslq -0x134(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
mov    -0x134(%rbp),%esi
lea    -0x130(%rbp),%rdx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <main+0x158>
jmp    123f <main+0xcf>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    0x2e52(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe58(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11c0 <main+0x40>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <tcgetattr@plt>
mov    0x2e04(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2db2(%rip),%rax
or     (%rax),%ecx
mov    0x2de1(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1090 <tcsetattr@plt>
cmp    $0xffffffff,%ebp
je     129a <main+0x11a>
lea    0x20(%rsp),%rbx
lea    0xdf5(%rip),%r14
lea    0xddd(%rip),%r15
lea    0xdc2(%rip),%r12
jmp    1258 <main+0xd8>
nopl   (%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1250 <main+0xd0>
je     1290 <main+0x110>
mov    %eax,%ecx
movb   $0x0,0x20(%rsp,%rcx,1)
mov    %r12,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1258 <main+0xd8>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    1258 <main+0xd8>
mov    $0xffffffff,%edi
call   1060 <close@plt>
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [78 x i8] c"Error - Unable to open UART. Ensure it is not in use by another application\0A\00"
@B9600 = 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 = [13 x i8] c"Read Error \0A\00"
@.str.3 = [18 x i8] c"No Data Waiting \0A\00"
@.str.4 = [20 x i8] c"%i bytes read : %s\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 -1, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %1
%10 = load i32, i32* %1
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* %1
%16 = call i32 @tcgetattr(i32 %15, %struct.termios* %2)
%17 = load i32, i32* @B9600
%18 = load i32, i32* @CS8
%19 = or i32 %17, %18
%20 = load i32, i32* @CLOCAL
%21 = or i32 %19, %20
%22 = load i32, i32* @CREAD
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @IGNPAR
%26 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 3
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 2
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
store i64 0, i64* %28
%29 = load i32, i32* %1
%30 = load i32, i32* @TCIFLUSH
%31 = call i32 @tcflush(i32 %29, i32 %30)
%32 = load i32, i32* %1
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %2)
%35 = load i32, i32* %1
%36 = icmp ne i32 %35, -1
br i1 %36, label %37, label %60
37: ; preds = %14
br label %38
38: ; preds = %37, %59
%39 = load i32, i32* %1
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 255)
store i32 %41, i32* %4
%42 = load i32, i32* %4
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %59
46: ; preds = %38
%47 = load i32, i32* %4
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %58
51: ; preds = %46
%52 = load i32, i32* %4
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %4
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %55, i8* %56)
br label %58
58: ; preds = %51, %49
br label %59
59: ; preds = %58, %44
br label %38
60: ; preds = %14
%61 = load i32, i32* %1
%62 = call i32 @close(i32 %61)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B9600 = 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 = [20 x i8] c"%i bytes read : %s\0A\00"
@str = [17 x i8] c"No Data Waiting \00"
@str.5 = [12 x i8] c"Read Error \00"
@str.6 = [77 x i8] c"Error - Unable to open UART. Ensure it is not in use by another application\00"
define void @main() {
%1 = alloca %struct.termios
%2 = alloca [256 x i8]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([77 x i8], [77 x i8]* @str.6, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = bitcast %struct.termios* %1 to i8*
%12 = call i32 @tcgetattr(i32 %6, %struct.termios* %1)
%13 = load i32, i32* @B9600
%14 = load i32, i32* @CS8
%15 = or i32 %14, %13
%16 = load i32, i32* @CLOCAL
%17 = or i32 %15, %16
%18 = load i32, i32* @CREAD
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @IGNPAR
%22 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%24 = bitcast i64* %23 to i8*
%25 = load i32, i32* @TCIFLUSH
%26 = call i32 @tcflush(i32 %6, i32 %25)
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 %6, i32 %27, %struct.termios* %1)
br i1 %7, label %45, label %29
29: ; preds = %10
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %31
31: ; preds = %29, %44
%32 = call i32 @read(i32 %6, i8* %30, i32 255)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %44
36: ; preds = %31
%37 = icmp eq i32 %32, 0
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %44
40: ; preds = %36
%41 = zext i32 %32 to i64
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %41
store i8 0, i8* %42
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %32, i8* %30)
br label %44
44: ; preds = %38, %40, %34
br label %31
45: ; preds = %10
%46 = call i32 @close(i32 -1)
ret void
}