#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*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
char *disk_name(char *, char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strchr(char *, char);
char *strdup(char *);
int /*<<< orphan*/ strstr(char *, char *);
char *model_name(char *path, char *shortname) {
  DIR *dir;
  struct dirent *dirent;
  char pathname[PATH_MAX];
  sprintf(pathname, "%s/device", path);
  dir = opendir(pathname);
  if (!dir)
    return strdup(shortname);
  while ((dirent = readdir(dir))) {
    if (dirent->d_name[0] == '.')
      continue;
    if (!strchr(dirent->d_name, ':'))
      continue;
    if (!strstr(dirent->d_name, "target"))
      continue;
    return disk_name(pathname, dirent->d_name, shortname);
  }
  closedir(dir);
  return strdup(shortname);
}


x86 assembly (clang, -O0):
<model_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fd <model_name+0x7d>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12b2 <model_name+0x132>
jmp    1202 <model_name+0x82>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1295 <model_name+0x115>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1231 <model_name+0xb1>
jmp    1202 <model_name+0x82>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
cmp    $0x0,%eax
jne    1250 <model_name+0xd0>
jmp    1202 <model_name+0x82>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xdac(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%eax
jne    1271 <model_name+0xf1>
jmp    1202 <model_name+0x82>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <disk_name@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12b2 <model_name+0x132>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<model_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rdx
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe4b(%rip),%rsi
mov    %r15,%rdi
call   1080 <sprintf@plt>
mov    %r15,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     124a <model_name+0xca>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1242 <model_name+0xc2>
mov    %rax,%rbx
lea    0xe22(%rip),%r13
jmp    1200 <model_name+0x80>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1242 <model_name+0xc2>
mov    (%rbx),%rdi
cmpb   $0x2e,(%rdi)
je     11f0 <model_name+0x70>
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %eax,%eax
je     11f0 <model_name+0x70>
mov    (%rbx),%rdi
mov    %r13,%rsi
call   10a0 <strstr@plt>
test   %eax,%eax
je     11f0 <model_name+0x70>
mov    (%rbx),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1050 <disk_name@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1060 <closedir@plt>
mov    %r14,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <strdup@plt>

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

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