#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 O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int O_SYNC;
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *, int);
scalar_t__ serial_set_attribs(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int serial_open() {
  int fd;
  char dbuf[32];
  for (int i = 0; i < 9; i++) {
    sprintf(dbuf, "/dev/ttyUSB%d", i);
    fd = open(dbuf, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
    if (fd >= 0) {
      printf("Found output on '%s', %d\n", dbuf, fd);
      break;
    }
    sprintf(dbuf, "/dev/ttyACM%d", i);
    fd = open(dbuf, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
    if (fd >= 0) {
      printf("Found output on '%s', %d\n", dbuf, fd);
      break;
    }
  }
  if (fd < 0)
    return -1;
  if (serial_set_attribs(fd) < 0)
    return -2;
  return fd;
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x34(%rbp)
cmpl   $0x9,-0x34(%rbp)
jge    1236 <serial_open+0xf6>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xe99(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11be <serial_open+0x7e>
lea    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
jmp    1236 <serial_open+0xf6>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xe5c(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
or     (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1223 <serial_open+0xe3>
lea    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
jmp    1236 <serial_open+0xf6>
jmp    1228 <serial_open+0xe8>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    114f <serial_open+0xf>
cmpl   $0x0,-0x8(%rbp)
jge    124c <serial_open+0x10c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <serial_open+0x130>
mov    -0x8(%rbp),%edi
call   1030 <serial_set_attribs@plt>
cmp    $0x0,%rax
jge    126a <serial_open+0x12a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1270 <serial_open+0x130>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %r13d,%r13d
mov    %rsp,%rbx
mov    0x2e8d(%rip),%r14
mov    0x2e7e(%rip),%r15
mov    0x2e6f(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
lea    0xe86(%rip),%rsi
mov    %r13d,%edx
call   1060 <sprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
or     (%r15),%esi
or     (%r12),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    11f1 <serial_open+0xb1>
mov    %rbx,%rdi
lea    0xe7d(%rip),%rsi
mov    %r13d,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
or     (%r15),%esi
or     (%r12),%esi
call   1050 <open@plt>
test   %eax,%eax
jns    11f1 <serial_open+0xb1>
add    $0x1,%r13d
cmp    $0x9,%r13d
mov    0x2e07(%rip),%r14
mov    0x2df0(%rip),%r12
jne    1170 <serial_open+0x30>
mov    $0xffffffff,%eax
jmp    1216 <serial_open+0xd6>
mov    %eax,%ebp
lea    0xe14(%rip),%rdi
mov    %rsp,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1030 <serial_set_attribs@plt>
test   %rax,%rax
mov    $0xfffffffe,%eax
cmovns %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/ttyUSB%d\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c"Found output on '%s', %d\0A\00"
@.str.2 = [14 x i8] c"/dev/ttyACM%d\00"
define i32 @serial_open() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca i32
store i32 0, i32* %4
br label %5
5: ; preds = %47, %0
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 9
br i1 %7, label %8, label %50
8: ; preds = %5
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_SYNC
%17 = or i32 %15, %16
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %12, i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %8
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%25 = load i32, i32* %2
%26 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %25)
br label %50
27: ; preds = %8
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%29 = load i32, i32* %4
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NOCTTY
%34 = or i32 %32, %33
%35 = load i32, i32* @O_SYNC
%36 = or i32 %34, %35
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %31, i32 %38)
store i32 %39, i32* %2
%40 = load i32, i32* %2
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %27
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%44 = load i32, i32* %2
%45 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %43, i32 %44)
br label %50
46: ; preds = %27
br label %47
47: ; preds = %46
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
store i32 %49, i32* %4
br label %5
50: ; preds = %42, %23, %5
%51 = load i32, i32* %2
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 -1, i32* %1
br label %61
54: ; preds = %50
%55 = load i32, i32* %2
%56 = call i64 @serial_set_attribs(i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %54
store i32 -2, i32* %1
br label %61
59: ; preds = %54
%60 = load i32, i32* %2
store i32 %60, i32* %1
br label %61
61: ; preds = %59, %58, %53
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/ttyUSB%d\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c"Found output on '%s', %d\0A\00"
@.str.2 = [14 x i8] c"/dev/ttyACM%d\00"
define i32 @serial_open() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
br label %6
3: ; preds = %18
%4 = add nuw nsw i32 %7, 1
%5 = icmp eq i32 %4, 9
br i1 %5, label %35, label %6
6: ; preds = %0, %3
%7 = phi i32 [ 0, %0 ], [ %4, %3 ]
%8 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %10, %9
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %2, i32 %15)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %29, label %18
18: ; preds = %6
%19 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_NOCTTY
%22 = or i32 %21, %20
%23 = load i32, i32* @O_SYNC
%24 = or i32 %22, %23
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %2, i32 %26)
%28 = icmp sgt i32 %27, -1
br i1 %28, label %29, label %3
29: ; preds = %18, %6
%30 = phi i32 [ %16, %6 ], [ %27, %18 ]
%31 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 %30)
%32 = call i64 @serial_set_attribs(i32 %30)
%33 = icmp slt i64 %32, 0
%34 = select i1 %33, i32 -2, i32 %30
br label %35
35: ; preds = %3, %29
%36 = phi i32 [ %34, %29 ], [ -1, %3 ]
ret i32 %36
}