#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;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ check_baudrate(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ msg_Dbg(char *, int);
int /*<<< orphan*/ msg_Err(char *);
int /*<<< orphan*/ msg_Info(char *);
int open(char const *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_port(const char *pPort, int pBaud) {
  int fd = open(pPort, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    msg_Err("Unable to open serial-port");
    msg_Info("Retrying in 5 seconds...");
    sleep(5);
    fd = open(pPort, O_RDWR | O_NOCTTY | O_NDELAY);
  }
  if (fd == -1) {
    die("Unable to open serial-port");
  } else {
    fcntl(fd, F_SETFL, 0);
    struct termios options;
    tcgetattr(fd, &options);
    check_baudrate(pBaud);
    cfsetispeed(&options, pBaud);
    cfsetospeed(&options, pBaud);
    options.c_cflag |= (CLOCAL | CREAD);
    tcsetattr(fd, TCSANOW, &options);
    msg_Dbg("BAUDRATE SET TO %i", pBaud);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1249 <open_port+0x89>
lea    0xdf9(%rip),%rdi
call   1060 <msg_Err@plt>
lea    0xe08(%rip),%rdi
call   1070 <msg_Info@plt>
mov    $0x5,%edi
call   10e0 <sleep@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2d92(%rip),%rax
or     (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1264 <open_port+0xa4>
lea    0xda6(%rip),%rdi
call   10b0 <die@plt>
jmp    12df <open_port+0x11f>
mov    -0x10(%rbp),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0xc(%rbp),%edi
call   1030 <check_baudrate@plt>
mov    -0xc(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetospeed@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    0x2d25(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0xc(%rbp),%esi
lea    0xd5a(%rip),%rdi
call   10d0 <msg_Dbg@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e16(%rip),%r15
mov    0x2def(%rip),%r12
mov    (%r12),%eax
or     (%r15),%eax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2dcb(%rip),%r13
or     0x0(%r13),%eax
mov    %eax,%esi
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    123f <open_port+0x7f>
lea    0xdfa(%rip),%rdi
call   1060 <msg_Err@plt>
lea    0xe09(%rip),%rdi
call   1070 <msg_Info@plt>
mov    $0x5,%edi
call   10e0 <sleep@plt>
mov    (%r12),%esi
or     (%r15),%esi
or     0x0(%r13),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12c4 <open_port+0x104>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <tcgetattr@plt>
mov    %r14d,%edi
call   1030 <check_baudrate@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   10c0 <cfsetispeed@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <cfsetospeed@plt>
mov    0x2d25(%rip),%rax
mov    0x2d4e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,(%rsp)
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1090 <tcsetattr@plt>
lea    0xd89(%rip),%rdi
mov    %r14d,%esi
call   10d0 <msg_Dbg@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd35(%rip),%rdi
call   10b0 <die@plt>
mov    $0xffffffff,%ebp
jmp    12b3 <open_port+0xf3>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [27 x i8] c"Unable to open serial-port\00"
@.str.1 = [25 x i8] c"Retrying in 5 seconds...\00"
@F_SETFL = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@.str.2 = [19 x i8] c"BAUDRATE SET TO %i\00"
define i32 @open_port(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_NDELAY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %7, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %27
16: ; preds = %2
%17 = call i32 @msg_Err(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @msg_Info(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @sleep(i32 5)
%20 = load i8*, i8** %3
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NOCTTY
%23 = or i32 %21, %22
%24 = load i32, i32* @O_NDELAY
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %20, i32 %25)
store i32 %26, i32* %5
br label %27
27: ; preds = %16, %2
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %55
32: ; preds = %27
%33 = load i32, i32* %5
%34 = load i32, i32* @F_SETFL
%35 = call i32 @fcntl(i32 %33, i32 %34, i32 0)
%36 = load i32, i32* %5
%37 = call i32 @tcgetattr(i32 %36, %struct.termios* %6)
%38 = load i32, i32* %4
%39 = call i32 @check_baudrate(i32 %38)
%40 = load i32, i32* %4
%41 = call i32 @cfsetispeed(%struct.termios* %6, i32 %40)
%42 = load i32, i32* %4
%43 = call i32 @cfsetospeed(%struct.termios* %6, i32 %42)
%44 = load i32, i32* @CLOCAL
%45 = load i32, i32* @CREAD
%46 = or i32 %44, %45
%47 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%48 = load i32, i32* %47
%49 = or i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* %5
%51 = load i32, i32* @TCSANOW
%52 = call i32 @tcsetattr(i32 %50, i32 %51, %struct.termios* %6)
%53 = load i32, i32* %4
%54 = call i32 @msg_Dbg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %32, %30
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [27 x i8] c"Unable to open serial-port\00"
@.str.1 = [25 x i8] c"Retrying in 5 seconds...\00"
@F_SETFL = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@.str.2 = [19 x i8] c"BAUDRATE SET TO %i\00"
define i32 @open_port(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NDELAY
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = tail call i32 @msg_Err(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @msg_Info(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 5)
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_NOCTTY
%17 = or i32 %16, %15
%18 = load i32, i32* @O_NDELAY
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %0, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = tail call i32 @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %42
24: ; preds = %2, %11
%25 = phi i32 [ %20, %11 ], [ %9, %2 ]
%26 = load i32, i32* @F_SETFL
%27 = tail call i32 @fcntl(i32 %25, i32 %26, i32 0)
%28 = bitcast %struct.termios* %3 to i8*
%29 = call i32 @tcgetattr(i32 %25, %struct.termios* %3)
%30 = call i32 @check_baudrate(i32 %1)
%31 = call i32 @cfsetispeed(%struct.termios* %3, i32 %1)
%32 = call i32 @cfsetospeed(%struct.termios* %3, i32 %1)
%33 = load i32, i32* @CLOCAL
%34 = load i32, i32* @CREAD
%35 = or i32 %34, %33
%36 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%37 = load i32, i32* %36
%38 = or i32 %35, %37
store i32 %38, i32* %36
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %25, i32 %39, %struct.termios* %3)
%41 = call i32 @msg_Dbg(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %42
42: ; preds = %24, %22
%43 = phi i32 [ %25, %24 ], [ -1, %22 ]
ret i32 %43
}