#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 O_NDELAY;
int O_NOCTTY;
int O_RDWR;
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ isatty(int);
int open(char *, int);
int /*<<< orphan*/ stderr;
int open_dev(char *dev) {
  int fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
  if (-1 == fd) {
    fprintf(stderr, "Can't Open Serial Port %s\n", dev);
    return -1;
  }
  if (fcntl(fd, F_SETFL, 0) < 0)
    fprintf(stderr, "fcntl failed!\n");
  if (isatty(fd) == 0)
    fprintf(stderr, "This is not a tty device!\n");
  return fd;
}


x86 assembly (clang, -O0):
<open_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    11a8 <open_dev+0x68>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <open_dev+0xcb>
mov    -0x14(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    11dc <open_dev+0x9c>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1205 <open_dev+0xc5>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_dev>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%rbx
mov    0x2e61(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11c0 <open_dev+0x80>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %rax,%rax
jns    119b <open_dev+0x5b>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <isatty@plt>
test   %rax,%rax
jne    11da <open_dev+0x9a>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11da <open_dev+0x9a>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Can't Open Serial Port %s\0A\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl failed!\0A\00"
@.str.2 = [27 x i8] c"This is not a tty device!\0A\00"
define i32 @open_dev(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 -1, %12
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %35
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* @F_SETFL
%21 = call i64 @fcntl(i32 %19, i32 %20, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %18
%27 = load i32, i32* %4
%28 = call i64 @isatty(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %30, %26
%34 = load i32, i32* %4
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %14
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Can't Open Serial Port %s\0A\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl failed!\0A\00"
@.str.2 = [27 x i8] c"This is not a tty device!\0A\00"
define i32 @open_dev(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = load i32, i32* @O_NDELAY
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
12: ; preds = %1
%13 = load i32, i32* @F_SETFL
%14 = tail call i64 @fcntl(i32 %7, i32 %13, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %16, %12
%20 = tail call i64 @isatty(i32 %7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %19, %22, %9
ret i32 %7
}