#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*/ exit(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ isatty(int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int open_com(const char *com_path) {
  int sfd = open(com_path, O_RDWR | O_NOCTTY | O_NDELAY);
  if (sfd < 0) {
    perror("open com_port error:");
    exit(-1);
  }
  if (fcntl(sfd, F_SETFL, 0) < 0) {
    perror("fcntl F_SETFL:");
    exit(-1);
  }
  if (0 == isatty(sfd)) {
    perror("it's not a com !!:");
    exit(-1);
  }
  return sfd;
}


x86 assembly (clang, -O0):
<open_com>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a3 <open_com+0x53>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    11d6 <open_com+0x86>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
call   1030 <isatty@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1202 <open_com+0xb2>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_com>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     119e <open_com+0x4e>
mov    %eax,%ebx
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %rax,%rax
js     11a7 <open_com+0x57>
mov    %ebx,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     11b0 <open_com+0x60>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe5b(%rip),%rdi
jmp    11b7 <open_com+0x67>
lea    0xe67(%rip),%rdi
jmp    11b7 <open_com+0x67>
lea    0xe6d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [21 x i8] c"open com_port error:\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl F_SETFL:\00"
@.str.2 = [19 x i8] c"it's not a com !!:\00"
define i32 @open_com(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %5, %6
%8 = load i32, i32* @O_NDELAY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %4, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load i32, i32* @F_SETFL
%19 = call i64 @fcntl(i32 %17, i32 %18, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %16
%25 = load i32, i32* %3
%26 = call i64 @isatty(i32 %25)
%27 = icmp eq i64 0, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %24
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [21 x i8] c"open com_port error:\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl F_SETFL:\00"
@.str.2 = [19 x i8] c"it's not a com !!:\00"
define i32 @open_com(i8* nocapture %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 slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
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 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %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 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %19
ret i32 %7
}