#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*/ O_RDWR;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ isatty(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int Open_Port(char *comport) {
  char port_tmp[20];
  sprintf(port_tmp, comport, comport);
  int fd = 0;
  fd = open(port_tmp, O_RDWR);
  if (fd == -1) {
    perror("Can not open serial port");
    return -1;
  } else {
  }
  if (fcntl(fd, F_SETFL, 0) < 0) {
    printf("fcntl failed!\n");
  } else {
  }
  if (isatty(STDIN_FILENO) == 0) {
    printf("standard input is not a termined device\n");
  } else {
  }
  return fd;
}


x86 assembly (clang, -O0):
<Open_Port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1080 <sprintf@plt>
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11bb <Open_Port+0x5b>
lea    0xe56(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <Open_Port+0xc7>
jmp    11c0 <Open_Port+0x60>
mov    -0x34(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    11ee <Open_Port+0x8e>
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
jmp    11f3 <Open_Port+0x93>
jmp    11f3 <Open_Port+0x93>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    121c <Open_Port+0xbc>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
jmp    1221 <Open_Port+0xc1>
jmp    1221 <Open_Port+0xc1>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Open_Port>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1080 <sprintf@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11d8 <Open_Port+0x78>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %rax,%rax
jns    11b1 <Open_Port+0x51>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
jne    11d0 <Open_Port+0x70>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
jmp    11d0 <Open_Port+0x70>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [25 x i8] c"Can not open serial port\00"
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl failed!\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [41 x i8] c"standard input is not a termined device\0A\00"
define i32 @Open_Port(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [20 x i8]
%5 = alloca i32
store i8* %0, i8** %3
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%7 = load i8*, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @sprintf(i8* %6, i8* %7, i8* %8)
store i32 0, i32* %5
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
17: ; preds = %1
br label %18
18: ; preds = %17
%19 = load i32, i32* %5
%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 %25
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
25: ; preds = %18
br label %26
26: ; preds = %25, %23
%27 = load i32, i32* @STDIN_FILENO
%28 = call i64 @isatty(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %33
32: ; preds = %26
br label %33
33: ; preds = %32, %30
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %15
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [25 x i8] c"Can not open serial port\00"
@F_SETFL = global i32 0
@STDIN_FILENO = global i32 0
@str = [40 x i8] c"standard input is not a termined device\00"
@str.3 = [14 x i8] c"fcntl failed!\00"
define i32 @Open_Port(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) %0, i8* %0)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %22
10: ; preds = %1
%11 = load i32, i32* @F_SETFL
%12 = tail call i64 @fcntl(i32 %6, i32 %11, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.3, i64 0, i64 0))
br label %16
16: ; preds = %10, %14
%17 = load i32, i32* @STDIN_FILENO
%18 = tail call i64 @isatty(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %20, %16, %8
ret i32 %6
}