#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 *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *append_home_dir(char *to) {
  struct passwd *foo;
  int len;
  char *lookup;
  lookup = getenv("HOME");
  if (lookup) {
    len = strlen(lookup);
    strcpy(to, lookup);
  } else {
    foo = getpwuid(getuid());
    len = strlen(foo->pw_dir);
    strcpy(to, foo->pw_dir);
  }
  return to + len;
}


x86 assembly (clang, -O0):
<append_home_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1195 <append_home_dir+0x45>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11c6 <append_home_dir+0x76>
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
add    %rcx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_home_dir>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xea2(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     117e <append_home_dir+0x2e>
mov    %rax,%rbp
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %rbp,%rsi
jmp    11a1 <append_home_dir+0x51>
xor    %eax,%eax
call   1070 <getuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,%rbp
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
movslq %r14d,%rax
add    %rax,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
define i8* @append_home_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.passwd*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %5
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %4
%12 = load i8*, i8** %2
%13 = load i8*, i8** %5
%14 = call i32 @strcpy(i8* %12, i8* %13)
br label %27
15: ; preds = %1
%16 = call i32 (...) @getuid()
%17 = call %struct.passwd* @getpwuid(i32 %16)
store %struct.passwd* %17, %struct.passwd** %3
%18 = load %struct.passwd*, %struct.passwd** %3
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %4
%22 = load i8*, i8** %2
%23 = load %struct.passwd*, %struct.passwd** %3
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 @strcpy(i8* %22, i8* %25)
br label %27
27: ; preds = %15, %9
%28 = load i8*, i8** %2
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
ret i8* %31
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"HOME\00"
define i8* @append_home_dir(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strcpy(i8* %0, i8* %2)
br label %15
7: ; preds = %1
%8 = tail call i32 (...) @getuid()
%9 = tail call %struct.passwd* @getpwuid(i32 %8)
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i64 0, i32 0
%11 = load i8*, i8** %10
%12 = tail call i32 @strlen(i8* %11)
%13 = load i8*, i8** %10
%14 = tail call i32 @strcpy(i8* %0, i8* %13)
br label %15
15: ; preds = %7, %4
%16 = phi i32 [ %5, %4 ], [ %12, %7 ]
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
ret i8* %18
}