#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 const *pw_dir;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ geteuid();
struct passwd *getpwnam(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char const *);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *cfg_tilde_expand(const char *filename) {
  char *expanded = 0;
  if (filename[0] == '~') {
    struct passwd *passwd = 0;
    const char *file = 0;
    if (filename[1] == '/' || filename[1] == 0) {
      passwd = getpwuid(geteuid());
      file = filename + 1;
    } else {
      char *user;
      file = strchr(filename, '/');
      if (file == 0)
        file = filename + strlen(filename);
      user = malloc(file - filename);
      strncpy(user, filename + 1, file - filename - 1);
      passwd = getpwnam(user);
      free(user);
    }
    if (passwd) {
      expanded = malloc(strlen(passwd->pw_dir) + strlen(file) + 1);
      strcpy(expanded, passwd->pw_dir);
      strcat(expanded, file);
    }
  }
  if (!expanded)
    expanded = strdup(filename);
  return expanded;
}


x86 assembly (clang, -O0):
<cfg_tilde_expand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    130b <cfg_tilde_expand+0x15b>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
je     1206 <cfg_tilde_expand+0x56>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1229 <cfg_tilde_expand+0x79>
mov    $0x0,%al
call   1090 <geteuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    12b5 <cfg_tilde_expand+0x105>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1267 <cfg_tilde_expand+0xb7>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x1,%rsi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
je     1306 <cfg_tilde_expand+0x156>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x34(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1050 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10c0 <strcat@plt>
jmp    130b <cfg_tilde_expand+0x15b>
cmpq   $0x0,-0x10(%rbp)
jne    1323 <cfg_tilde_expand+0x173>
mov    -0x8(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfg_tilde_expand>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
cmpb   $0x7e,(%rdi)
jne    129f <cfg_tilde_expand+0xef>
lea    0x1(%r13),%r14
mov    0x1(%r13),%al
cmp    $0x2f,%al
je     11d7 <cfg_tilde_expand+0x27>
test   %al,%al
jne    11f2 <cfg_tilde_expand+0x42>
xor    %eax,%eax
call   1090 <geteuid@plt>
mov    %eax,%edi
call   1060 <getpwuid@plt>
mov    %rax,%r12
test   %r12,%r12
jne    1250 <cfg_tilde_expand+0xa0>
jmp    129f <cfg_tilde_expand+0xef>
mov    %r13,%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1215 <cfg_tilde_expand+0x65>
mov    %r13,%rdi
call   1070 <strlen@plt>
movslq %eax,%r15
add    %r13,%r15
mov    %r15d,%ebx
sub    %r13d,%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
add    $0xffffffff,%ebx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
call   10a0 <getpwnam@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%r14
test   %r12,%r12
je     129f <cfg_tilde_expand+0xef>
mov    (%r12),%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    (%r12),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <strcat@plt>
test   %rbx,%rbx
je     129f <cfg_tilde_expand+0xef>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <strdup@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
define i8* @cfg_tilde_expand(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
%7 = load i8*, i8** %2
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 126
br i1 %11, label %12, label %85
12: ; preds = %1
store %struct.passwd* null, %struct.passwd** %4
store i8* null, i8** %5
%13 = load i8*, i8** %2
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 47
br i1 %17, label %24, label %18
18: ; preds = %12
%19 = load i8*, i8** %2
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18, %12
%25 = call i32 (...) @geteuid()
%26 = call %struct.passwd* @getpwuid(i32 %25)
store %struct.passwd* %26, %struct.passwd** %4
%27 = load i8*, i8** %2
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8* %28, i8** %5
br label %63
29: ; preds = %18
%30 = load i8*, i8** %2
%31 = call i8* @strchr(i8* %30, i8 signext 47)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i8*, i8** %2
%36 = load i8*, i8** %2
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8* %39, i8** %5
br label %40
40: ; preds = %34, %29
%41 = load i8*, i8** %5
%42 = load i8*, i8** %2
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** %6
%48 = load i8*, i8** %6
%49 = load i8*, i8** %2
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = load i8*, i8** %5
%52 = load i8*, i8** %2
%53 = ptrtoint i8* %51 to i64
%54 = ptrtoint i8* %52 to i64
%55 = sub i64 %53, %54
%56 = sub nsw i64 %55, 1
%57 = trunc i64 %56 to i32
%58 = call i32 @strncpy(i8* %48, i8* %50, i32 %57)
%59 = load i8*, i8** %6
%60 = call %struct.passwd* @getpwnam(i8* %59)
store %struct.passwd* %60, %struct.passwd** %4
%61 = load i8*, i8** %6
%62 = call i32 @free(i8* %61)
br label %63
63: ; preds = %40, %24
%64 = load %struct.passwd*, %struct.passwd** %4
%65 = icmp ne %struct.passwd* %64, null
br i1 %65, label %66, label %84
66: ; preds = %63
%67 = load %struct.passwd*, %struct.passwd** %4
%68 = getelementptr inbounds %struct.passwd, %struct.passwd* %67, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = call i32 @strlen(i8* %69)
%71 = load i8*, i8** %5
%72 = call i32 @strlen(i8* %71)
%73 = add nsw i32 %70, %72
%74 = add nsw i32 %73, 1
%75 = call i8* @malloc(i32 %74)
store i8* %75, i8** %3
%76 = load i8*, i8** %3
%77 = load %struct.passwd*, %struct.passwd** %4
%78 = getelementptr inbounds %struct.passwd, %struct.passwd* %77, i32 0, i32 0
%79 = load i8*, i8** %78
%80 = call i32 @strcpy(i8* %76, i8* %79)
%81 = load i8*, i8** %3
%82 = load i8*, i8** %5
%83 = call i32 @strcat(i8* %81, i8* %82)
br label %84
84: ; preds = %66, %63
br label %85
85: ; preds = %84, %1
%86 = load i8*, i8** %3
%87 = icmp ne i8* %86, null
br i1 %87, label %91, label %88
88: ; preds = %85
%89 = load i8*, i8** %2
%90 = call i8* @strdup(i8* %89)
store i8* %90, i8** %3
br label %91
91: ; preds = %88, %85
%92 = load i8*, i8** %3
ret i8* %92
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
define i8* @cfg_tilde_expand(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %44
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
switch i8 %6, label %10 [
i8 47, label %7
i8 0, label %7
]
7: ; preds = %4, %4
%8 = tail call i32 (...) @geteuid()
%9 = tail call %struct.passwd* @getpwuid(i32 %8)
br label %28
10: ; preds = %4
%11 = tail call i8* @strchr(i8* %0, i8 signext 47)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @strlen(i8* %0)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
br label %17
17: ; preds = %13, %10
%18 = phi i8* [ %16, %13 ], [ %11, %10 ]
%19 = ptrtoint i8* %18 to i64
%20 = ptrtoint i8* %0 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = tail call i8* @malloc(i32 %22)
%24 = add i32 %22, -1
%25 = tail call i32 @strncpy(i8* %23, i8* %5, i32 %24)
%26 = tail call %struct.passwd* @getpwnam(i8* %23)
%27 = tail call i32 @free(i8* %23)
br label %28
28: ; preds = %17, %7
%29 = phi %struct.passwd* [ %9, %7 ], [ %26, %17 ]
%30 = phi i8* [ %5, %7 ], [ %18, %17 ]
%31 = icmp eq %struct.passwd* %29, null
br i1 %31, label %44, label %32
32: ; preds = %28
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %29, i64 0, i32 0
%34 = load i8*, i8** %33
%35 = tail call i32 @strlen(i8* %34)
%36 = tail call i32 @strlen(i8* %30)
%37 = add i32 %35, 1
%38 = add i32 %37, %36
%39 = tail call i8* @malloc(i32 %38)
%40 = load i8*, i8** %33
%41 = tail call i32 @strcpy(i8* %39, i8* %40)
%42 = tail call i32 @strcat(i8* %39, i8* %30)
%43 = icmp eq i8* %39, null
br i1 %43, label %44, label %46
44: ; preds = %28, %1, %32
%45 = tail call i8* @strdup(i8* %0)
br label %46
46: ; preds = %44, %32
%47 = phi i8* [ %39, %32 ], [ %45, %44 ]
ret i8* %47
}