#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*/ FNDELAY;
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 comport) {
  int fd;
  long vdisable;
  if (comport == 0) {
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (-1 == fd) {
      perror("Can't Open Serial Port0");
      return (-1);
    } else
      printf("open tts/0 .....\n");
  } else if (comport == 1) {
    fd = open("/dev/ttyS1", O_RDWR);
    if (-1 == fd) {
      perror("Can't Open Serial Port2");
      return (-1);
    } else
      printf("open tts/1 .....\n");
  } else if (comport == 2) {
    fd = open("/dev/ttyS2", O_RDWR);
    if (-1 == fd) {
      perror("Can't Open Serial Port3");
      return (-1);
    } else
      printf("open tts/2 .....\n");
  }
  if (fcntl(fd, F_SETFL, FNDELAY) < 0)
    printf("fcntl failed!\n");
  else
    printf("fcntl=%d\n", fcntl(fd, F_SETFL, FNDELAY));
  if (isatty(STDIN_FILENO) == 0)
    printf("standard input is not a terminal device\n");
  else
    printf("isatty success!\n");
  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)
cmpl   $0x0,-0x8(%rbp)
jne    11c8 <open_port+0x78>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
lea    0xe79(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    11b5 <open_port+0x65>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <open_port+0x1e8>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1288 <open_port+0x138>
cmpl   $0x1,-0x8(%rbp)
jne    1223 <open_port+0xd3>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    1210 <open_port+0xc0>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <open_port+0x1e8>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1283 <open_port+0x133>
cmpl   $0x2,-0x8(%rbp)
jne    127e <open_port+0x12e>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    126b <open_port+0x11b>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <open_port+0x1e8>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127e <open_port+0x12e>
jmp    1283 <open_port+0x133>
jmp    1288 <open_port+0x138>
mov    -0xc(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    12be <open_port+0x16e>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e8 <open_port+0x198>
mov    -0xc(%rbp),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
mov    0x2cef(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1313 <open_port+0x1c3>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1321 <open_port+0x1d1>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r15
push   %r14
push   %rbx
cmp    $0x2,%edi
je     11b2 <open_port+0x52>
cmp    $0x1,%edi
je     11db <open_port+0x7b>
test   %edi,%edi
jne    1207 <open_port+0xa7>
mov    0x2e6a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe67(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1291 <open_port+0x131>
mov    %eax,%ebx
lea    0xf37(%rip),%rdi
jmp    1202 <open_port+0xa2>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     129a <open_port+0x13a>
mov    %eax,%ebx
lea    0xeec(%rip),%rdi
jmp    1202 <open_port+0xa2>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12a3 <open_port+0x143>
mov    %eax,%ebx
lea    0xed4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc2(%rip),%r14
mov    (%r14),%esi
mov    0x2da8(%rip),%r15
mov    (%r15),%edx
mov    %ebx,%edi
call   1050 <fcntl@plt>
test   %eax,%eax
js     1245 <open_port+0xe5>
mov    (%r14),%esi
mov    (%r15),%edx
mov    %ebx,%edi
call   1050 <fcntl@plt>
lea    0xe2f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1251 <open_port+0xf1>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
lea    0xe26(%rip),%rax
lea    0xe0f(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xdf3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd73(%rip),%rdi
jmp    12aa <open_port+0x14a>
lea    0xdb0(%rip),%rdi
jmp    12aa <open_port+0x14a>
lea    0xd84(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1289 <open_port+0x129>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [24 x i8] c"Can't Open Serial Port0\00"
@.str.2 = [18 x i8] c"open tts/0 .....\0A\00"
@.str.3 = [11 x i8] c"/dev/ttyS1\00"
@.str.4 = [24 x i8] c"Can't Open Serial Port2\00"
@.str.5 = [18 x i8] c"open tts/1 .....\0A\00"
@.str.6 = [11 x i8] c"/dev/ttyS2\00"
@.str.7 = [24 x i8] c"Can't Open Serial Port3\00"
@.str.8 = [18 x i8] c"open tts/2 .....\0A\00"
@F_SETFL = global i32 0
@FNDELAY = global i32 0
@.str.9 = [15 x i8] c"fcntl failed!\0A\00"
@.str.10 = [10 x i8] c"fcntl=%d\0A\00"
@STDIN_FILENO = global i32 0
@.str.11 = [41 x i8] c"standard input is not a terminal device\0A\00"
@.str.12 = [17 x i8] c"isatty success!\0A\00"
@.str.13 = [12 x i8] c"fd-open=%d\0A\00"
define i32 @open_port(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NDELAY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 -1, %15
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %76
19: ; preds = %8
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19
br label %50
22: ; preds = %1
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %26)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp eq i32 -1, %28
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %76
32: ; preds = %25
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32
br label %49
35: ; preds = %22
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %48
38: ; preds = %35
%39 = load i32, i32* @O_RDWR
%40 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %39)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp eq i32 -1, %41
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %2
br label %76
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %47
47: ; preds = %45
br label %48
48: ; preds = %47, %35
br label %49
49: ; preds = %48, %34
br label %50
50: ; preds = %49, %21
%51 = load i32, i32* %4
%52 = load i32, i32* @F_SETFL
%53 = load i32, i32* @FNDELAY
%54 = call i32 @fcntl(i32 %51, i32 %52, i32 %53)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %64
58: ; preds = %50
%59 = load i32, i32* %4
%60 = load i32, i32* @F_SETFL
%61 = load i32, i32* @FNDELAY
%62 = call i32 @fcntl(i32 %59, i32 %60, i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %58, %56
%65 = load i32, i32* @STDIN_FILENO
%66 = call i64 @isatty(i32 %65)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.11, i64 0, i64 0))
br label %72
70: ; preds = %64
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
br label %72
72: ; preds = %70, %68
%73 = load i32, i32* %4
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0), i32 %73)
%75 = load i32, i32* %4
store i32 %75, i32* %2
br label %76
76: ; preds = %72, %43, %30, %17
%77 = load i32, i32* %2
ret i32 %77
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [24 x i8] c"Can't Open Serial Port0\00"
@.str.3 = [11 x i8] c"/dev/ttyS1\00"
@.str.4 = [24 x i8] c"Can't Open Serial Port2\00"
@.str.6 = [11 x i8] c"/dev/ttyS2\00"
@.str.7 = [24 x i8] c"Can't Open Serial Port3\00"
@F_SETFL = global i32 0
@FNDELAY = global i32 0
@.str.10 = [10 x i8] c"fcntl=%d\0A\00"
@STDIN_FILENO = global i32 0
@.str.13 = [12 x i8] c"fd-open=%d\0A\00"
@str = [16 x i8] c"isatty success!\00"
@str.14 = [40 x i8] c"standard input is not a terminal device\00"
@str.15 = [14 x i8] c"fcntl failed!\00"
@str.16 = [17 x i8] c"open tts/2 .....\00"
@str.17 = [17 x i8] c"open tts/1 .....\00"
@str.18 = [17 x i8] c"open tts/0 .....\00"
define i32 @open_port(i32 %0) {
switch i32 %0, label %28 [
i32 0, label %2
i32 1, label %12
i32 2, label %18
]
2: ; preds = %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* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %24
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %48
12: ; preds = %1
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %48
18: ; preds = %1
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %48
24: ; preds = %18, %12, %2
%25 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.18, i64 0, i64 0), %2 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.17, i64 0, i64 0), %12 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.16, i64 0, i64 0), %18 ]
%26 = phi i32 [ %8, %2 ], [ %14, %12 ], [ %20, %18 ]
%27 = tail call i32 @puts(i8* dereferenceable(1) %25)
br label %28
28: ; preds = %24, %1
%29 = phi i32 [ undef, %1 ], [ %26, %24 ]
%30 = load i32, i32* @F_SETFL
%31 = load i32, i32* @FNDELAY
%32 = tail call i32 @fcntl(i32 %29, i32 %30, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.15, i64 0, i64 0))
br label %41
36: ; preds = %28
%37 = load i32, i32* @F_SETFL
%38 = load i32, i32* @FNDELAY
%39 = tail call i32 @fcntl(i32 %29, i32 %37, i32 %38)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %36, %34
%42 = load i32, i32* @STDIN_FILENO
%43 = tail call i64 @isatty(i32 %42)
%44 = icmp eq i64 %43, 0
%45 = select i1 %44, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @str.14, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0)
%46 = tail call i32 @puts(i8* dereferenceable(1) %45)
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.13, i64 0, i64 0), i32 %29)
br label %48
48: ; preds = %41, %22, %16, %10
%49 = phi i32 [ -1, %10 ], [ %29, %41 ], [ -1, %16 ], [ -1, %22 ]
ret i32 %49
}