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

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ usb_checkdevice(char *);
void usb_scandir(char *dirname) {
  char path[PATH_MAX];
  DIR *dr;
  struct dirent *de;
  struct stat st;
  if ((dr = opendir(dirname)) == NULL) {
    return;
  }
  while ((de = readdir(dr)) != NULL) {
    if (de->d_name[0] < '0' || de->d_name[0] > '9')
      continue;
    snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
    if (stat(path, &st) == -1)
      continue;
    if (S_ISDIR(st.st_mode)) {
      usb_scandir(path);
      continue;
    }
    usb_checkdevice(path);
  }
  closedir(dr);
}


x86 assembly (clang, -O0):
<usb_scandir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11df <usb_scandir+0x5f>
movl   $0x1,-0x34(%rbp)
jmp    12a0 <usb_scandir+0x120>
jmp    11e4 <usb_scandir+0x64>
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1290 <usb_scandir+0x110>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     1221 <usb_scandir+0xa1>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1226 <usb_scandir+0xa6>
jmp    11e4 <usb_scandir+0x64>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xdbe(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    1262 <usb_scandir+0xe2>
jmp    11e4 <usb_scandir+0x64>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1282 <usb_scandir+0x102>
mov    -0x40(%rbp),%rdi
call   10a0 <usb_scandir@plt>
jmp    11e4 <usb_scandir+0x64>
mov    -0x40(%rbp),%rdi
call   1090 <usb_checkdevice@plt>
jmp    11e4 <usb_scandir+0x64>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usb_scandir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e58(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1030 <opendir@plt>
test   %rax,%rax
je     123e <usb_scandir+0xbe>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1236 <usb_scandir+0xb6>
lea    -0x30(%rbp),%r13
jmp    11ed <usb_scandir+0x6d>
call   10a0 <usb_scandir@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1236 <usb_scandir+0xb6>
mov    (%rax),%r8
movzbl (%r8),%eax
add    $0xc6,%al
cmp    $0xf6,%al
jb     11e0 <usb_scandir+0x60>
mov    %rbx,%rdi
mov    %r15d,%esi
lea    0xdf9(%rip),%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     11e0 <usb_scandir+0x60>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
mov    %rbx,%rdi
test   %rax,%rax
jne    11d0 <usb_scandir+0x50>
call   1090 <usb_checkdevice@plt>
jmp    11e0 <usb_scandir+0x60>
mov    %r12,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define void @usb_scandir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
%13 = load i8*, i8** %2
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 1, i32* %8
br label %60
17: ; preds = %1
br label %18
18: ; preds = %55, %54, %48, %38, %17
%19 = load i32*, i32** %5
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %6
%21 = icmp ne %struct.dirent* %20, null
br i1 %21, label %22, label %57
22: ; preds = %18
%23 = load %struct.dirent*, %struct.dirent** %6
%24 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp slt i32 %28, 48
br i1 %29, label %38, label %30
30: ; preds = %22
%31 = load %struct.dirent*, %struct.dirent** %6
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp sgt i32 %36, 57
br i1 %37, label %38, label %39
38: ; preds = %30, %22
br label %18
39: ; preds = %30
%40 = trunc i64 %10 to i32
%41 = load i8*, i8** %2
%42 = load %struct.dirent*, %struct.dirent** %6
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @snprintf(i8* %12, i32 %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %41, i8* %44)
%46 = call i32 @stat(i8* %12, %struct.stat* %7)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %39
br label %18
49: ; preds = %39
%50 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i64 @S_ISDIR(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
br label %18
55: ; preds = %49
%56 = call i32 @usb_checkdevice(i8* %12)
br label %18
57: ; preds = %18
%58 = load i32*, i32** %5
%59 = call i32 @closedir(i32* %58)
store i32 0, i32* %8
br label %60
60: ; preds = %57, %16
%61 = load i8*, i8** %3
%62 = load i32, i32* %8
switch i32 %62, label %64 [
i32 0, label %63
i32 1, label %63
]
63: ; preds = %60, %60
ret void
64: ; preds = %60
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
define void @usb_scandir(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast %struct.stat* %2 to i8*
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %37, label %9
9: ; preds = %1
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %14
14: ; preds = %12, %30
%15 = phi %struct.dirent* [ %10, %12 ], [ %31, %30 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
%19 = add i8 %18, -58
%20 = icmp ult i8 %19, -10
br i1 %20, label %30, label %21
21: ; preds = %14
%22 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %17)
%23 = call i32 @stat(i8* %5, %struct.stat* %2)
%24 = icmp eq i32 %23, -1
br i1 %24, label %30, label %25
25: ; preds = %21
%26 = load i32, i32* %13
%27 = call i64 @S_ISDIR(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %25
br label %30
30: ; preds = %29, %33, %21, %14
%31 = call %struct.dirent* @readdir(i32* %7)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %35, label %14
33: ; preds = %25
%34 = call i32 @usb_checkdevice(i8* %5)
br label %30
35: ; preds = %30, %9
%36 = call i32 @closedir(i32* %7)
br label %37
37: ; preds = %1, %35
ret void
}