#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 */

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ B9600;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int getUARTReg(int);
int open(char *, int);
int /*<<< orphan*/ output(int, char *, ...);
int /*<<< orphan*/ portSettings(int, int /*<<< orphan*/);
int /*<<< orphan*/ setUARTReg(int, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int openPort() {
  int portFD;
  portFD = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (portFD == -1) {
    output(1, "No port descriptor, giving up\n");
    return -1;
  }
  fcntl(portFD, F_SETFL, 0);
  output(1, "Setting port to 9600, LCR=");
  portSettings(portFD, B9600);
  tcflush(portFD, TCIOFLUSH);
  output(1, "%02x\n", getUARTReg(3));
  output(1, "Setting port to 115200, LCR=");
  portSettings(portFD, B115200);
  tcflush(portFD, TCIOFLUSH);
  output(1, "%02x\n", getUARTReg(3));
  setUARTReg(4, getUARTReg(4) | 0x00000022);
  return portFD;
}


x86 assembly (clang, -O0):
<openPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
lea    0xe66(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cb <openPort+0x5b>
mov    $0x1,%edi
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <openPort+0x135>
mov    -0x8(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    $0x1,%edi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x8(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1070 <portSettings@plt>
mov    -0x8(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1060 <tcflush@plt>
mov    $0x3,%edi
call   1090 <getUARTReg@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    $0x1,%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    -0x8(%rbp),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1070 <portSettings@plt>
mov    -0x8(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
call   1060 <tcflush@plt>
mov    $0x3,%edi
call   1090 <getUARTReg@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1050 <output@plt>
mov    $0x4,%edi
call   1090 <getUARTReg@plt>
mov    %eax,%esi
or     $0x22,%esi
mov    $0x4,%edi
call   1030 <setUARTReg@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openPort>:
push   %r15
push   %r14
push   %rbx
mov    0x2e6c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
lea    0xe69(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1272 <openPort+0x102>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
lea    0xe6a(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <output@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <portSettings@plt>
mov    0x2dd5(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
call   1060 <tcflush@plt>
mov    $0x3,%edi
call   1090 <getUARTReg@plt>
lea    0xe47(%rip),%r14
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <output@plt>
lea    0xe35(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <output@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <portSettings@plt>
mov    (%r15),%esi
mov    %ebx,%edi
call   1060 <tcflush@plt>
mov    $0x3,%edi
call   1090 <getUARTReg@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <output@plt>
mov    $0x4,%edi
call   1090 <getUARTReg@plt>
or     $0x22,%eax
mov    $0x4,%edi
mov    %eax,%esi
call   1030 <setUARTReg@plt>
jmp    1285 <openPort+0x115>
lea    0xd92(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <output@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [31 x i8] c"No port descriptor, giving up\0A\00"
@F_SETFL = global i32 0
@.str.2 = [27 x i8] c"Setting port to 9600, LCR=\00"
@B9600 = global i32 0
@TCIOFLUSH = global i32 0
@.str.3 = [6 x i8] c"%02x\0A\00"
@.str.4 = [29 x i8] c"Setting port to 115200, LCR=\00"
@B115200 = global i32 0
define i32 @openPort() {
%1 = alloca i32
%2 = alloca i32
%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 ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %39
13: ; preds = %0
%14 = load i32, i32* %2
%15 = load i32, i32* @F_SETFL
%16 = call i32 @fcntl(i32 %14, i32 %15, i32 0)
%17 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = load i32, i32* @B9600
%20 = call i32 @portSettings(i32 %18, i32 %19)
%21 = load i32, i32* %2
%22 = load i32, i32* @TCIOFLUSH
%23 = call i32 @tcflush(i32 %21, i32 %22)
%24 = call i32 @getUARTReg(i32 3)
%25 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* %2
%28 = load i32, i32* @B115200
%29 = call i32 @portSettings(i32 %27, i32 %28)
%30 = load i32, i32* %2
%31 = load i32, i32* @TCIOFLUSH
%32 = call i32 @tcflush(i32 %30, i32 %31)
%33 = call i32 @getUARTReg(i32 3)
%34 = call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = call i32 @getUARTReg(i32 4)
%36 = or i32 %35, 34
%37 = call i32 @setUARTReg(i32 4, i32 %36)
%38 = load i32, i32* %2
store i32 %38, i32* %1
br label %39
39: ; preds = %13, %11
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [31 x i8] c"No port descriptor, giving up\0A\00"
@F_SETFL = global i32 0
@.str.2 = [27 x i8] c"Setting port to 9600, LCR=\00"
@B9600 = global i32 0
@TCIOFLUSH = global i32 0
@.str.3 = [6 x i8] c"%02x\0A\00"
@.str.4 = [29 x i8] c"Setting port to 115200, LCR=\00"
@B115200 = global i32 0
define i32 @openPort() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_NOCTTY
%3 = or i32 %2, %1
%4 = load i32, i32* @O_NDELAY
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 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 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %30
10: ; preds = %0
%11 = load i32, i32* @F_SETFL
%12 = tail call i32 @fcntl(i32 %6, i32 %11, i32 0)
%13 = tail call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @B9600
%15 = tail call i32 @portSettings(i32 %6, i32 %14)
%16 = load i32, i32* @TCIOFLUSH
%17 = tail call i32 @tcflush(i32 %6, i32 %16)
%18 = tail call i32 @getUARTReg(i32 3)
%19 = tail call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = tail call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32, i32* @B115200
%22 = tail call i32 @portSettings(i32 %6, i32 %21)
%23 = load i32, i32* @TCIOFLUSH
%24 = tail call i32 @tcflush(i32 %6, i32 %23)
%25 = tail call i32 @getUARTReg(i32 3)
%26 = tail call i32 (i32, i8*, ...) @output(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = tail call i32 @getUARTReg(i32 4)
%28 = or i32 %27, 34
%29 = tail call i32 @setUARTReg(i32 4, i32 %28)
br label %30
30: ; preds = %10, %8
ret i32 %6
}