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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
char *strstr(char *, char const *);
char *modem_get_iface_dev(const char *port, const char *dev_type, int iface,
                          char *dev, int dev_len) {
  char *s, *res = NULL;
  struct dirent *item;
  char path[0x100];
  DIR *dir;
  int i, j;
  snprintf(path, sizeof(path), "/sys/bus/usb/devices/%s:1.%d/", port, iface);
  if ((dir = opendir(path)) == NULL)
    return (res);
  while ((item = readdir(dir))) {
    if ((s = strstr(item->d_name, dev_type)) == item->d_name) {
      snprintf(dev, dev_len - 1, "/dev/%s", s);
      res = dev;
      break;
    }
  }
  closedir(dir);
  return (res);
}


x86 assembly (clang, -O0):
<modem_get_iface_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x150(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    $0x100,%esi
lea    0xe6f(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x150(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x158(%rbp)
cmp    $0x0,%rax
jne    11c2 <modem_get_iface_dev+0x72>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1244 <modem_get_iface_dev+0xf4>
jmp    11c7 <modem_get_iface_dev+0x77>
mov    -0x158(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
je     1230 <modem_get_iface_dev+0xe0>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x48(%rbp),%rcx
cmp    (%rcx),%rax
jne    122b <modem_get_iface_dev+0xdb>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
sub    $0x1,%esi
mov    -0x38(%rbp),%rcx
lea    0xe07(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1230 <modem_get_iface_dev+0xe0>
jmp    11c7 <modem_get_iface_dev+0x77>
mov    -0x158(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<modem_get_iface_dev>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %edx,%r8d
mov    %rsi,%rbx
mov    %rdi,%rcx
lea    0xe7b(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     11f7 <modem_get_iface_dev+0x97>
mov    %rax,%r12
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11fc <modem_get_iface_dev+0x9c>
mov    (%rax),%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11b0 <modem_get_iface_dev+0x50>
add    $0xffffffff,%r14d
lea    0xe39(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    11ff <modem_get_iface_dev+0x9f>
xor    %r15d,%r15d
jmp    1207 <modem_get_iface_dev+0xa7>
xor    %r15d,%r15d
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    %r15,%rax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [30 x i8] c"/sys/bus/usb/devices/%s:1.%d/\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
define i8* @modem_get_iface_dev(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca %struct.dirent*
%15 = alloca [256 x i8]
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i8* null, i8** %13
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%20 = load i8*, i8** %7
%21 = load i32, i32* %9
%22 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %19, i32 256, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %15, i64 0, i64 0
%24 = call i32* @opendir(i8* %23)
store i32* %24, i32** %16
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %5
%27 = load i8*, i8** %13
store i8* %27, i8** %6
br label %55
28: ; preds = %5
br label %29
29: ; preds = %50, %28
%30 = load i32*, i32** %16
%31 = call %struct.dirent* @readdir(i32* %30)
store %struct.dirent* %31, %struct.dirent** %14
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %51
33: ; preds = %29
%34 = load %struct.dirent*, %struct.dirent** %14
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = load i8*, i8** %8
%38 = call i8* @strstr(i8* %36, i8* %37)
store i8* %38, i8** %12
%39 = load %struct.dirent*, %struct.dirent** %14
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = icmp eq i8* %38, %41
br i1 %42, label %43, label %50
43: ; preds = %33
%44 = load i8*, i8** %10
%45 = load i32, i32* %11
%46 = sub nsw i32 %45, 1
%47 = load i8*, i8** %12
%48 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %44, i32 %46, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = load i8*, i8** %10
store i8* %49, i8** %13
br label %51
50: ; preds = %33
br label %29
51: ; preds = %43, %29
%52 = load i32*, i32** %16
%53 = call i32 @closedir(i32* %52)
%54 = load i8*, i8** %13
store i8* %54, i8** %6
br label %55
55: ; preds = %51, %26
%56 = load i8*, i8** %6
ret i8* %56
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [30 x i8] c"/sys/bus/usb/devices/%s:1.%d/\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
define i8* @modem_get_iface_dev(i8* %0, i8* nocapture %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%8 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %2)
%9 = call i32* @opendir(i8* %7)
%10 = icmp eq i32* %9, null
br i1 %10, label %26, label %11
11: ; preds = %5, %14
%12 = call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strlen(i8* dereferenceable(1) %1)
%18 = call i32 @strncmp(i8* %16, i8* %1, i64 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %11
20: ; preds = %14
%21 = add nsw i32 %4, -1
%22 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %3, i32 %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %23
23: ; preds = %11, %20
%24 = phi i8* [ %3, %20 ], [ null, %11 ]
%25 = call i32 @closedir(i32* %9)
br label %26
26: ; preds = %5, %23
%27 = phi i8* [ %24, %23 ], [ null, %5 ]
ret i8* %27
}