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

/* Variables and functions */
int /*<<< orphan*/ B19200;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ O_NOCTTY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ isatty(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_port(char dev) {
  int fd;
  if (dev == 1)
    fd = open("/dev/ttyUSB0", O_RDWR, O_NOCTTY);
  else
    fd = open("/dev/ttyUSB1", O_RDWR, O_NOCTTY);
  if (fd < 0) {
    syslog(LOG_ERR, "Can't open serial port");
    return -1;
  }
  struct termios port_cfg;
  if (tcgetattr(fd, &port_cfg) < 0) {
    syslog(LOG_ERR, "Error getting serial config");
    return -1;
  }
  cfmakeraw(&port_cfg);
  port_cfg.c_cc[VMIN] = 0;
  port_cfg.c_cc[VTIME] = 1;
  cfsetospeed(&port_cfg, B19200);
  cfsetispeed(&port_cfg, B19200);
  if (tcsetattr(fd, TCSAFLUSH, &port_cfg) < 0) {
    syslog(LOG_ERR, "Error entering Daemon. Port setattr");
    return -1;
  }
  if (!isatty(fd)) {
    syslog(LOG_ERR, "Not a tty port");
    return -1;
  }
  return fd;
  return -1;
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x1,%eax
jne    11c1 <open_port+0x41>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    0xe4c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
jmp    11e2 <open_port+0x62>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    0xe33(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    120d <open_port+0x8d>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1050 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <open_port+0x193>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1244 <open_port+0xc4>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1050 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <open_port+0x193>
lea    -0x18(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    -0x18(%rbp),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2d4b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   10a0 <cfsetispeed@plt>
mov    -0xc(%rbp),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
jge    12db <open_port+0x15b>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
call   1050 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <open_port+0x193>
mov    -0xc(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    130d <open_port+0x18d>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
call   1050 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <open_port+0x193>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%dil
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    0xe5a(%rip),%rax
lea    0xe60(%rip),%rdi
cmove  %rax,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1252 <open_port+0xd2>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <tcgetattr@plt>
test   %rax,%rax
js     1264 <open_port+0xe4>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    0x8(%rsp),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2db9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2de0(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1040 <cfsetospeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   10a0 <cfsetispeed@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
js     126d <open_port+0xed>
mov    %ebx,%edi
call   1030 <isatty@plt>
test   %eax,%eax
jne    1287 <open_port+0x107>
lea    0xe21(%rip),%rsi
jmp    1274 <open_port+0xf4>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
jmp    127d <open_port+0xfd>
lea    0xdc6(%rip),%rsi
jmp    1274 <open_port+0xf4>
lea    0xdd9(%rip),%rsi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1050 <syslog@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32* }
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [13 x i8] c"/dev/ttyUSB1\00"
@LOG_ERR = global i32 0
@.str.2 = [23 x i8] c"Can't open serial port\00"
@.str.3 = [28 x i8] c"Error getting serial config\00"
@VMIN = global i64 0
@VTIME = global i64 0
@B19200 = global i32 0
@TCSAFLUSH = global i32 0
@.str.4 = [36 x i8] c"Error entering Daemon. Port setattr\00"
@.str.5 = [15 x i8] c"Not a tty port\00"
define i32 @open_port(i8 signext %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 = sext i8 %6 to i32
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
store i32 %12, i32* %4
br label %17
13: ; preds = %1
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_NOCTTY
%16 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15)
store i32 %16, i32* %4
br label %17
17: ; preds = %13, %9
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @LOG_ERR
%22 = call i32 @syslog(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
23: ; preds = %17
%24 = load i32, i32* %4
%25 = call i64 @tcgetattr(i32 %24, %struct.termios* %5)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @LOG_ERR
%29 = call i32 @syslog(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
30: ; preds = %23
%31 = call i32 @cfmakeraw(%struct.termios* %5)
%32 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%33 = load i32*, i32** %32
%34 = load i64, i64* @VMIN
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 0, i32* %35
%36 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%37 = load i32*, i32** %36
%38 = load i64, i64* @VTIME
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 1, i32* %39
%40 = load i32, i32* @B19200
%41 = call i32 @cfsetospeed(%struct.termios* %5, i32 %40)
%42 = load i32, i32* @B19200
%43 = call i32 @cfsetispeed(%struct.termios* %5, i32 %42)
%44 = load i32, i32* %4
%45 = load i32, i32* @TCSAFLUSH
%46 = call i64 @tcsetattr(i32 %44, i32 %45, %struct.termios* %5)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %30
%49 = load i32, i32* @LOG_ERR
%50 = call i32 @syslog(i32 %49, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
51: ; preds = %30
%52 = load i32, i32* %4
%53 = call i32 @isatty(i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %51
%56 = load i32, i32* @LOG_ERR
%57 = call i32 @syslog(i32 %56, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
58: ; preds = %51
%59 = load i32, i32* %4
store i32 %59, i32* %2
br label %60
60: ; preds = %58, %55, %48, %27, %20
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.termios = type { i32* }
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [13 x i8] c"/dev/ttyUSB1\00"
@LOG_ERR = global i32 0
@.str.2 = [23 x i8] c"Can't open serial port\00"
@.str.3 = [28 x i8] c"Error getting serial config\00"
@VMIN = global i64 0
@VTIME = global i64 0
@B19200 = global i32 0
@TCSAFLUSH = global i32 0
@.str.4 = [36 x i8] c"Error entering Daemon. Port setattr\00"
@.str.5 = [15 x i8] c"Not a tty port\00"
define i32 @open_port(i8 signext %0) {
%2 = alloca %struct.termios
%3 = icmp eq i8 %0, 1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = select i1 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0)
%7 = tail call i32 @open(i8* %6, i32 %4, i32 %5)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @LOG_ERR
%11 = tail call i32 @syslog(i32 %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %40
12: ; preds = %1
%13 = bitcast %struct.termios* %2 to i8*
%14 = call i64 @tcgetattr(i32 %7, %struct.termios* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %34, label %16
16: ; preds = %12
%17 = call i32 @cfmakeraw(%struct.termios* %2)
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%19 = load i32*, i32** %18
%20 = load i64, i64* @VMIN
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 0, i32* %21
%22 = load i64, i64* @VTIME
%23 = getelementptr inbounds i32, i32* %19, i64 %22
store i32 1, i32* %23
%24 = load i32, i32* @B19200
%25 = call i32 @cfsetospeed(%struct.termios* %2, i32 %24)
%26 = load i32, i32* @B19200
%27 = call i32 @cfsetispeed(%struct.termios* %2, i32 %26)
%28 = load i32, i32* @TCSAFLUSH
%29 = call i64 @tcsetattr(i32 %7, i32 %28, %struct.termios* %2)
%30 = icmp slt i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %16
%32 = call i32 @isatty(i32 %7)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31, %16, %12
%35 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), %16 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), %31 ]
%36 = load i32, i32* @LOG_ERR
%37 = call i32 @syslog(i32 %36, i8* %35)
br label %38
38: ; preds = %34, %31
%39 = phi i32 [ %7, %31 ], [ -1, %34 ]
br label %40
40: ; preds = %38, %9
%41 = phi i32 [ -1, %9 ], [ %39, %38 ]
ret i32 %41
}