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


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11ac <open_port+0x5c>
lea    0xe65(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <open_port+0x10c>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    11e9 <open_port+0x99>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    120c <open_port+0xbc>
mov    -0x8(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1237 <open_port+0xe7>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1245 <open_port+0xf5>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rcx
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%rdi
mov    0x2e48(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1223 <open_port+0xc3>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e29(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     11d5 <open_port+0x75>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11e1 <open_port+0x81>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
lea    0xe5a(%rip),%rax
lea    0xe43(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xe11(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
jmp    1219 <open_port+0xb9>

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [23 x i8] c"Can't Open Serial Port\00"
@F_SETFL = global i32 0
@.str.3 = [10 x i8] c"fcntl=%d\0A\00"
@STDIN_FILENO = global i32 0
@.str.6 = [21 x i8] c"serial : %d open OK\0A\00"
@str = [13 x i8] c"open success\00"
@str.7 = [16 x i8] c"isatty success!\00"
@str.8 = [40 x i8] c"standard input is not a terminal device\00"
@str.9 = [14 x i8] c"fcntl failed!\00"
define i32 @open_port(i32 %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %1, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %30
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @F_SETFL
%15 = tail call i32 @fcntl(i32 %8, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %23
19: ; preds = %12
%20 = load i32, i32* @F_SETFL
%21 = tail call i32 @fcntl(i32 %8, i32 %20, i32 0)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %17
%24 = load i32, i32* @STDIN_FILENO
%25 = tail call i64 @isatty(i32 %24)
%26 = icmp eq i64 %25, 0
%27 = select i1 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @str.8, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0)
%28 = tail call i32 @puts(i8* dereferenceable(1) %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %8)
br label %30
30: ; preds = %23, %10
ret i32 %8
}