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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
char *strchr(char *, char);
char *strdup(char *);
char *disk_name(char *path, char *target, char *shortname) {
  DIR *dir;
  struct dirent *dirent;
  char pathname[PATH_MAX];
  sprintf(pathname, "%s/%s", path, target);
  dir = opendir(pathname);
  if (!dir)
    return strdup(shortname);
  while ((dirent = readdir(dir))) {
    char line[4096], *c;
    FILE *file;
    if (dirent->d_name[0] == '.')
      continue;
    if (!strchr(dirent->d_name, ':'))
      continue;
    sprintf(line, "%s/%s/model", pathname, dirent->d_name);
    file = fopen(line, "r");
    if (file) {
      if (fgets(line, 4096, file) == NULL)
        return strdup(shortname);
      fclose(file);
      c = strchr(line, '\n');
      if (c)
        *c = 0;
      return strdup(line);
    }
  }
  closedir(dir);
  return strdup(shortname);
}


x86 assembly (clang, -O0):
<disk_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1070,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x1068(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe1b(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x1068(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121e <disk_name+0x8e>
mov    -0x20(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1370 <disk_name+0x1e0>
jmp    1223 <disk_name+0x93>
mov    -0x28(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1353 <disk_name+0x1c3>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1252 <disk_name+0xc2>
jmp    1223 <disk_name+0x93>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    1272 <disk_name+0xe2>
jmp    1223 <disk_name+0x93>
mov    -0x1068(%rbp),%rdx
lea    -0x1050(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd78(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x1050(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x1060(%rbp)
cmpq   $0x0,-0x1060(%rbp)
je     134e <disk_name+0x1be>
lea    -0x1050(%rbp),%rdi
mov    -0x1060(%rbp),%rdx
mov    $0x1000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    12f6 <disk_name+0x166>
mov    -0x20(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1370 <disk_name+0x1e0>
mov    -0x1060(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x1050(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x1058(%rbp)
cmpq   $0x0,-0x1058(%rbp)
je     1332 <disk_name+0x1a2>
mov    -0x1058(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1050(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1370 <disk_name+0x1e0>
jmp    1223 <disk_name+0x93>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disk_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%rbx
mov    %rsi,%rcx
mov    %rdi,%rdx
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe32(%rip),%rsi
mov    %r15,%rdi
call   10a0 <sprintf@plt>
mov    %r15,%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12ca <disk_name+0x13a>
mov    %rax,%r14
mov    %rbx,-0x30(%rbp)
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12aa <disk_name+0x11a>
lea    -0x1030(%rbp),%r13
jmp    1221 <disk_name+0x91>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     12aa <disk_name+0x11a>
mov    (%rax),%rbx
cmpb   $0x2e,(%rbx)
je     1210 <disk_name+0x80>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1210 <disk_name+0x80>
mov    %r13,%rdi
lea    0xdc1(%rip),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
call   10a0 <sprintf@plt>
mov    %r13,%rdi
lea    0xdb8(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1210 <disk_name+0x80>
mov    %rax,%r12
lea    -0x1030(%rbp),%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12b2 <disk_name+0x122>
mov    %r12,%rdi
call   1030 <fclose@plt>
lea    -0x1030(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12a1 <disk_name+0x111>
movb   $0x0,(%rax)
lea    -0x1030(%rbp),%rdi
jmp    12b6 <disk_name+0x126>
mov    %r14,%rdi
call   1060 <closedir@plt>
mov    -0x30(%rbp),%rdi
call   10b0 <strdup@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <strdup@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [12 x i8] c"%s/%s/model\00"
@.str.2 = [2 x i8] c"r\00"
define i8* @disk_name(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca [4096 x i8]
%14 = alloca i8*
%15 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %10
%19 = alloca i8, i64 %17
store i64 %17, i64* %11
%20 = load i8*, i8** %5
%21 = load i8*, i8** %6
%22 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %21)
%23 = call i32* @opendir(i8* %19)
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %29, label %26
26: ; preds = %3
%27 = load i8*, i8** %7
%28 = call i8* @strdup(i8* %27)
store i8* %28, i8** %4
store i32 1, i32* %12
br label %86
29: ; preds = %3
br label %30
30: ; preds = %80, %49, %42, %29
%31 = load i32*, i32** %8
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %9
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %81
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %9
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 46
br i1 %41, label %42, label %43
42: ; preds = %34
br label %30
43: ; preds = %34
%44 = load %struct.dirent*, %struct.dirent** %9
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i8* @strchr(i8* %46, i8 signext 58)
%48 = icmp ne i8* %47, null
br i1 %48, label %50, label %49
49: ; preds = %43
br label %30
50: ; preds = %43
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%52 = load %struct.dirent*, %struct.dirent** %9
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %54)
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%57 = call i32* @fopen(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %57, i32** %15
%58 = load i32*, i32** %15
%59 = icmp ne i32* %58, null
br i1 %59, label %60, label %80
60: ; preds = %50
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%62 = load i32*, i32** %15
%63 = call i32* @fgets(i8* %61, i32 4096, i32* %62)
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %60
%66 = load i8*, i8** %7
%67 = call i8* @strdup(i8* %66)
store i8* %67, i8** %4
store i32 1, i32* %12
br label %86
68: ; preds = %60
%69 = load i32*, i32** %15
%70 = call i32 @fclose(i32* %69)
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%72 = call i8* @strchr(i8* %71, i8 signext 10)
store i8* %72, i8** %14
%73 = load i8*, i8** %14
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %77
75: ; preds = %68
%76 = load i8*, i8** %14
store i8 0, i8* %76
br label %77
77: ; preds = %75, %68
%78 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%79 = call i8* @strdup(i8* %78)
store i8* %79, i8** %4
store i32 1, i32* %12
br label %86
80: ; preds = %50
br label %30
81: ; preds = %30
%82 = load i32*, i32** %8
%83 = call i32 @closedir(i32* %82)
%84 = load i8*, i8** %7
%85 = call i8* @strdup(i8* %84)
store i8* %85, i8** %4
store i32 1, i32* %12
br label %86
86: ; preds = %81, %77, %65, %26
%87 = load i8*, i8** %10
%88 = load i8*, i8** %4
ret i8* %88
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [12 x i8] c"%s/%s/model\00"
@.str.2 = [2 x i8] c"r\00"
define noalias i8* @disk_name(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca [4096 x i8]
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%9 = call i32* @opendir(i8* %7)
%10 = icmp eq i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %3
%12 = call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %48, label %14
14: ; preds = %11
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
br label %18
16: ; preds = %3
%17 = tail call i8* @strdup(i8* %2)
br label %51
18: ; preds = %14, %45
%19 = phi %struct.dirent* [ %12, %14 ], [ %46, %45 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 46
br i1 %23, label %45, label %24
24: ; preds = %18
%25 = call i8* @strchr(i8* %21, i8 signext 58)
%26 = icmp eq i8* %25, null
br i1 %26, label %45, label %27
27: ; preds = %24
%28 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %21)
%29 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %45, label %31
31: ; preds = %27
%32 = call i32* @fgets(i8* %15, i32 4096, i32* %29)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i8* @strdup(i8* %2)
br label %43
36: ; preds = %31
%37 = call i32 @fclose(i32* %29)
%38 = call i8* @strchr(i8* %15, i8 signext 10)
%39 = icmp eq i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %36
store i8 0, i8* %38
br label %41
41: ; preds = %40, %36
%42 = call i8* @strdup(i8* %15)
br label %43
43: ; preds = %34, %41
%44 = phi i8* [ %42, %41 ], [ %35, %34 ]
br label %51
45: ; preds = %27, %24, %18
%46 = call %struct.dirent* @readdir(i32* %9)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %18
48: ; preds = %45, %11
%49 = call i32 @closedir(i32* %9)
%50 = call i8* @strdup(i8* %2)
br label %51
51: ; preds = %43, %48, %16
%52 = phi i8* [ %50, %48 ], [ %17, %16 ], [ %44, %43 ]
ret i8* %52
}