#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*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int UARTopen() {
  int uart_descriptor = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (uart_descriptor == -1) {
    printf("Error - Unable to open UART\n");
    exit(0);
  }
  struct termios options;
  tcgetattr(uart_descriptor, &options);
  options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(uart_descriptor, TCIFLUSH);
  tcsetattr(uart_descriptor, TCSANOW, &options);
  return uart_descriptor;
}


x86 assembly (clang, -O0):
<UARTopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
lea    0xe76(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11af <UARTopen+0x4f>
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbc(%rip),%rcx
or     (%rcx),%eax
mov    0x2df3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UARTopen>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e7a(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     120e <UARTopen+0xae>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e2d(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dd3(%rip),%rax
or     (%rax),%ecx
mov    0x2e0a(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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 = [29 x i8] c"Error - Unable to open UART\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
define i32 @UARTopen() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %3, %4
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %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
ret i32 %35
}

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 = [28 x i8] c"Error - Unable to open UART\00"
define i32 @UARTopen() {
%1 = alloca %struct.termios
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_NDELAY
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %0
%13 = bitcast %struct.termios* %1 to i8*
%14 = call i32 @tcgetattr(i32 %7, %struct.termios* %1)
%15 = load i32, i32* @B9600
%16 = load i32, i32* @CS8
%17 = or i32 %16, %15
%18 = load i32, i32* @CLOCAL
%19 = or i32 %17, %18
%20 = load i32, i32* @CREAD
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @IGNPAR
%24 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%26 = bitcast i64* %25 to i8*
%27 = load i32, i32* @TCIFLUSH
%28 = call i32 @tcflush(i32 %7, i32 %27)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %7, i32 %29, %struct.termios* %1)
ret i32 %7
}