#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 */
char *getenv(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *findhome(char *path) {
  static char *HomeDir = NULL;
  if (*path == '~') {
    char *pathptr;
    char tmppath[1024];
    if (HomeDir == NULL) {
      HomeDir = getenv("HOME");
      if (HomeDir == NULL)
        HomeDir = "/";
    }
    pathptr = path + 1;
    if ((*pathptr == '/') || (*pathptr == '\0'))
      strcpy(tmppath, HomeDir);
    else {
      struct passwd *pwent;
      char *namep;
      char name[50];
      namep = name;
      while ((*pathptr != '\0') && (*pathptr != '/'))
        *(namep++) = *(pathptr++);
      *namep = '\0';
      if ((pwent = getpwnam(name)) == NULL) {
        (void)sprintf(path, "Can't find user %s", name);
        return (NULL);
      }
      strcpy(tmppath, pwent->pw_dir);
    }
    strcat(tmppath, pathptr);
    strcpy(path, tmppath);
  }
  return (path);
}


x86 assembly (clang, -O0):
<findhome>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    12ef <findhome+0x19f>
cmpq   $0x0,0x2ed9(%rip)
jne    11b1 <findhome+0x61>
lea    0xe7c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,0x2ec0(%rip)
cmpq   $0x0,0x2eb8(%rip)
jne    11ac <findhome+0x5c>
lea    0xe60(%rip),%rax
mov    %rax,0x2ea4(%rip)
jmp    11b1 <findhome+0x61>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11dd <findhome+0x8d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11f5 <findhome+0xa5>
lea    -0x420(%rbp),%rdi
mov    0x2e65(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12cf <findhome+0x17f>
lea    -0x470(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x471(%rbp)
je     122e <findhome+0xde>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x471(%rbp)
mov    -0x471(%rbp),%al
test   $0x1,%al
jne    1241 <findhome+0xf1>
jmp    126e <findhome+0x11e>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%cl
mov    -0x430(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x430(%rbp)
mov    %cl,(%rax)
jmp    1203 <findhome+0xb3>
mov    -0x430(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x470(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jne    12b9 <findhome+0x169>
mov    -0x10(%rbp),%rdi
lea    -0x470(%rbp),%rdx
lea    0xd60(%rip),%rsi
call   1070 <sprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f7 <findhome+0x1a7>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findhome>:
push   %r15
push   %r14
push   %rbx
sub    $0x440,%rsp
mov    %rdi,%r14
cmpb   $0x7e,(%rdi)
jne    11fe <findhome+0xae>
mov    0x2ee1(%rip),%rsi
test   %rsi,%rsi
jne    1195 <findhome+0x45>
lea    0xe85(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe7b(%rip),%rsi
cmovne %rax,%rsi
mov    %rsi,0x2ebb(%rip)
lea    0x1(%r14),%rbx
mov    0x1(%r14),%al
cmp    $0x2f,%al
je     11d9 <findhome+0x89>
test   %al,%al
je     11d9 <findhome+0x89>
mov    %rsp,%rcx
test   %al,%al
je     11c6 <findhome+0x76>
nopl   0x0(%rax)
cmp    $0x2f,%al
je     11c6 <findhome+0x76>
mov    %al,(%rcx)
add    $0x1,%rcx
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11b0 <findhome+0x60>
movb   $0x0,(%rcx)
mov    %rsp,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     120e <findhome+0xbe>
mov    (%rax),%rsi
lea    0x40(%rsp),%rdi
call   1040 <strcpy@plt>
lea    0x40(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rax
add    $0x440,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1070 <sprintf@plt>
xor    %r14d,%r14d
jmp    11fe <findhome+0xae>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@findhome.HomeDir = internal global i8* null
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [19 x i8] c"Can't find user %s\00"
define i8* @findhome(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca [50 x i8]
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 126
br i1 %12, label %13, label %80
13: ; preds = %1
%14 = load i8*, i8** @findhome.HomeDir
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** @findhome.HomeDir
%18 = load i8*, i8** @findhome.HomeDir
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %16
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** @findhome.HomeDir
br label %21
21: ; preds = %20, %16
br label %22
22: ; preds = %21, %13
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 47
br i1 %28, label %34, label %29
29: ; preds = %22
%30 = load i8*, i8** %4
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29, %22
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = load i8*, i8** @findhome.HomeDir
%37 = call i32 @strcpy(i8* %35, i8* %36)
br label %73
38: ; preds = %29
%39 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
store i8* %39, i8** %7
br label %40
40: ; preds = %52, %38
%41 = load i8*, i8** %4
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 47
br label %50
50: ; preds = %45, %40
%51 = phi i1 [ false, %40 ], [ %49, %45 ]
br i1 %51, label %52, label %58
52: ; preds = %50
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %4
%55 = load i8, i8* %53
%56 = load i8*, i8** %7
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %7
store i8 %55, i8* %56
br label %40
58: ; preds = %50
%59 = load i8*, i8** %7
store i8 0, i8* %59
%60 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%61 = call %struct.passwd* @getpwnam(i8* %60)
store %struct.passwd* %61, %struct.passwd** %6
%62 = icmp eq %struct.passwd* %61, null
br i1 %62, label %63, label %67
63: ; preds = %58
%64 = load i8*, i8** %3
%65 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%66 = call i32 @sprintf(i8* %64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %65)
store i8* null, i8** %2
br label %82
67: ; preds = %58
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%69 = load %struct.passwd*, %struct.passwd** %6
%70 = getelementptr inbounds %struct.passwd, %struct.passwd* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = call i32 @strcpy(i8* %68, i8* %71)
br label %73
73: ; preds = %67, %34
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%75 = load i8*, i8** %4
%76 = call i32 @strcat(i8* %74, i8* %75)
%77 = load i8*, i8** %3
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%79 = call i32 @strcpy(i8* %77, i8* %78)
br label %80
80: ; preds = %73, %1
%81 = load i8*, i8** %3
store i8* %81, i8** %2
br label %82
82: ; preds = %80, %63
%83 = load i8*, i8** %2
ret i8* %83
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@findhome.HomeDir = internal unnamed_addr global i8* null
@.str = [5 x i8] c"HOME\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [19 x i8] c"Can't find user %s\00"
define i8* @findhome(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [50 x i8]
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 126
br i1 %5, label %6, label %43
6: ; preds = %1
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%8 = load i8*, i8** @findhome.HomeDir
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %11
store i8* %13, i8** @findhome.HomeDir
br label %14
14: ; preds = %10, %6
%15 = phi i8* [ %13, %10 ], [ %8, %6 ]
%16 = getelementptr inbounds i8, i8* %0, i64 1
%17 = load i8, i8* %16
switch i8 %17, label %20 [
i8 47, label %18
i8 0, label %18
]
18: ; preds = %14, %14
%19 = call i32 @strcpy(i8* %7, i8* %15)
br label %37
20: ; preds = %14
%21 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
br label %22
22: ; preds = %26, %20
%23 = phi i8 [ %29, %26 ], [ %17, %20 ]
%24 = phi i8* [ %27, %26 ], [ %16, %20 ]
%25 = phi i8* [ %28, %26 ], [ %21, %20 ]
switch i8 %23, label %26 [
i8 0, label %30
i8 47, label %30
]
26: ; preds = %22
%27 = getelementptr inbounds i8, i8* %24, i64 1
%28 = getelementptr inbounds i8, i8* %25, i64 1
store i8 %23, i8* %25
%29 = load i8, i8* %27
br label %22
30: ; preds = %22, %22
store i8 0, i8* %25
%31 = call %struct.passwd* @getpwnam(i8* %21)
%32 = icmp eq %struct.passwd* %31, null
br i1 %32, label %41, label %33
33: ; preds = %30
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @strcpy(i8* %7, i8* %35)
br label %37
37: ; preds = %18, %33
%38 = phi i8* [ %16, %18 ], [ %24, %33 ]
%39 = call i32 @strcat(i8* %7, i8* %38)
%40 = call i32 @strcpy(i8* %0, i8* %7)
br label %43
41: ; preds = %30
%42 = call i32 @sprintf(i8* %0, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %43
43: ; preds = %1, %37, %41
%44 = phi i8* [ null, %41 ], [ %0, %37 ], [ %0, %1 ]
ret i8* %44
}