#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 dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int sscanf(int /*<<< orphan*/, char *, int *);
int findmidtty(char *sysdevdir) {
  DIR *dd;
  struct dirent *de;
  int di;
  int ret = -1;
  if (!(dd = opendir(sysdevdir)))
    return ret;
  while ((de = readdir(dd))) {
    if (sscanf(de->d_name, "ttyUSB%i", &di) != 1)
      continue;
    if (ret < 0 || di < ret)
      ret = di;
  };
  return ret < 0 ? ret : ret + 1;
}


x86 assembly (clang, -O0):
<findmidtty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1165 <findmidtty+0x35>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <findmidtty+0xba>
jmp    116a <findmidtty+0x3a>
mov    -0x18(%rbp),%rdi
call   1040 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11c6 <findmidtty+0x96>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     11a5 <findmidtty+0x75>
jmp    116a <findmidtty+0x3a>
cmpl   $0x0,-0x28(%rbp)
jl     11bb <findmidtty+0x8b>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    11c1 <findmidtty+0x91>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    116a <findmidtty+0x3a>
cmpl   $0x0,-0x28(%rbp)
jge    11db <findmidtty+0xab>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11e4 <findmidtty+0xb4>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findmidtty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
call   1030 <opendir@plt>
test   %rax,%rax
je     119e <findmidtty+0x6e>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
lea    0xeb0(%rip),%r14
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1040 <readdir@plt>
test   %rax,%rax
je     1193 <findmidtty+0x63>
mov    (%rax),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    1160 <findmidtty+0x30>
mov    0x4(%rsp),%eax
cmp    %ebp,%eax
mov    %ebp,%ecx
cmovl  %eax,%ecx
test   %ebp,%ebp
cmovs  %eax,%ecx
mov    %ecx,%ebp
jmp    1160 <findmidtty+0x30>
mov    %ebp,%eax
not    %eax
shr    $0x1f,%eax
add    %ebp,%eax
jmp    11a3 <findmidtty+0x73>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"ttyUSB%i\00"
define i32 @findmidtty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.dirent*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %7
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = load i32, i32* %7
store i32 %12, i32* %2
br label %45
13: ; preds = %1
br label %14
14: ; preds = %34, %24, %13
%15 = load i32*, i32** %4
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %5
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %35
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %5
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @sscanf(i32 %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32* %6)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %18
br label %14
25: ; preds = %18
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28, %25
%33 = load i32, i32* %6
store i32 %33, i32* %7
br label %34
34: ; preds = %32, %28
br label %14
35: ; preds = %14
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i32, i32* %7
br label %43
40: ; preds = %35
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
br label %43
43: ; preds = %40, %38
%44 = phi i32 [ %39, %38 ], [ %42, %40 ]
store i32 %44, i32* %2
br label %45
45: ; preds = %43, %11
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [9 x i8] c"ttyUSB%i\00"
define i32 @findmidtty(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %26, label %6
6: ; preds = %1, %16
%7 = phi i32 [ %21, %16 ], [ -1, %1 ]
br label %8
8: ; preds = %6, %11
%9 = call %struct.dirent* @readdir(i32* %4)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.dirent, %struct.dirent* %9, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @sscanf(i32 %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32* %2)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %8
16: ; preds = %11
%17 = icmp slt i32 %7, 0
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, %7
%20 = select i1 %17, i1 true, i1 %19
%21 = select i1 %20, i32 %18, i32 %7
br label %6
22: ; preds = %8
%23 = xor i32 %7, -1
%24 = lshr i32 %23, 31
%25 = add nsw i32 %24, %7
br label %26
26: ; preds = %1, %22
%27 = phi i32 [ %25, %22 ], [ -1, %1 ]
ret i32 %27
}