#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 {
  char *d_name;
};
struct devinfo_head {
  int dummy;
};
struct dev_head {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ usb_scan_dev(struct dev_head *, struct devinfo_head const *,
                                char *);
int usb_scan_dir(struct dev_head *result, const struct devinfo_head *sel,
                 const char *dir) {
  DIR *d;
  struct dirent *ent;
  char fn[512];
  d = opendir(dir);
  if (!d)
    return 0;
  while ((ent = readdir(d))) {
    if (ent->d_name[0] != '.') {
      sprintf(fn, "%s/%s", dir, ent->d_name);
      usb_scan_dev(result, sel, fn);
    }
  }
  closedir(d);
  return 0;
}


x86 assembly (clang, -O0):
<usb_scan_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118b <usb_scan_dir+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    1201 <usb_scan_dir+0xb1>
jmp    1190 <usb_scan_dir+0x40>
mov    -0x28(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11f1 <usb_scan_dir+0xa1>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     11ec <usb_scan_dir+0x9c>
lea    -0x230(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xe2d(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x230(%rbp),%rdx
call   1040 <usb_scan_dev@plt>
jmp    1190 <usb_scan_dir+0x40>
mov    -0x28(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usb_scan_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11db <usb_scan_dir+0x8b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d3 <usb_scan_dir+0x83>
lea    0xe72(%rip),%r13
mov    %rsp,%rbp
jmp    11ad <usb_scan_dir+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11d3 <usb_scan_dir+0x83>
mov    (%rax),%rcx
cmpb   $0x2e,(%rcx)
je     11a0 <usb_scan_dir+0x50>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1070 <sprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1040 <usb_scan_dev@plt>
jmp    11a0 <usb_scan_dir+0x50>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dev_head = type { i32 }
%struct.devinfo_head = type { i32 }
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define i32 @usb_scan_dir(%struct.dev_head* %0, %struct.devinfo_head* %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @opendir(i8* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %23, label %11
11: ; preds = %8, %20
%12 = phi %struct.dirent* [ %21, %20 ], [ %9, %8 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 46
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %14)
%19 = call i32 @usb_scan_dev(%struct.dev_head* %0, %struct.devinfo_head* %1, i8* %5)
br label %20
20: ; preds = %17, %11
%21 = call %struct.dirent* @readdir(i32* %6)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %23, label %11
23: ; preds = %20, %8
%24 = call i32 @closedir(i32* %6)
br label %25
25: ; preds = %3, %23
ret i32 0
}