#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_NDELAY;
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 *);
int write(int, unsigned char *, int);
int main(int argc, char **argv) {
  int fd;
  fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    printf("Error - Unable to open UART.  Ensure it is not in use by another "
           "application\n");
  }
  struct termios options;
  tcgetattr(fd, &options);
  options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &options);
  unsigned char tx_buffer[20];
  unsigned char *p_tx_buffer;
  p_tx_buffer = &tx_buffer[0];
  *p_tx_buffer++ = 'H';
  *p_tx_buffer++ = 'e';
  *p_tx_buffer++ = 'l';
  *p_tx_buffer++ = 'l';
  *p_tx_buffer++ = 'o';
  if (fd != -1) {
    int count = write(fd, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0]));
    if (count < 0) {
      printf("UART TX error\n");
    }
  }
  if (fd != -1) {
    unsigned char rx_buffer[256];
    int rx_length = read(fd, (void *)rx_buffer, 255);
    if (rx_length < 0) {
    } else if (rx_length == 0) {
    } else {
      rx_buffer[rx_length] = '\0';
      printf("%i bytes read : %s\n", rx_length, rx_buffer);
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
lea    0xe45(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11db <main+0x5b>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    0x2dd1(%rip),%rcx
or     (%rcx),%eax
mov    0x2d90(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1090 <tcsetattr@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x58(%rbp)
movb   $0x48,(%rax)
mov    -0x58(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x58(%rbp)
movb   $0x65,(%rax)
mov    -0x58(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x58(%rbp)
movb   $0x6c,(%rax)
mov    -0x58(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x58(%rbp)
movb   $0x6c,(%rax)
mov    -0x58(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x58(%rbp)
movb   $0x6f,(%rax)
cmpl   $0xffffffff,-0x14(%rbp)
je     12f5 <main+0x175>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rax
lea    -0x50(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    12f0 <main+0x170>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f5 <main+0x175>
cmpl   $0xffffffff,-0x14(%rbp)
je     1371 <main+0x1f1>
mov    -0x14(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x164(%rbp)
cmpl   $0x0,-0x164(%rbp)
jge    132b <main+0x1ab>
jmp    136c <main+0x1ec>
cmpl   $0x0,-0x164(%rbp)
jne    133d <main+0x1bd>
jmp    1367 <main+0x1e7>
movslq -0x164(%rbp),%rax
movb   $0x0,-0x160(%rbp,%rax,1)
mov    -0x164(%rbp),%esi
lea    -0x160(%rbp),%rdx
lea    0xd0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    136c <main+0x1ec>
jmp    1371 <main+0x1f1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x148,%rsp
mov    0x2e47(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
lea    0xe44(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d4 <main+0x44>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <tcgetattr@plt>
mov    0x2dee(%rip),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d94(%rip),%rax
or     (%rax),%ecx
mov    0x2dcb(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x20(%rsp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <tcflush@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
movl   $0x6c6c6548,(%rsp)
movb   $0x6f,0x4(%rsp)
cmp    $0xffffffff,%ebx
je     12a4 <main+0x114>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    1273 <main+0xe3>
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
lea    0x40(%rsp),%rsi
mov    %ebx,%edi
mov    $0xff,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12a4 <main+0x114>
mov    %eax,%ecx
movb   $0x0,0x40(%rsp,%rcx,1)
lea    0xd77(%rip),%rdi
lea    0x40(%rsp),%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r14
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
@O_NDELAY = 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 = [15 x i8] c"UART TX error\0A\00"
@.str.3 = [20 x i8] c"%i bytes read : %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca [20 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_NDELAY
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = load i32, i32* %6
%25 = call i32 @tcgetattr(i32 %24, %struct.termios* %7)
%26 = load i32, i32* @B9600
%27 = load i32, i32* @CS8
%28 = or i32 %26, %27
%29 = load i32, i32* @CLOCAL
%30 = or i32 %28, %29
%31 = load i32, i32* @CREAD
%32 = or i32 %30, %31
%33 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* @IGNPAR
%35 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
store i64 0, i64* %36
%37 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
store i64 0, i64* %37
%38 = load i32, i32* %6
%39 = load i32, i32* @TCIFLUSH
%40 = call i32 @tcflush(i32 %38, i32 %39)
%41 = load i32, i32* %6
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %7)
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
store i8* %44, i8** %9
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %9
store i8 72, i8* %45
%47 = load i8*, i8** %9
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %9
store i8 101, i8* %47
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %9
store i8 108, i8* %49
%51 = load i8*, i8** %9
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %9
store i8 108, i8* %51
%53 = load i8*, i8** %9
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %9
store i8 111, i8* %53
%55 = load i32, i32* %6
%56 = icmp ne i32 %55, -1
br i1 %56, label %57, label %72
57: ; preds = %23
%58 = load i32, i32* %6
%59 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%60 = load i8*, i8** %9
%61 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = trunc i64 %64 to i32
%66 = call i32 @write(i32 %58, i8* %59, i32 %65)
store i32 %66, i32* %10
%67 = load i32, i32* %10
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %57
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %71
71: ; preds = %69, %57
br label %72
72: ; preds = %71, %23
%73 = load i32, i32* %6
%74 = icmp ne i32 %73, -1
br i1 %74, label %75, label %95
75: ; preds = %72
%76 = load i32, i32* %6
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%78 = call i32 @read(i32 %76, i8* %77, i32 255)
store i32 %78, i32* %12
%79 = load i32, i32* %12
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %75
br label %94
82: ; preds = %75
%83 = load i32, i32* %12
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %82
br label %93
86: ; preds = %82
%87 = load i32, i32* %12
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %88
store i8 0, i8* %89
%90 = load i32, i32* %12
%91 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %90, i8* %91)
br label %93
93: ; preds = %86, %85
br label %94
94: ; preds = %93, %81
br label %95
95: ; preds = %94, %72
%96 = load i32, i32* %6
%97 = call i32 @close(i32 %96)
ret i32 0
}

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