#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__ {
  int gl_pathc;
  char const **gl_pathv;
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
int GLOB_NOMATCH;
int /*<<< orphan*/ GLOB_TILDE;
int /*<<< orphan*/ free(char *);
int glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
int /*<<< orphan*/ perrordie(char *);
char *safe_strcat(int, char const *, char *);
char *safe_strdup(char const *);
char *strchr(char const *, char);
size_t strlen(char const *);
char *strndup(char const *, size_t);
char *resolve_tilde(const char *path) {
  static glob_t globbuf;
  char *tail, *result = NULL;
  tail = strchr(path, '/');
  result = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
  int res = glob(result, GLOB_TILDE, NULL, &globbuf);
  free(result);
  if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1) {
    result = safe_strdup(path);
  } else if (res != 0) {
    perrordie("glob failed\n");
  } else if (tail) {
    result = safe_strcat(2, globbuf.gl_pathv[0], tail);
  } else {
    result = safe_strdup(globbuf.gl_pathv[0]);
  }
  globfree(&globbuf);
  return result;
}


x86 assembly (clang, -O0):
<resolve_tilde>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dd <resolve_tilde+0x4d>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11ea <resolve_tilde+0x5a>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strndup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    0x2e5d(%rip),%rcx
call   10b0 <glob@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1c(%rbp),%eax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
je     1243 <resolve_tilde+0xb3>
cmpl   $0x1,0x2e33(%rip)
je     1255 <resolve_tilde+0xc5>
mov    -0x8(%rbp),%rdi
call   10a0 <safe_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    12b9 <resolve_tilde+0x129>
cmpl   $0x0,-0x1c(%rbp)
je     1270 <resolve_tilde+0xe0>
lea    0xd9a(%rip),%rdi
call   1090 <perrordie@plt>
jmp    12b4 <resolve_tilde+0x124>
cmpq   $0x0,-0x10(%rbp)
je     129c <resolve_tilde+0x10c>
mov    0x2df6(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
call   1050 <safe_strcat@plt>
mov    %rax,-0x18(%rbp)
jmp    12af <resolve_tilde+0x11f>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rdi
call   10a0 <safe_strdup@plt>
mov    %rax,-0x18(%rbp)
jmp    12b4 <resolve_tilde+0x124>
jmp    12b9 <resolve_tilde+0x129>
lea    0x2db0(%rip),%rdi
call   1030 <globfree@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_tilde>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,%r14
test   %rax,%rax
je     11b4 <resolve_tilde+0x24>
mov    %r14,%rsi
sub    %r15,%rsi
jmp    11bf <resolve_tilde+0x2f>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%rsi
mov    %r15,%rdi
call   1080 <strndup@plt>
mov    %rax,%rbx
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0x2e96(%rip),%rcx
mov    %rbx,%rdi
xor    %edx,%edx
call   10b0 <glob@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    0x2dfb(%rip),%rax
cmp    (%rax),%ebp
je     1214 <resolve_tilde+0x84>
cmpl   $0x1,0x2e70(%rip)
jne    1214 <resolve_tilde+0x84>
test   %ebp,%ebp
je     1239 <resolve_tilde+0xa9>
lea    0xdf3(%rip),%rdi
call   1090 <perrordie@plt>
jmp    121f <resolve_tilde+0x8f>
mov    %r15,%rdi
call   10a0 <safe_strdup@plt>
mov    %rax,%rbx
lea    0x2e4a(%rip),%rdi
call   1030 <globfree@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e38(%rip),%rax
mov    (%rax),%rsi
test   %r14,%r14
je     1257 <resolve_tilde+0xc7>
mov    $0x2,%edi
mov    %r14,%rdx
call   1050 <safe_strcat@plt>
jmp    121c <resolve_tilde+0x8c>
mov    %rsi,%rdi
jmp    1217 <resolve_tilde+0x87>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i8** }
@resolve_tilde.globbuf = internal global %struct.TYPE_4__ zeroinitializer
@GLOB_TILDE = global i32 0
@GLOB_NOMATCH = global i32 0
@.str = [13 x i8] c"glob failed\0A\00"
define i8* @resolve_tilde(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %4
%6 = load i8*, i8** %2
%7 = call i8* @strchr(i8* %6, i8 signext 47)
store i8* %7, i8** %3
%8 = load i8*, i8** %2
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i8*, i8** %2
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
br label %20
17: ; preds = %1
%18 = load i8*, i8** %2
%19 = call i64 @strlen(i8* %18)
br label %20
20: ; preds = %17, %11
%21 = phi i64 [ %16, %11 ], [ %19, %17 ]
%22 = call i8* @strndup(i8* %8, i64 %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = load i32, i32* @GLOB_TILDE
%25 = call i32 @glob(i8* %23, i32 %24, i32* null, %struct.TYPE_4__* @resolve_tilde.globbuf)
store i32 %25, i32* %5
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
%28 = load i32, i32* %5
%29 = load i32, i32* @GLOB_NOMATCH
%30 = icmp eq i32 %28, %29
br i1 %30, label %34, label %31
31: ; preds = %20
%32 = load i32, i32* getelementptr inbounds (%struct.TYPE_4__, %struct.TYPE_4__* @resolve_tilde.globbuf, i32 0, i32 0)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %31, %20
%35 = load i8*, i8** %2
%36 = call i8* @safe_strdup(i8* %35)
store i8* %36, i8** %4
br label %58
37: ; preds = %31
%38 = load i32, i32* %5
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perrordie(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %57
42: ; preds = %37
%43 = load i8*, i8** %3
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = load i8**, i8*** getelementptr inbounds (%struct.TYPE_4__, %struct.TYPE_4__* @resolve_tilde.globbuf, i32 0, i32 1)
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = load i8*, i8** %3
%50 = call i8* @safe_strcat(i32 2, i8* %48, i8* %49)
store i8* %50, i8** %4
br label %56
51: ; preds = %42
%52 = load i8**, i8*** getelementptr inbounds (%struct.TYPE_4__, %struct.TYPE_4__* @resolve_tilde.globbuf, i32 0, i32 1)
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = call i8* @safe_strdup(i8* %54)
store i8* %55, i8** %4
br label %56
56: ; preds = %51, %45
br label %57
57: ; preds = %56, %40
br label %58
58: ; preds = %57, %34
%59 = call i32 @globfree(%struct.TYPE_4__* @resolve_tilde.globbuf)
%60 = load i8*, i8** %4
ret i8* %60
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i8** }
@resolve_tilde.globbuf = internal global %struct.TYPE_4__ zeroinitializer
@GLOB_TILDE = global i32 0
@GLOB_NOMATCH = global i32 0
@.str = [13 x i8] c"glob failed\0A\00"
define i8* @resolve_tilde(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
br label %10
8: ; preds = %1
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
br label %10
10: ; preds = %8, %4
%11 = phi i64 [ %7, %4 ], [ %9, %8 ]
%12 = tail call i8* @strndup(i8* %0, i64 %11)
%13 = load i32, i32* @GLOB_TILDE
%14 = tail call i32 @glob(i8* %12, i32 %13, i32* null, %struct.TYPE_4__* @resolve_tilde.globbuf)
%15 = tail call i32 @free(i8* %12)
%16 = load i32, i32* @GLOB_NOMATCH
%17 = icmp eq i32 %14, %16
%18 = load i32, i32* getelementptr inbounds (%struct.TYPE_4__, %struct.TYPE_4__* @resolve_tilde.globbuf, i64 0, i32 0)
%19 = icmp ne i32 %18, 1
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = tail call i8* @safe_strdup(i8* %0)
br label %34
23: ; preds = %10
%24 = icmp eq i32 %14, 0
br i1 %24, label %27, label %25
25: ; preds = %23
%26 = tail call i32 @perrordie(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %34
27: ; preds = %23
%28 = load i8**, i8*** getelementptr inbounds (%struct.TYPE_4__, %struct.TYPE_4__* @resolve_tilde.globbuf, i64 0, i32 1)
%29 = load i8*, i8** %28
br i1 %3, label %32, label %30
30: ; preds = %27
%31 = tail call i8* @safe_strcat(i32 2, i8* %29, i8* %2)
br label %34
32: ; preds = %27
%33 = tail call i8* @safe_strdup(i8* %29)
br label %34
34: ; preds = %25, %32, %30, %21
%35 = phi i8* [ %22, %21 ], [ %12, %25 ], [ %31, %30 ], [ %33, %32 ]
%36 = tail call i32 @globfree(%struct.TYPE_4__* @resolve_tilde.globbuf)
ret i8* %35
}