#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 passwd {
  char *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ EX_IOERR;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ asprintf(char **, char *, char *, char const *);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
struct passwd *getpwnam(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *strchr(char *, char);
char *strdup(char const *);
char *texpand(const char *s) {
  if (!s || s[0] == '\0') {
    return texpand("~/var/cache/pamei");
  } else if (s[0] == '~') {
    char *out;
    struct passwd *p;
    if (s[1] == '/' || s[1] == '\0') {
      char *home;
      if (!(home = getenv("HOME"))) {
        if (!(p = getpwuid(getuid())))
          err(EX_IOERR, "cannot find HOME directory");
        home = p->pw_dir;
      }
      asprintf(&out, "%s%s", home, &s[1]);
    } else {
      char *user = strdup(&s[1]);
      char *end = strchr(user, '/');
      if (end)
        *end = '\0';
      if (!(p = getpwnam(user)))
        err(EX_OSERR, "cannot lookup user %s", user);
      free(user);
      asprintf(&out, "%s%s", p->pw_dir, end ? "" : s + (end - user + 1));
    }
    return out;
  } else {
    return strdup(s);
  }
}


x86 assembly (clang, -O0):
<texpand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c7 <texpand+0x27>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11dc <texpand+0x3c>
lea    0xe32(%rip),%rdi
call   1090 <texpand@plt>
mov    %rax,-0x8(%rbp)
jmp    1369 <texpand+0x1c9>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    135c <texpand+0x1bc>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
je     120e <texpand+0x6e>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1287 <texpand+0xe7>
lea    0xdfd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1266 <texpand+0xc6>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    125b <texpand+0xbb>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
lea    -0x18(%rbp),%rdi
lea    0xdb5(%rip),%rsi
call   10b0 <asprintf@plt>
jmp    134f <texpand+0x1af>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12bc <texpand+0x11c>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
call   1080 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12ee <texpand+0x14e>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     131d <texpand+0x17d>
lea    0xd02(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1337 <texpand+0x197>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rdx
sub    %rdx,%rcx
add    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    -0x18(%rbp),%rdi
lea    0xce8(%rip),%rsi
call   10b0 <asprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1369 <texpand+0x1c9>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<texpand>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe61(%rip),%rax
nop
mov    %rdi,%rbx
mov    %rax,%rdi
test   %rbx,%rbx
je     11a0 <texpand+0x10>
movzbl (%rbx),%ecx
mov    %rax,%rdi
test   %cl,%cl
je     11a0 <texpand+0x10>
cmp    $0x7e,%cl
jne    121f <texpand+0x8f>
lea    0x1(%rbx),%r14
mov    0x1(%rbx),%al
cmp    $0x2f,%al
je     11c9 <texpand+0x39>
test   %al,%al
jne    1232 <texpand+0xa2>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
jne    120a <texpand+0x7a>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1207 <texpand+0x77>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
mov    (%rbx),%rax
lea    0xe21(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
mov    %r14,%rcx
jmp    12ab <texpand+0x11b>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    10b0 <strdup@plt>
mov    %r14,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,%r15
test   %rax,%rax
je     1256 <texpand+0xc6>
movb   $0x0,(%r15)
mov    %r14,%rdi
call   1080 <getpwnam@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1280 <texpand+0xf0>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <err@plt>
mov    %r14,%rdi
call   1040 <free@plt>
sub    %r14,%rbx
add    $0x1,%rbx
test   %r15,%r15
mov    (%r12),%rdx
lea    0xd79(%rip),%rcx
cmove  %rbx,%rcx
lea    0xd8a(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <asprintf@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [18 x i8] c"~/var/cache/pamei\00"
@.str.1 = [5 x i8] c"HOME\00"
@EX_IOERR = global i32 0
@.str.2 = [27 x i8] c"cannot find HOME directory\00"
@.str.3 = [5 x i8] c"%s%s\00"
@EX_OSERR = global i32 0
@.str.4 = [22 x i8] c"cannot lookup user %s\00"
@.str.5 = [1 x i8] zeroinitializer
define i8* @texpand(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.passwd*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
%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 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11, %1
%18 = call i8* @texpand(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i8* %18, i8** %2
br label %100
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 126
br i1 %24, label %25, label %97
25: ; preds = %19
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 47
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %56
37: ; preds = %31, %25
%38 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %38, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %51, label %40
40: ; preds = %37
%41 = call i32 (...) @getuid()
%42 = call %struct.passwd* @getpwuid(i32 %41)
store %struct.passwd* %42, %struct.passwd** %5
%43 = icmp ne %struct.passwd* %42, null
br i1 %43, label %47, label %44
44: ; preds = %40
%45 = load i32, i32* @EX_IOERR
%46 = call i32 (i32, i8*, ...) @err(i32 %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %44, %40
%48 = load %struct.passwd*, %struct.passwd** %5
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 0
%50 = load i8*, i8** %49
store i8* %50, i8** %6
br label %51
51: ; preds = %47, %37
%52 = load i8*, i8** %6
%53 = load i8*, i8** %3
%54 = getelementptr inbounds i8, i8* %53, i64 1
%55 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %52, i8* %54)
br label %95
56: ; preds = %31
%57 = load i8*, i8** %3
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = call i8* @strdup(i8* %58)
store i8* %59, i8** %7
%60 = load i8*, i8** %7
%61 = call i8* @strchr(i8* %60, i8 signext 47)
store i8* %61, i8** %8
%62 = load i8*, i8** %8
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %66
64: ; preds = %56
%65 = load i8*, i8** %8
store i8 0, i8* %65
br label %66
66: ; preds = %64, %56
%67 = load i8*, i8** %7
%68 = call %struct.passwd* @getpwnam(i8* %67)
store %struct.passwd* %68, %struct.passwd** %5
%69 = icmp ne %struct.passwd* %68, null
br i1 %69, label %74, label %70
70: ; preds = %66
%71 = load i32, i32* @EX_OSERR
%72 = load i8*, i8** %7
%73 = call i32 (i32, i8*, ...) @err(i32 %71, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %70, %66
%75 = load i8*, i8** %7
%76 = call i32 @free(i8* %75)
%77 = load %struct.passwd*, %struct.passwd** %5
%78 = getelementptr inbounds %struct.passwd, %struct.passwd* %77, i32 0, i32 0
%79 = load i8*, i8** %78
%80 = load i8*, i8** %8
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %83
82: ; preds = %74
br label %92
83: ; preds = %74
%84 = load i8*, i8** %3
%85 = load i8*, i8** %8
%86 = load i8*, i8** %7
%87 = ptrtoint i8* %85 to i64
%88 = ptrtoint i8* %86 to i64
%89 = sub i64 %87, %88
%90 = add nsw i64 %89, 1
%91 = getelementptr inbounds i8, i8* %84, i64 %90
br label %92
92: ; preds = %83, %82
%93 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), %82 ], [ %91, %83 ]
%94 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %79, i8* %93)
br label %95
95: ; preds = %92, %51
%96 = load i8*, i8** %4
store i8* %96, i8** %2
br label %100
97: ; preds = %19
%98 = load i8*, i8** %3
%99 = call i8* @strdup(i8* %98)
store i8* %99, i8** %2
br label %100
100: ; preds = %97, %95, %17
%101 = load i8*, i8** %2
ret i8* %101
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [18 x i8] c"~/var/cache/pamei\00"
@.str.1 = [5 x i8] c"HOME\00"
@EX_IOERR = global i32 0
@.str.2 = [27 x i8] c"cannot find HOME directory\00"
@.str.3 = [5 x i8] c"%s%s\00"
@EX_OSERR = global i32 0
@.str.4 = [22 x i8] c"cannot lookup user %s\00"
@.str.5 = [1 x i8] zeroinitializer
define i8* @texpand(i8* %0) {
%2 = alloca i8*
br label %3
3: ; preds = %8, %1
%4 = phi i8* [ %0, %1 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %8 ]
%5 = icmp eq i8* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = load i8, i8* %4
switch i8 %7, label %51 [
i8 0, label %8
i8 126, label %9
]
8: ; preds = %6, %3
br label %3
9: ; preds = %6
%10 = bitcast i8** %2 to i8*
%11 = getelementptr inbounds i8, i8* %4, i64 1
%12 = load i8, i8* %11
switch i8 %12, label %29 [
i8 47, label %13
i8 0, label %13
]
13: ; preds = %9, %9
%14 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %26
16: ; preds = %13
%17 = tail call i32 (...) @getuid()
%18 = tail call %struct.passwd* @getpwuid(i32 %17)
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @EX_IOERR
%22 = tail call i32 (i32, i8*, ...) @err(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %20, %16
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 0
%25 = load i8*, i8** %24
br label %26
26: ; preds = %23, %13
%27 = phi i8* [ %14, %13 ], [ %25, %23 ]
%28 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %27, i8* %11)
br label %49
29: ; preds = %9
%30 = tail call i8* @strdup(i8* %11)
%31 = tail call i8* @strchr(i8* dereferenceable(1) %30, i8 signext 47)
%32 = icmp eq i8* %31, null
br i1 %32, label %34, label %33
33: ; preds = %29
store i8 0, i8* %31
br label %34
34: ; preds = %33, %29
%35 = tail call %struct.passwd* @getpwnam(i8* %30)
%36 = icmp eq %struct.passwd* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @EX_OSERR
%39 = tail call i32 (i32, i8*, ...) @err(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %30)
br label %40
40: ; preds = %37, %34
%41 = tail call i32 @free(i8* %30)
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = ptrtoint i8* %30 to i64
%45 = sub i64 1, %44
%46 = getelementptr inbounds i8, i8* %4, i64 %45
%47 = select i1 %32, i8* %46, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0)
%48 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %43, i8* %47)
br label %49
49: ; preds = %40, %26
%50 = load i8*, i8** %2
br label %53
51: ; preds = %6
%52 = tail call i8* @strdup(i8* %4)
br label %53
53: ; preds = %51, %49
%54 = phi i8* [ %50, %49 ], [ %52, %51 ]
ret i8* %54
}