#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*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
struct passwd *getpwnam(char *);
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *expand(char *filepath) {
  static char newpath[256];
  static char username[32];
  struct passwd *pwent;
  char *home;
  int i, j;
  i = 0;
  newpath[0] = '\0';
  if (filepath[i] == '~') {
    i++;
    if (!filepath[i] || filepath[i] == '/') {
      home = getenv("HOME");
      if (home) {
        strcat(newpath, home);
      } else {
        pwent = getpwuid(getuid());
        if (!pwent) {
          fprintf(stderr, "usepackage: cannot obtain home directory.\n");
          return (filepath);
        }
        strcat(newpath, pwent->pw_dir);
      }
    } else {
      j = i;
      while (filepath[i] && (filepath[i] != '/'))
        i++;
      strncpy(username, filepath + j, i - j);
      username[i - j] = '\0';
      pwent = getpwnam(username);
      if (!pwent) {
        fprintf(stderr,
                "usepackage: cannot obtain home directory of user `%s'.\n",
                username);
        return (filepath);
      }
      strcat(newpath, pwent->pw_dir);
    }
  }
  strcat(newpath, filepath + i);
  return (newpath);
}


x86 assembly (clang, -O0):
<expand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x0,0x2ee6(%rip)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x7e,%eax
jne    133a <expand+0x1ca>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11cf <expand+0x5f>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1258 <expand+0xe8>
lea    0xe2a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ff <expand+0x8f>
mov    -0x20(%rbp),%rsi
lea    0x2e7b(%rip),%rdi
call   1090 <strcat@plt>
jmp    1253 <expand+0xe3>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1240 <expand+0xd0>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    135c <expand+0x1ec>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0x2e22(%rip),%rdi
call   1090 <strcat@plt>
jmp    1335 <expand+0x1c5>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     128d <expand+0x11d>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    129d <expand+0x12d>
jmp    12ab <expand+0x13b>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    125e <expand+0xee>
mov    -0x10(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
sub    -0x28(%rbp),%edx
lea    0x2ead(%rip),%rdi
call   1040 <strncpy@plt>
mov    -0x24(%rbp),%eax
sub    -0x28(%rbp),%eax
movslq %eax,%rcx
lea    0x2e98(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e8d(%rip),%rdi
call   1070 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1322 <expand+0x1b2>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0xd29(%rip),%rsi
lea    0x2e62(%rip),%rdx
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    135c <expand+0x1ec>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0x2d40(%rip),%rdi
call   1090 <strcat@plt>
jmp    133a <expand+0x1ca>
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
lea    0x2d24(%rip),%rdi
call   1090 <strcat@plt>
lea    0x2d18(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
movb   $0x0,0x2eee(%rip)
cmpb   $0x7e,(%rdi)
jne    11b4 <expand+0x44>
mov    0x1(%r12),%al
test   %al,%al
je     1194 <expand+0x24>
cmp    $0x2f,%al
jne    11bb <expand+0x4b>
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1220 <expand+0xb0>
lea    0x2ec4(%rip),%rdi
mov    %rax,%rsi
jmp    123d <expand+0xcd>
xor    %eax,%eax
jmp    1247 <expand+0xd7>
lea    0x1(%r12),%rsi
mov    $0x1,%ebx
test   %al,%al
je     11e2 <expand+0x72>
nopl   0x0(%rax)
cmp    $0x2f,%al
je     11e2 <expand+0x72>
movzbl 0x1(%r12,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    11d0 <expand+0x60>
lea    -0x1(%rbx),%r14
lea    0x2f83(%rip),%r15
mov    %r15,%rdi
mov    %r14d,%edx
call   1040 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rax,%r15,1)
mov    %r15,%rdi
call   1070 <getpwnam@plt>
test   %rax,%rax
je     126e <expand+0xfe>
mov    (%rax),%rsi
lea    0x2e59(%rip),%rdi
call   1090 <strcat@plt>
mov    %ebx,%eax
jmp    1247 <expand+0xd7>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
test   %rax,%rax
je     128e <expand+0x11e>
mov    (%rax),%rsi
lea    0x2e33(%rip),%rdi
call   1090 <strcat@plt>
mov    $0x1,%eax
add    %rax,%r12
lea    0x2e1f(%rip),%r14
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %r14,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
lea    0x2eeb(%rip),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    125f <expand+0xef>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    125f <expand+0xef>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@expand.newpath = internal global [256 x i8] zeroinitializer
@expand.username = internal global [32 x i8] zeroinitializer
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"usepackage: cannot obtain home directory.\0A\00"
@.str.2 = [56 x i8] c"usepackage: cannot obtain home directory of user `%s'.\0A\00"
define i8* @expand(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0)
%8 = load i8*, i8** %3
%9 = load i32, i32* %6
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 126
br i1 %14, label %15, label %104
15: ; preds = %1
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6
%18 = load i8*, i8** %3
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %15
%25 = load i8*, i8** %3
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 47
br i1 %31, label %32, label %54
32: ; preds = %24, %15
%33 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %33, i8** %5
%34 = load i8*, i8** %5
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %37)
br label %53
39: ; preds = %32
%40 = call i32 (...) @getuid()
%41 = call %struct.passwd* @getpwuid(i32 %40)
store %struct.passwd* %41, %struct.passwd** %4
%42 = load %struct.passwd*, %struct.passwd** %4
%43 = icmp ne %struct.passwd* %42, null
br i1 %43, label %48, label %44
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%47 = load i8*, i8** %3
store i8* %47, i8** %2
br label %110
48: ; preds = %39
%49 = load %struct.passwd*, %struct.passwd** %4
%50 = getelementptr inbounds %struct.passwd, %struct.passwd* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %48, %36
br label %103
54: ; preds = %24
%55 = load i32, i32* %6
store i32 %55, i32* %7
br label %56
56: ; preds = %74, %54
%57 = load i8*, i8** %3
%58 = load i32, i32* %6
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %72
64: ; preds = %56
%65 = load i8*, i8** %3
%66 = load i32, i32* %6
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 47
br label %72
72: ; preds = %64, %56
%73 = phi i1 [ false, %56 ], [ %71, %64 ]
br i1 %73, label %74, label %77
74: ; preds = %72
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
store i32 %76, i32* %6
br label %56
77: ; preds = %72
%78 = load i8*, i8** %3
%79 = load i32, i32* %7
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %78, i64 %80
%82 = load i32, i32* %6
%83 = load i32, i32* %7
%84 = sub nsw i32 %82, %83
%85 = call i32 @strncpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @expand.username, i64 0, i64 0), i8* %81, i32 %84)
%86 = load i32, i32* %6
%87 = load i32, i32* %7
%88 = sub nsw i32 %86, %87
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds [32 x i8], [32 x i8]* @expand.username, i64 0, i64 %89
store i8 0, i8* %90
%91 = call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @expand.username, i64 0, i64 0))
store %struct.passwd* %91, %struct.passwd** %4
%92 = load %struct.passwd*, %struct.passwd** %4
%93 = icmp ne %struct.passwd* %92, null
br i1 %93, label %98, label %94
94: ; preds = %77
%95 = load i32, i32* @stderr
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @expand.username, i64 0, i64 0))
%97 = load i8*, i8** %3
store i8* %97, i8** %2
br label %110
98: ; preds = %77
%99 = load %struct.passwd*, %struct.passwd** %4
%100 = getelementptr inbounds %struct.passwd, %struct.passwd* %99, i32 0, i32 0
%101 = load i8*, i8** %100
%102 = call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %101)
br label %103
103: ; preds = %98, %53
br label %104
104: ; preds = %103, %1
%105 = load i8*, i8** %3
%106 = load i32, i32* %6
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %105, i64 %107
%109 = call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %108)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8** %2
br label %110
110: ; preds = %104, %94, %44
%111 = load i8*, i8** %2
ret i8* %111
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@expand.newpath = internal global [256 x i8] zeroinitializer
@expand.username = internal global [32 x i8] zeroinitializer
@.str = [5 x i8] c"HOME\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"usepackage: cannot obtain home directory.\0A\00"
@.str.2 = [56 x i8] c"usepackage: cannot obtain home directory of user `%s'.\0A\00"
define i8* @expand(i8* %0) {
store i8 0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0)
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %46
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
switch i8 %6, label %23 [
i8 0, label %7
i8 47, label %7
]
7: ; preds = %4, %4
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %8)
br label %46
12: ; preds = %7
%13 = tail call i32 (...) @getuid()
%14 = tail call %struct.passwd* @getpwuid(i32 %13)
%15 = icmp eq %struct.passwd* %14, null
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %50
19: ; preds = %12
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = tail call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %21)
br label %46
23: ; preds = %4, %26
%24 = phi i8 [ %29, %26 ], [ %6, %4 ]
%25 = phi i64 [ %27, %26 ], [ 1, %4 ]
switch i8 %24, label %26 [
i8 0, label %30
i8 47, label %30
]
26: ; preds = %23
%27 = add nuw i64 %25, 1
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i8, i8* %28
br label %23
30: ; preds = %23, %23
%31 = trunc i64 %25 to i32
%32 = add nsw i32 %31, -1
%33 = tail call i32 @strncpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @expand.username, i64 0, i64 0), i8* %5, i32 %32)
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds [32 x i8], [32 x i8]* @expand.username, i64 0, i64 %34
store i8 0, i8* %35
%36 = tail call %struct.passwd* @getpwnam(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @expand.username, i64 0, i64 0))
%37 = icmp eq %struct.passwd* %36, null
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @expand.username, i64 0, i64 0))
br label %50
41: ; preds = %30
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = tail call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %43)
%45 = and i64 %25, 4294967295
br label %46
46: ; preds = %41, %19, %10, %1
%47 = phi i64 [ 1, %10 ], [ 1, %19 ], [ %45, %41 ], [ 0, %1 ]
%48 = getelementptr inbounds i8, i8* %0, i64 %47
%49 = tail call i32 @strcat(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %46, %38, %16
%51 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @expand.newpath, i64 0, i64 0), %46 ], [ %0, %16 ], [ %0, %38 ]
ret i8* %51
}