#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 */
struct termios {
  int c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int O_NDELAY;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_port(int fd, int verbose) {
  char ttyport[33];
  int n;
  for (n = 0; n < 10; n++) {
    sprintf(ttyport, "/dev/ttyACM%d", n);
    fd = open(ttyport, O_RDWR | O_NDELAY);
    if (fd != -1) {
      if (verbose)
        break;
    }
  }
  if (fd == -1) {
    return fd;
  } else {
    struct termios options;
    tcgetattr(fd, &options);
    cfsetispeed(&options, B9600);
    options.c_cflag |= (CLOCAL | CREAD);
    tcsetattr(fd, TCSANOW, &options);
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    return (fd);
  }
}


x86 assembly (clang, -O0):
<open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0xa,-0x34(%rbp)
jge    11d1 <open_port+0x81>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xe83(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11be <open_port+0x6e>
cmpl   $0x0,-0xc(%rbp)
je     11b9 <open_port+0x69>
jmp    11d1 <open_port+0x81>
jmp    11be <open_port+0x6e>
jmp    11c3 <open_port+0x73>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1165 <open_port+0x15>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11e6 <open_port+0x96>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129d <open_port+0x14d>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1060 <cfsetispeed@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    0x2db4(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
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   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
lea    0xe9d(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %edx,%edx
call   1070 <sprintf@plt>
mov    0x2e6f(%rip),%r14
mov    0x2e40(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     119d <open_port+0x4d>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xe5c(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     11d3 <open_port+0x83>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xe26(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     1209 <open_port+0xb9>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xdf0(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x3,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     123f <open_port+0xef>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xdba(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x4,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     1275 <open_port+0x125>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xd84(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x5,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     12ab <open_port+0x15b>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xd4e(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x6,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     12e1 <open_port+0x191>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xd18(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x7,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     1313 <open_port+0x1c3>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xce6(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x8,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %ebx,%ebx
je     1345 <open_port+0x1f5>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1373 <open_port+0x223>
lea    0xcb4(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x9,%edx
call   1070 <sprintf@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     13c9 <open_port+0x279>
lea    0x8(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <tcgetattr@plt>
mov    0x2c4f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <cfsetispeed@plt>
mov    0x2c16(%rip),%rax
mov    0x2c2f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
mov    0x2c30(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    13bc <open_port+0x26c>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [14 x i8] c"/dev/ttyACM%d\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@B9600 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
define i32 @open_port(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [33 x i8]
%7 = alloca i32
%8 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %9
9: ; preds = %29, %2
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%14 = load i32, i32* %7
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NDELAY
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %12
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
br label %32
27: ; preds = %23
br label %28
28: ; preds = %27, %12
br label %29
29: ; preds = %28
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %9
32: ; preds = %26, %9
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i32, i32* %4
store i32 %36, i32* %3
br label %80
37: ; preds = %32
%38 = load i32, i32* %4
%39 = call i32 @tcgetattr(i32 %38, %struct.termios* %8)
%40 = load i32, i32* @B9600
%41 = call i32 @cfsetispeed(%struct.termios* %8, i32 %40)
%42 = load i32, i32* @CLOCAL
%43 = load i32, i32* @CREAD
%44 = or i32 %42, %43
%45 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%46 = load i32, i32* %45
%47 = or i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* %4
%49 = load i32, i32* @TCSANOW
%50 = call i32 @tcsetattr(i32 %48, i32 %49, %struct.termios* %8)
%51 = load i32, i32* @CSIZE
%52 = xor i32 %51, -1
%53 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%54 = load i32, i32* %53
%55 = and i32 %54, %52
store i32 %55, i32* %53
%56 = load i32, i32* @CS8
%57 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = or i32 %58, %56
store i32 %59, i32* %57
%60 = load i32, i32* @PARENB
%61 = xor i32 %60, -1
%62 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = and i32 %63, %61
store i32 %64, i32* %62
%65 = load i32, i32* @CSTOPB
%66 = xor i32 %65, -1
%67 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%68 = load i32, i32* %67
%69 = and i32 %68, %66
store i32 %69, i32* %67
%70 = load i32, i32* @CSIZE
%71 = xor i32 %70, -1
%72 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%73 = load i32, i32* %72
%74 = and i32 %73, %71
store i32 %74, i32* %72
%75 = load i32, i32* @CS8
%76 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%77 = load i32, i32* %76
%78 = or i32 %77, %75
store i32 %78, i32* %76
%79 = load i32, i32* %4
store i32 %79, i32* %3
br label %80
80: ; preds = %37, %35
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [14 x i8] c"/dev/ttyACM%d\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@B9600 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
define i32 @open_port(i32 %0, i32 %1) {
%3 = alloca [33 x i8]
%4 = alloca %struct.termios
%5 = getelementptr inbounds [33 x i8], [33 x i8]* %3, i64 0, i64 0
%6 = icmp ne i32 %1, 0
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %9, %8
%11 = call i32 @open(i8* %5, i32 %10)
%12 = icmp ne i32 %11, -1
%13 = and i1 %6, %12
br i1 %13, label %85, label %14
14: ; preds = %2
%15 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 1)
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NDELAY
%18 = or i32 %17, %16
%19 = call i32 @open(i8* %5, i32 %18)
%20 = icmp ne i32 %19, -1
%21 = and i1 %6, %20
br i1 %21, label %85, label %22
22: ; preds = %14
%23 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 2)
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NDELAY
%26 = or i32 %25, %24
%27 = call i32 @open(i8* %5, i32 %26)
%28 = icmp ne i32 %27, -1
%29 = and i1 %6, %28
br i1 %29, label %85, label %30
30: ; preds = %22
%31 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 3)
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NDELAY
%34 = or i32 %33, %32
%35 = call i32 @open(i8* %5, i32 %34)
%36 = icmp ne i32 %35, -1
%37 = and i1 %6, %36
br i1 %37, label %85, label %38
38: ; preds = %30
%39 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 4)
%40 = load i32, i32* @O_RDWR
%41 = load i32, i32* @O_NDELAY
%42 = or i32 %41, %40
%43 = call i32 @open(i8* %5, i32 %42)
%44 = icmp ne i32 %43, -1
%45 = and i1 %6, %44
br i1 %45, label %85, label %46
46: ; preds = %38
%47 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 5)
%48 = load i32, i32* @O_RDWR
%49 = load i32, i32* @O_NDELAY
%50 = or i32 %49, %48
%51 = call i32 @open(i8* %5, i32 %50)
%52 = icmp ne i32 %51, -1
%53 = and i1 %6, %52
br i1 %53, label %85, label %54
54: ; preds = %46
%55 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 6)
%56 = load i32, i32* @O_RDWR
%57 = load i32, i32* @O_NDELAY
%58 = or i32 %57, %56
%59 = call i32 @open(i8* %5, i32 %58)
%60 = icmp ne i32 %59, -1
%61 = and i1 %6, %60
br i1 %61, label %85, label %62
62: ; preds = %54
%63 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 7)
%64 = load i32, i32* @O_RDWR
%65 = load i32, i32* @O_NDELAY
%66 = or i32 %65, %64
%67 = call i32 @open(i8* %5, i32 %66)
%68 = icmp ne i32 %67, -1
%69 = and i1 %6, %68
br i1 %69, label %85, label %70
70: ; preds = %62
%71 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 8)
%72 = load i32, i32* @O_RDWR
%73 = load i32, i32* @O_NDELAY
%74 = or i32 %73, %72
%75 = call i32 @open(i8* %5, i32 %74)
%76 = icmp ne i32 %75, -1
%77 = and i1 %6, %76
br i1 %77, label %85, label %78
78: ; preds = %70
%79 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 9)
%80 = load i32, i32* @O_RDWR
%81 = load i32, i32* @O_NDELAY
%82 = or i32 %81, %80
%83 = call i32 @open(i8* %5, i32 %82)
%84 = icmp eq i32 %83, -1
br i1 %84, label %99, label %85
85: ; preds = %2, %14, %22, %30, %38, %46, %54, %62, %70, %78
%86 = phi i32 [ %11, %2 ], [ %19, %14 ], [ %27, %22 ], [ %35, %30 ], [ %43, %38 ], [ %51, %46 ], [ %59, %54 ], [ %67, %62 ], [ %75, %70 ], [ %83, %78 ]
%87 = bitcast %struct.termios* %4 to i8*
%88 = call i32 @tcgetattr(i32 %86, %struct.termios* %4)
%89 = load i32, i32* @B9600
%90 = call i32 @cfsetispeed(%struct.termios* %4, i32 %89)
%91 = load i32, i32* @CLOCAL
%92 = load i32, i32* @CREAD
%93 = or i32 %92, %91
%94 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%95 = load i32, i32* %94
%96 = or i32 %93, %95
store i32 %96, i32* %94
%97 = load i32, i32* @TCSANOW
%98 = call i32 @tcsetattr(i32 %86, i32 %97, %struct.termios* %4)
br label %99
99: ; preds = %78, %85
%100 = phi i32 [ %86, %85 ], [ -1, %78 ]
ret i32 %100
}