#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*/ _p11_message(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
char *getenv(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
char *strconcat(char const *, char const *, int /*<<< orphan*/ *);
char *strdup(char const *);
int /*<<< orphan*/ strerror(int);
char *expand_user_path(const char *path) {
  const char *env;
  struct passwd *pwd;
  int error = 0;
  if (path[0] == '~' && path[1] == '/') {
    env = getenv("HOME");
    if (env && env[0]) {
      return strconcat(env, path + 1, NULL);
    } else {
      pwd = getpwuid(getuid());
      if (!pwd) {
        error = errno;
        _p11_message("couldn't lookup home directory for user %d: %s", getuid(),
                     strerror(errno));
        errno = error;
        return NULL;
      }
      return strconcat(pwd->pw_dir, path + 1, NULL);
    }
  }
  return strdup(path);
}


x86 assembly (clang, -O0):
<expand_user_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    1279 <expand_user_path+0x109>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1279 <expand_user_path+0x109>
lea    0xe55(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ed <expand_user_path+0x7d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ed <expand_user_path+0x7d>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <strconcat@plt>
mov    %rax,-0x8(%rbp)
jmp    1286 <expand_user_path+0x116>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1258 <expand_user_path+0xe8>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,-0x28(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xdcb(%rip),%rdi
call   1070 <_p11_message@plt>
mov    -0x24(%rbp),%ecx
mov    0x2d97(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1286 <expand_user_path+0x116>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <strconcat@plt>
mov    %rax,-0x8(%rbp)
jmp    1286 <expand_user_path+0x116>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_user_path>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
cmpb   $0x7e,(%rdi)
jne    11a1 <expand_user_path+0x31>
cmpb   $0x2f,0x1(%rbx)
jne    11a1 <expand_user_path+0x31>
add    $0x1,%rbx
lea    0xe73(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ad <expand_user_path+0x3d>
cmpb   $0x0,(%rax)
je     11ad <expand_user_path+0x3d>
mov    %rax,%rdi
jmp    11c3 <expand_user_path+0x53>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <strdup@plt>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
test   %rax,%rax
je     11d1 <expand_user_path+0x61>
mov    (%rax),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <strconcat@plt>
mov    0x2e08(%rip),%r14
mov    (%r14),%ebp
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%ebx
mov    (%r14),%edi
call   1090 <strerror@plt>
lea    0xe12(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1070 <_p11_message@plt>
mov    %ebp,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"couldn't lookup home directory for user %d: %s\00"
define i8* @expand_user_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.passwd*
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
%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 %52
12: ; preds = %1
%13 = load i8*, i8** %3
%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 %18, label %52
18: ; preds = %12
%19 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i8*, i8** %4
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = call i8* @strconcat(i8* %29, i8* %31, i32* null)
store i8* %32, i8** %2
br label %55
33: ; preds = %22, %18
%34 = call i32 (...) @getuid()
%35 = call %struct.passwd* @getpwuid(i32 %34)
store %struct.passwd* %35, %struct.passwd** %5
%36 = load %struct.passwd*, %struct.passwd** %5
%37 = icmp ne %struct.passwd* %36, null
br i1 %37, label %45, label %38
38: ; preds = %33
%39 = load i32, i32* @errno
store i32 %39, i32* %6
%40 = call i32 (...) @getuid()
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 @_p11_message(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %42)
%44 = load i32, i32* %6
store i32 %44, i32* @errno
store i8* null, i8** %2
br label %55
45: ; preds = %33
%46 = load %struct.passwd*, %struct.passwd** %5
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = load i8*, i8** %3
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = call i8* @strconcat(i8* %48, i8* %50, i32* null)
store i8* %51, i8** %2
br label %55
52: ; preds = %12, %1
%53 = load i8*, i8** %3
%54 = call i8* @strdup(i8* %53)
store i8* %54, i8** %2
br label %55
55: ; preds = %52, %45, %38, %28
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"couldn't lookup home directory for user %d: %s\00"
define i8* @expand_user_path(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %30
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 47
br i1 %7, label %8, label %30
8: ; preds = %4
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = load i8, i8* %9
%13 = icmp eq i8 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i8* @strconcat(i8* %9, i8* %5, i32* null)
br label %32
16: ; preds = %11, %8
%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 %26
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = tail call i32 (...) @getuid()
%23 = load i32, i32* @errno
%24 = tail call i32 @strerror(i32 %23)
%25 = tail call i32 @_p11_message(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %24)
store i32 %21, i32* @errno
br label %32
26: ; preds = %16
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = tail call i8* @strconcat(i8* %28, i8* %5, i32* null)
br label %32
30: ; preds = %4, %1
%31 = tail call i8* @strdup(i8* %0)
br label %32
32: ; preds = %30, %26, %20, %14
%33 = phi i8* [ %15, %14 ], [ %29, %26 ], [ null, %20 ], [ %31, %30 ]
ret i8* %33
}