#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/ttyUSB0", "/dev/ttyUSB1"};
  long vdisable;
  if (comport == 0) {
    fd = open(dev[comport], O_RDWR | O_NOCTTY | O_NDELAY);
    if (-1 == fd) {
      perror("Can't Open Serial Port");
      return (-1);
    } else {
      printf("open %s .....\n", dev[comport]);
    }
  } else if (comport == 1) {
    fd = open(dev[comport], O_RDWR | O_NOCTTY | O_NDELAY);
    if (-1 == fd) {
      perror("Can't Open Serial Port");
      return (-1);
    } else {
      printf("open %s .....\n", dev[comport]);
    }
  } else if (comport == 2) {
    fd = open(dev[comport], O_RDWR | O_NOCTTY | O_NDELAY);
    if (-1 == fd) {
      perror("Can't Open Serial Port");
      return (-1);
    } else {
      printf("open %s .....\n", dev[comport]);
    }
  }
  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    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2c7b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c78(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11ec <open_port+0x9c>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11d0 <open_port+0x80>
lea    0xe5b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <open_port+0x238>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e6 <open_port+0x196>
cmpl   $0x1,-0xc(%rbp)
jne    1264 <open_port+0x114>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
or     (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1248 <open_port+0xf8>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <open_port+0x238>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e1 <open_port+0x191>
cmpl   $0x2,-0xc(%rbp)
jne    12dc <open_port+0x18c>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    0x2d49(%rip),%rax
or     (%rax),%esi
mov    0x2d30(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    12c0 <open_port+0x170>
lea    0xd6b(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <open_port+0x238>
movslq -0xc(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rsi
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12dc <open_port+0x18c>
jmp    12e1 <open_port+0x191>
jmp    12e6 <open_port+0x196>
mov    -0x8(%rbp),%edi
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    1315 <open_port+0x1c5>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1338 <open_port+0x1e8>
mov    -0x8(%rbp),%edi
mov    0x2ca9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%esi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
jne    1363 <open_port+0x213>
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1371 <open_port+0x221>
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_port>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
je     119d <open_port+0x3d>
cmp    $0x1,%esi
je     11c1 <open_port+0x61>
mov    %edi,%ebx
cmp    $0x2,%esi
jne    1207 <open_port+0xa7>
mov    0x2e69(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
jmp    128a <open_port+0x12a>
mov    0x2e44(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%r14
jmp    11e3 <open_port+0x83>
mov    0x2e20(%rip),%rax
mov    0x2e09(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
lea    0xe2a(%rip),%r14
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     128a <open_port+0x12a>
lea    0xe34(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dc2(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     123c <open_port+0xdc>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
lea    0xe0f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1248 <open_port+0xe8>
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
test   %rax,%rax
lea    0xe06(%rip),%rax
lea    0xdef(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xdd3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1280 <open_port+0x120>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@.str.1 = [13 x i8] c"/dev/ttyUSB1\00"
@__const.open_port.dev = [2 x i8*] [i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i32 0, i32 0)]
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [23 x i8] c"Can't Open Serial Port\00"
@.str.3 = [15 x i8] c"open %s .....\0A\00"
@F_SETFL = global i32 0
@.str.4 = [15 x i8] c"fcntl failed!\0A\00"
@.str.5 = [10 x i8] c"fcntl=%d\0A\00"
@STDIN_FILENO = global i32 0
@.str.6 = [41 x i8] c"standard input is not a terminal device\0A\00"
@.str.7 = [17 x i8] c"isatty success!\0A\00"
@.str.8 = [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 [2 x i8*]
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = bitcast [2 x i8*]* %6 to i8*
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %2
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %13
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = load i32, i32* @O_NDELAY
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %15, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %109
26: ; preds = %11
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %28
%30 = load i8*, i8** %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %26
br label %85
33: ; preds = %2
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %58
36: ; preds = %33
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %38
%40 = load i8*, i8** %39
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_NOCTTY
%43 = or i32 %41, %42
%44 = load i32, i32* @O_NDELAY
%45 = or i32 %43, %44
%46 = call i32 @open(i8* %40, i32 %45)
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = icmp eq i32 -1, %47
br i1 %48, label %49, label %51
49: ; preds = %36
%50 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %109
51: ; preds = %36
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %53
%55 = load i8*, i8** %54
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %51
br label %84
58: ; preds = %33
%59 = load i32, i32* %5
%60 = icmp eq i32 %59, 2
br i1 %60, label %61, label %83
61: ; preds = %58
%62 = load i32, i32* %5
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %63
%65 = load i8*, i8** %64
%66 = load i32, i32* @O_RDWR
%67 = load i32, i32* @O_NOCTTY
%68 = or i32 %66, %67
%69 = load i32, i32* @O_NDELAY
%70 = or i32 %68, %69
%71 = call i32 @open(i8* %65, i32 %70)
store i32 %71, i32* %4
%72 = load i32, i32* %4
%73 = icmp eq i32 -1, %72
br i1 %73, label %74, label %76
74: ; preds = %61
%75 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %109
76: ; preds = %61
%77 = load i32, i32* %5
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 %78
%80 = load i8*, i8** %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %76
br label %83
83: ; preds = %82, %58
br label %84
84: ; preds = %83, %57
br label %85
85: ; preds = %84, %32
%86 = load i32, i32* %4
%87 = load i32, i32* @F_SETFL
%88 = call i32 @fcntl(i32 %86, i32 %87, i32 0)
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %97
92: ; preds = %85
%93 = load i32, i32* %4
%94 = load i32, i32* @F_SETFL
%95 = call i32 @fcntl(i32 %93, i32 %94, i32 0)
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %95)
br label %97
97: ; preds = %92, %90
%98 = load i32, i32* @STDIN_FILENO
%99 = call i64 @isatty(i32 %98)
%100 = icmp eq i64 %99, 0
br i1 %100, label %101, label %103
101: ; preds = %97
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
br label %105
103: ; preds = %97
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %105
105: ; preds = %103, %101
%106 = load i32, i32* %4
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %106)
%108 = load i32, i32* %4
store i32 %108, i32* %3
br label %109
109: ; preds = %105, %74, %49, %24
%110 = load i32, i32* %3
ret i32 %110
}

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