#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, int comport) {
  char *dev[] = {"/dev/tts/0", "/dev/tts/1", "/dev/tts/2"};
  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, 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("fd-open=%d\n", fd);
  return fd;
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2c7b(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c78(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c75(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11ec <open_port+0x9c>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
lea    0xe76(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11d9 <open_port+0x89>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <open_port+0x1fe>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ac <open_port+0x15c>
cmpl   $0x1,-0xc(%rbp)
jne    1247 <open_port+0xf7>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0xe50(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1234 <open_port+0xe4>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <open_port+0x1fe>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a7 <open_port+0x157>
cmpl   $0x2,-0xc(%rbp)
jne    12a2 <open_port+0x152>
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    128f <open_port+0x13f>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <open_port+0x1fe>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a2 <open_port+0x152>
jmp    12a7 <open_port+0x157>
jmp    12ac <open_port+0x15c>
mov    -0x8(%rbp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    12db <open_port+0x18b>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12fe <open_port+0x1ae>
mov    -0x8(%rbp),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1329 <open_port+0x1d9>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1337 <open_port+0x1e7>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%esi
je     11b3 <open_port+0x53>
cmp    $0x1,%esi
je     11dc <open_port+0x7c>
mov    %edi,%ebx
test   %esi,%esi
jne    1208 <open_port+0xa8>
mov    0x2e69(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
lea    0xe66(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     128b <open_port+0x12b>
mov    %eax,%ebx
lea    0xf36(%rip),%rdi
jmp    1203 <open_port+0xa3>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    0xe83(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1294 <open_port+0x134>
mov    %eax,%ebx
lea    0xeeb(%rip),%rdi
jmp    1203 <open_port+0xa3>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe37(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     129d <open_port+0x13d>
mov    %eax,%ebx
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc1(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     123d <open_port+0xdd>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1249 <open_port+0xe9>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
lea    0xe2e(%rip),%rax
lea    0xe17(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xdfb(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd79(%rip),%rdi
jmp    12a4 <open_port+0x144>
lea    0xdb6(%rip),%rdi
jmp    12a4 <open_port+0x144>
lea    0xd8a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1281 <open_port+0x121>

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

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