#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 O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ gbser__db(int, char *, int /*<<< orphan*/);
int isatty(int);
int open(char const *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int gbser_is_serial(const char *port_name) {
  int fd;
  int is_port = 0;
  if (fd = open(port_name, O_RDWR | O_NOCTTY), fd == -1) {
    gbser__db(1, "Failed to open port (%s) to check its type\n",
              strerror(errno));
    return 0;
  }
  is_port = isatty(fd);
  close(fd);
  return is_port;
}


x86 assembly (clang, -O0):
<gbser_is_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b8 <gbser_is_serial+0x68>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe59(%rip),%rsi
call   1050 <gbser__db@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <gbser_is_serial+0x81>
mov    -0x14(%rbp),%edi
call   1030 <isatty@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gbser_is_serial>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1185 <gbser_is_serial+0x35>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <isatty@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
jmp    11a8 <gbser_is_serial+0x58>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe66(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1050 <gbser__db@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [44 x i8] c"Failed to open port (%s) to check its type\0A\00"
@errno = global i32 0
define i32 @gbser_is_serial(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @gbser__db(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 0, i32* %2
br label %23
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i32 @isatty(i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %5
store i32 %22, i32* %2
br label %23
23: ; preds = %17, %13
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [44 x i8] c"Failed to open port (%s) to check its type\0A\00"
@errno = global i32 0
define i32 @gbser_is_serial(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @gbser__db(i32 1, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %14
11: ; preds = %1
%12 = tail call i32 @isatty(i32 %5)
%13 = tail call i32 @close(i32 %5)
br label %14
14: ; preds = %11, %7
%15 = phi i32 [ 0, %7 ], [ %12, %11 ]
ret i32 %15
}