#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  scalar_t__ gl_pathc;
  char **gl_pathv;
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
int /*<<< orphan*/ GLOB_PERIOD;
size_t PATH_MAX;
char *dirname(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getcwd(char *, size_t);
int /*<<< orphan*/ glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *,
                        TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *,
                            char const *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int strnlen(char *, size_t);
char *cosm_lookup(const char *subpath) {
  size_t dir_len = PATH_MAX;
  char *startdir = malloc(sizeof(char) * dir_len);
  char *globstr = malloc(sizeof(char) * dir_len);
  size_t globstr_len;
  glob_t pglob;
  char *dir;
  char *target;
  char *retstr = NULL;
  size_t retstr_len = 0;
  if (getcwd(startdir, dir_len) == NULL) {
    fprintf(stderr, "cosm_lookup: cwd NULL, abort.");
    exit(-1);
  }
  dir = startdir;
  while (strncmp(dir, "/", 2) != 0) {
    globstr_len = strlen(dir) + strlen("[._][Cc]osm") + strlen(subpath) + 3;
    snprintf(globstr, globstr_len, "%s/%s/%s", dir, "[._][Cc]osm", subpath);
    glob(globstr, GLOB_PERIOD, NULL, &pglob);
    if (pglob.gl_pathc != 0)
      target = pglob.gl_pathv[0];
    else
      target = NULL;
    if (target != NULL) {
      retstr_len = strnlen(target, PATH_MAX) + 1;
      retstr = malloc(sizeof(char) * retstr_len);
      strncpy(retstr, target, retstr_len);
      globfree(&pglob);
      break;
    }
    globfree(&pglob);
    dir = dirname(dir);
  }
  free(startdir);
  free(globstr);
  return retstr;
}


x86 assembly (clang, -O0):
<cosm_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <getcwd@plt>
cmp    $0x0,%rax
jne    1256 <cosm_lookup+0x86>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xdb5(%rip),%rsi
mov    $0x2,%edx
call   1060 <strncmp@plt>
cmp    $0x0,%rax
je     138c <cosm_lookup+0x1bc>
mov    -0x40(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x60(%rbp)
lea    0xd90(%rip),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
add    %ecx,%eax
add    $0x3,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%r9
lea    0xd5d(%rip),%rdx
lea    0xd4a(%rip),%r8
call   1080 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cea(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x38(%rbp),%rcx
call   10e0 <glob@plt>
cmpq   $0x0,-0x38(%rbp)
je     1310 <cosm_lookup+0x140>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1318 <cosm_lookup+0x148>
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1371 <cosm_lookup+0x1a1>
mov    -0x48(%rbp),%rdi
mov    0x2cc2(%rip),%rax
mov    (%rax),%rsi
call   10a0 <strnlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   1050 <strncpy@plt>
lea    -0x38(%rbp),%rdi
call   1030 <globfree@plt>
jmp    138c <cosm_lookup+0x1bc>
lea    -0x38(%rbp),%rdi
call   1030 <globfree@plt>
mov    -0x40(%rbp),%rdi
call   1090 <dirname@plt>
mov    %rax,-0x40(%rbp)
jmp    125e <cosm_lookup+0x8e>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x50(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosm_lookup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r13
mov    0x2e08(%rip),%rax
mov    (%rax),%rbp
mov    %ebp,%edi
call   10d0 <malloc@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   10d0 <malloc@plt>
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
mov    %rbp,%rsi
call   10b0 <getcwd@plt>
test   %rax,%rax
je     136b <cosm_lookup+0x19b>
lea    0xe02(%rip),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
call   1060 <strncmp@plt>
test   %rax,%rax
je     1345 <cosm_lookup+0x175>
lea    0x18(%rsp),%r15
mov    %r14,0x8(%rsp)
mov    %r14,%rbp
jmp    1280 <cosm_lookup+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <globfree@plt>
mov    %rbp,%rdi
call   1090 <dirname@plt>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0xdb1(%rip),%rsi
mov    $0x2,%edx
call   1060 <strncmp@plt>
test   %rax,%rax
je     1340 <cosm_lookup+0x170>
mov    %rbp,%rdi
call   1070 <strlen@plt>
mov    %eax,%r14d
lea    0xd8e(%rip),%r12
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1070 <strlen@plt>
add    %r14d,%ebx
add    %ebx,%eax
add    $0x3,%eax
movslq %eax,%rsi
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
lea    0xd6e(%rip),%rdx
mov    %rbp,%rcx
mov    %r12,%r8
mov    %r13,%r9
call   1080 <snprintf@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
mov    %r15,%rcx
call   10e0 <glob@plt>
cmpq   $0x0,0x18(%rsp)
je     1250 <cosm_lookup+0x80>
mov    0x20(%rsp),%rax
mov    (%rax),%r14
test   %r14,%r14
je     1250 <cosm_lookup+0x80>
mov    0x2cea(%rip),%rax
mov    (%rax),%rsi
mov    %r14,%rdi
call   10a0 <strnlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    %ebx,%edi
call   10d0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <strncpy@plt>
lea    0x18(%rsp),%rdi
call   1030 <globfree@plt>
mov    0x8(%rsp),%r14
jmp    1347 <cosm_lookup+0x177>
mov    0x8(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
call   1040 <free@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    %rbp,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
lea    0xc85(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i64, i8** }
@PATH_MAX = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"cosm_lookup: cwd NULL, abort.\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [12 x i8] c"[._][Cc]osm\00"
@.str.3 = [9 x i8] c"%s/%s/%s\00"
@GLOB_PERIOD = global i32 0
define i8* @cosm_lookup(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.TYPE_4__
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %2
%12 = load i64, i64* @PATH_MAX
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %4
%17 = load i64, i64* %3
%18 = mul i64 1, %17
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %5
store i8* null, i8** %10
store i64 0, i64* %11
%21 = load i8*, i8** %4
%22 = load i64, i64* %3
%23 = call i32* @getcwd(i8* %21, i64 %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %1
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %1
%30 = load i8*, i8** %4
store i8* %30, i8** %8
br label %31
31: ; preds = %80, %29
%32 = load i8*, i8** %8
%33 = call i64 @strncmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %84
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%39 = add nsw i32 %37, %38
%40 = load i8*, i8** %2
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %39, %41
%43 = add nsw i32 %42, 3
%44 = sext i32 %43 to i64
store i64 %44, i64* %6
%45 = load i8*, i8** %5
%46 = load i64, i64* %6
%47 = load i8*, i8** %8
%48 = load i8*, i8** %2
%49 = call i32 @snprintf(i8* %45, i64 %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %47, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i8*, i8** %5
%51 = load i32, i32* @GLOB_PERIOD
%52 = call i32 @glob(i8* %50, i32 %51, i32* null, %struct.TYPE_4__* %7)
%53 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 0
%54 = load i64, i64* %53
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %35
%57 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 1
%58 = load i8**, i8*** %57
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
store i8* %60, i8** %9
br label %62
61: ; preds = %35
store i8* null, i8** %9
br label %62
62: ; preds = %61, %56
%63 = load i8*, i8** %9
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %80
65: ; preds = %62
%66 = load i8*, i8** %9
%67 = load i64, i64* @PATH_MAX
%68 = call i32 @strnlen(i8* %66, i64 %67)
%69 = add nsw i32 %68, 1
%70 = sext i32 %69 to i64
store i64 %70, i64* %11
%71 = load i64, i64* %11
%72 = mul i64 1, %71
%73 = trunc i64 %72 to i32
%74 = call i8* @malloc(i32 %73)
store i8* %74, i8** %10
%75 = load i8*, i8** %10
%76 = load i8*, i8** %9
%77 = load i64, i64* %11
%78 = call i32 @strncpy(i8* %75, i8* %76, i64 %77)
%79 = call i32 @globfree(%struct.TYPE_4__* %7)
br label %84
80: ; preds = %62
%81 = call i32 @globfree(%struct.TYPE_4__* %7)
%82 = load i8*, i8** %8
%83 = call i8* @dirname(i8* %82)
store i8* %83, i8** %8
br label %31
84: ; preds = %65, %31
%85 = load i8*, i8** %4
%86 = call i32 @free(i8* %85)
%87 = load i8*, i8** %5
%88 = call i32 @free(i8* %87)
%89 = load i8*, i8** %10
ret i8* %89
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i64, i8** }
@PATH_MAX = global i64 0
@stderr = global i32 0
@.str = [30 x i8] c"cosm_lookup: cwd NULL, abort.\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [12 x i8] c"[._][Cc]osm\00"
@.str.3 = [9 x i8] c"%s/%s/%s\00"
@GLOB_PERIOD = global i32 0
define i8* @cosm_lookup(i8* %0) {
%2 = alloca %struct.TYPE_4__
%3 = load i64, i64* @PATH_MAX
%4 = trunc i64 %3 to i32
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i8* @malloc(i32 %4)
%7 = bitcast %struct.TYPE_4__* %2 to i8*
%8 = tail call i32* @getcwd(i8* %5, i64 %3)
%9 = icmp eq i32* %8, null
br i1 %9, label %16, label %10
10: ; preds = %1
%11 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %51, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 1
br label %20
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %13, %46
%21 = phi i8* [ %5, %13 ], [ %48, %46 ]
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @strlen(i8* %0)
%25 = add i32 %22, 3
%26 = add i32 %25, %23
%27 = add i32 %26, %24
%28 = sext i32 %27 to i64
%29 = call i32 @snprintf(i8* %6, i64 %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%30 = load i32, i32* @GLOB_PERIOD
%31 = call i32 @glob(i8* %6, i32 %30, i32* null, %struct.TYPE_4__* %2)
%32 = load i64, i64* %14
%33 = icmp eq i64 %32, 0
br i1 %33, label %46, label %34
34: ; preds = %20
%35 = load i8**, i8*** %15
%36 = load i8*, i8** %35
%37 = icmp eq i8* %36, null
br i1 %37, label %46, label %38
38: ; preds = %34
%39 = load i64, i64* @PATH_MAX
%40 = call i32 @strnlen(i8* %36, i64 %39)
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = call i8* @malloc(i32 %41)
%44 = call i32 @strncpy(i8* %43, i8* %36, i64 %42)
%45 = call i32 @globfree(%struct.TYPE_4__* %2)
br label %51
46: ; preds = %20, %34
%47 = call i32 @globfree(%struct.TYPE_4__* %2)
%48 = call i8* @dirname(i8* %21)
%49 = call i64 @strncmp(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %20
51: ; preds = %46, %10, %38
%52 = phi i8* [ %43, %38 ], [ null, %10 ], [ null, %46 ]
%53 = call i32 @free(i8* %5)
%54 = call i32 @free(i8* %6)
ret i8* %52
}