#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 const *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int ows_initserial(const char *pathname) {
  int uart0fs;
  uart0fs = open(pathname, O_RDWR | O_NOCTTY | O_NDELAY);
  if (uart0fs == -1) {
    printf("Error - Unable to open UART.  Ensure it is not in use by another "
           "application\n");
    return (uart0fs);
  }
  struct termios options;
  if ((tcgetattr(uart0fs, &options)) == -1) {
    perror("tcgetattr()");
    close(uart0fs);
    return (-1);
  }
  options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(uart0fs, TCIFLUSH);
  tcsetattr(uart0fs, TCSANOW, &options);
  return (uart0fs);
}


x86 assembly (clang, -O0):
<ows_initserial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c4 <ows_initserial+0x54>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <ows_initserial+0xf8>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    11f9 <ows_initserial+0x89>
lea    0xe6e(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <ows_initserial+0xf8>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    0x2dbf(%rip),%rcx
or     (%rcx),%eax
mov    0x2d7e(%rip),%rcx
or     (%rcx),%eax
mov    0x2db5(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ows_initserial>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e6c(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     120c <ows_initserial+0x9c>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     1225 <ows_initserial+0xb5>
mov    0x2e26(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2dcc(%rip),%rax
or     (%rax),%ecx
mov    0x2e03(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1070 <tcsetattr@plt>
jmp    121d <ows_initserial+0xad>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1218 <ows_initserial+0xa8>

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

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [12 x i8] c"tcgetattr()\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 = [77 x i8] c"Error - Unable to open UART. Ensure it is not in use by another application\00"
define i32 @ows_initserial(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([77 x i8], [77 x i8]* @str, i64 0, i64 0))
br label %38
12: ; preds = %1
%13 = bitcast %struct.termios* %2 to i8*
%14 = call i32 @tcgetattr(i32 %8, %struct.termios* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %8)
br label %36
19: ; preds = %12
%20 = load i32, i32* @B9600
%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* %2, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @IGNPAR
%29 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%31 = bitcast i64* %30 to i8*
%32 = load i32, i32* @TCIFLUSH
%33 = call i32 @tcflush(i32 %8, i32 %32)
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 %8, i32 %34, %struct.termios* %2)
br label %36
36: ; preds = %19, %16
%37 = phi i32 [ -1, %16 ], [ %8, %19 ]
br label %38
38: ; preds = %36, %10
%39 = phi i32 [ -1, %10 ], [ %37, %36 ]
ret i32 %39
}