#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 *dev) {
  long vdisable;
  fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("Can't open serial port");
    return -1;
  } else {
    printf("Open %s ...\n", dev);
  }
  if (fcntl(fd, F_SETFL, 0) < 0) {
    printf("fcntl fail\n");
    return -1;
  } else {
    printf("fcntl = %d\n", fcntl(fd, F_SETFL, 0));
  }
  if (isatty(STDIN_FILENO) == 0) {
    printf("stdin is not a output device\n");
    return -1;
  }
  printf("fd-open = %d\n", fd);
  return fd;
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a8 <open_port+0x58>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <open_port+0x10c>
mov    -0x10(%rbp),%rsi
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    11f0 <open_port+0xa0>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <open_port+0x10c>
mov    -0x8(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%esi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1245 <open_port+0xf5>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <open_port+0x10c>
mov    -0x8(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rcx
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%rbx
mov    0x2e49(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1220 <open_port+0xc0>
mov    %eax,%ebp
lea    0xe7a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e22(%rip),%rbx
mov    (%rbx),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     11fd <open_port+0x9d>
mov    (%rbx),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
lea    0xe55(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
je     1206 <open_port+0xa6>
lea    0xe3e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1217 <open_port+0xb7>
lea    0xe57(%rip),%rdi
jmp    120d <open_port+0xad>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
jmp    1212 <open_port+0xb2>

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

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"
@.str.1 = [13 x i8] c"Open %s ...\0A\00"
@F_SETFL = global i32 0
@.str.3 = [12 x i8] c"fcntl = %d\0A\00"
@STDIN_FILENO = global i32 0
@.str.5 = [14 x i8] c"fd-open = %d\0A\00"
@str = [29 x i8] c"stdin is not a output device\00"
@str.6 = [11 x i8] c"fcntl fail\00"
define i32 @open_port(i32 %0, i8* %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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%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 ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
br label %30
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 ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @STDIN_FILENO
%24 = tail call i64 @isatty(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %30
28: ; preds = %19
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %8)
br label %30
30: ; preds = %28, %26, %17, %10
%31 = phi i32 [ -1, %10 ], [ -1, %17 ], [ -1, %26 ], [ %8, %28 ]
ret i32 %31
}