#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*/ F_SETFL;
int /*<<< orphan*/ LOG_ERR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int open_port(char dev) {
  int fd;
  if (dev == 2) {
    fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY);
    if (fd < 0) {
      syslog(LOG_ERR, "Can't open serial port\n");
      return -1;
    } else {
      fcntl(fd, F_SETFL, 0);
      return fd;
    }
  }
  if (dev == 3) {
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
    if (fd < 0) {
      syslog(LOG_ERR, "Can't open serial port\n");
      return -1;
    } else {
      fcntl(fd, F_SETFL, 0);
      return fd;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x2,%eax
jne    11b5 <open_port+0x85>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    0x2e7d(%rip),%rax
or     (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1197 <open_port+0x67>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <open_port+0x103>
mov    -0xc(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1233 <open_port+0x103>
movsbl -0x5(%rbp),%eax
cmp    $0x3,%eax
jne    122c <open_port+0xfc>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
or     (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    120e <open_port+0xde>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <open_port+0x103>
mov    -0xc(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1233 <open_port+0x103>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
cmp    $0x3,%dil
je     115f <open_port+0x2f>
cmp    $0x2,%dil
jne    11ae <open_port+0x7e>
mov    0x2e9d(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea3(%rip),%rdi
jmp    1178 <open_port+0x48>
mov    0x2e82(%rip),%rax
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1199 <open_port+0x69>
mov    %eax,%ebp
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %ebp,%ebx
jmp    11ae <open_port+0x7e>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1040 <syslog@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyUSB1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [24 x i8] c"Can't open serial port\0A\00"
@F_SETFL = global i32 0
@.str.2 = [13 x i8] c"/dev/ttyUSB0\00"
define i32 @open_port(i8 signext %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
store i8 %0, i8* %3
%5 = load i8, i8* %3
%6 = sext i8 %5 to i32
%7 = icmp eq i32 %6, 2
br i1 %7, label %8, label %23
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* @LOG_ERR
%17 = call i32 @syslog(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %43
18: ; preds = %8
%19 = load i32, i32* %4
%20 = load i32, i32* @F_SETFL
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
%22 = load i32, i32* %4
store i32 %22, i32* %2
br label %43
23: ; preds = %1
%24 = load i8, i8* %3
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 3
br i1 %26, label %27, label %42
27: ; preds = %23
%28 = load i32, i32* @O_RDWR
%29 = load i32, i32* @O_NOCTTY
%30 = or i32 %28, %29
%31 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* @LOG_ERR
%36 = call i32 @syslog(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %43
37: ; preds = %27
%38 = load i32, i32* %4
%39 = load i32, i32* @F_SETFL
%40 = call i32 @fcntl(i32 %38, i32 %39, i32 0)
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %43
42: ; preds = %23
store i32 -1, i32* %2
br label %43
43: ; preds = %42, %37, %34, %18, %15
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyUSB1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [24 x i8] c"Can't open serial port\0A\00"
@F_SETFL = global i32 0
@.str.2 = [13 x i8] c"/dev/ttyUSB0\00"
define i32 @open_port(i8 signext %0) {
switch i8 %0, label %26 [
i8 2, label %2
i8 3, label %14
]
2: ; preds = %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @LOG_ERR
%10 = tail call i32 @syslog(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %26
11: ; preds = %2
%12 = load i32, i32* @F_SETFL
%13 = tail call i32 @fcntl(i32 %6, i32 %12, i32 0)
br label %26
14: ; preds = %1
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_NOCTTY
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @LOG_ERR
%22 = tail call i32 @syslog(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %26
23: ; preds = %14
%24 = load i32, i32* @F_SETFL
%25 = tail call i32 @fcntl(i32 %18, i32 %24, i32 0)
br label %26
26: ; preds = %1, %23, %20, %11, %8
%27 = phi i32 [ -1, %8 ], [ %6, %11 ], [ -1, %20 ], [ %18, %23 ], [ -1, %1 ]
ret i32 %27
}