#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;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ debug(char *, ...);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ isatty(int /*<<< orphan*/);
int open(char *, int);
int opencom(char *dev) {
  int fd = 0;
  fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    debug("Can't open serial port %s\n", dev);
    return fd;
  }
  if (fcntl(fd, F_SETFL, 0) < 0) {
    debug("fcntl failed! port: %s\n", dev);
  } else {
    debug("fcntl = %d, port: %s\n", fcntl(fd, F_SETFL, 0), dev);
  }
  if (isatty(STDIN_FILENO) == 0) {
    debug("standard input is not a terminal device\n");
  } else {
    debug("isatty success!\n");
  }
  debug("open serial port %s successfully, fd: %d\n", dev, fd);
  return fd;
}


x86 assembly (clang, -O0):
<opencom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a1 <opencom+0x61>
mov    -0x10(%rbp),%rsi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <debug@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124f <opencom+0x10f>
mov    -0x14(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    11d4 <opencom+0x94>
mov    -0x10(%rbp),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1050 <debug@plt>
jmp    11fb <opencom+0xbb>
mov    -0x14(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <debug@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1226 <opencom+0xe6>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <debug@plt>
jmp    1234 <opencom+0xf4>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <debug@plt>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <debug@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opencom>:
push   %r15
push   %r14
push   %rbx
mov    0x2e9c(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a7 <opencom+0x67>
mov    0x2e5a(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     11ba <opencom+0x7a>
mov    (%r15),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
lea    0xe9a(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <debug@plt>
jmp    11cb <opencom+0x8b>
lea    0xe52(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <debug@plt>
jmp    1208 <opencom+0xc8>
lea    0xe5a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <debug@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
test   %rax,%rax
lea    0xe66(%rip),%rax
lea    0xe88(%rip),%rdi
cmove  %rax,%rdi
xor    %eax,%eax
call   1050 <debug@plt>
lea    0xe87(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <debug@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [27 x i8] c"Can't open serial port %s\0A\00"
@F_SETFL = global i32 0
@.str.1 = [24 x i8] c"fcntl failed! port: %s\0A\00"
@.str.2 = [22 x i8] c"fcntl = %d, port: %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.3 = [41 x i8] c"standard input is not a terminal device\0A\00"
@.str.4 = [17 x i8] c"isatty success!\0A\00"
@.str.5 = [42 x i8] c"open serial port %s successfully, fd: %d\0A\00"
define i32 @opencom(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%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 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* %4
store i32 %17, i32* %2
br label %45
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* @F_SETFL
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %32
26: ; preds = %18
%27 = load i32, i32* %4
%28 = load i32, i32* @F_SETFL
%29 = call i32 @fcntl(i32 %27, i32 %28, i32 0)
%30 = load i8*, i8** %3
%31 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %29, i8* %30)
br label %32
32: ; preds = %26, %23
%33 = load i32, i32* @STDIN_FILENO
%34 = call i64 @isatty(i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %40
38: ; preds = %32
%39 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %36
%41 = load i8*, i8** %3
%42 = load i32, i32* %4
%43 = call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %41, i32 %42)
%44 = load i32, i32* %4
store i32 %44, i32* %2
br label %45
45: ; preds = %40, %14
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [27 x i8] c"Can't open serial port %s\0A\00"
@F_SETFL = global i32 0
@.str.1 = [24 x i8] c"fcntl failed! port: %s\0A\00"
@.str.2 = [22 x i8] c"fcntl = %d, port: %s\0A\00"
@STDIN_FILENO = global i32 0
@.str.3 = [41 x i8] c"standard input is not a terminal device\0A\00"
@.str.4 = [17 x i8] c"isatty success!\0A\00"
@.str.5 = [42 x i8] c"open serial port %s successfully, fd: %d\0A\00"
define i32 @opencom(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 %11
9: ; preds = %1
%10 = tail call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %28
11: ; preds = %1
%12 = load i32, i32* @F_SETFL
%13 = tail call i32 @fcntl(i32 %7, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %21
17: ; preds = %11
%18 = load i32, i32* @F_SETFL
%19 = tail call i32 @fcntl(i32 %7, i32 %18, i32 0)
%20 = tail call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %0)
br label %21
21: ; preds = %17, %15
%22 = load i32, i32* @STDIN_FILENO
%23 = tail call i64 @isatty(i32 %22)
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0)
%26 = tail call i32 (i8*, ...) @debug(i8* %25)
%27 = tail call i32 (i8*, ...) @debug(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %7)
br label %28
28: ; preds = %21, %9
ret i32 %7
}