#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 {
  int /*<<< orphan*/ pw_dir;
};

/* Variables and functions */
struct passwd *getpwnam(char const *);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
char *getHomeDir(const char *c) {
  struct passwd *p = getpwnam(c);
  if (!p)
    return NULL;
  p->pw_dir;
  int n = strlen(p->pw_dir);
  char *ret = malloc((n + 1) * sizeof(char));
  strcpy(ret, p->pw_dir);
  return ret;
}


x86 assembly (clang, -O0):
<getHomeDir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <getHomeDir+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11ad <getHomeDir+0x6d>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHomeDir>:
push   %r14
push   %rbx
push   %rax
call   1050 <getpwnam@plt>
test   %rax,%rax
je     116f <getHomeDir+0x2f>
mov    %rax,%rbx
mov    (%rax),%edi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%esi
mov    %rax,%rdi
call   1030 <strcpy@plt>
jmp    1172 <getHomeDir+0x32>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
define i8* @getHomeDir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call %struct.passwd* @getpwnam(i8* %7)
store %struct.passwd* %8, %struct.passwd** %4
%9 = load %struct.passwd*, %struct.passwd** %4
%10 = icmp ne %struct.passwd* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %32
12: ; preds = %1
%13 = load %struct.passwd*, %struct.passwd** %4
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load %struct.passwd*, %struct.passwd** %4
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strlen(i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 1
%24 = trunc i64 %23 to i32
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = load %struct.passwd*, %struct.passwd** %4
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @strcpy(i8* %26, i32 %29)
%31 = load i8*, i8** %6
store i8* %31, i8** %2
br label %32
32: ; preds = %12, %11
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
define i8* @getHomeDir(i8* nocapture %0) {
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @strlen(i32 %6)
%8 = add nsw i32 %7, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = load i32, i32* %5
%11 = tail call i32 @strcpy(i8* %9, i32 %10)
br label %12
12: ; preds = %1, %4
%13 = phi i8* [ %9, %4 ], [ null, %1 ]
ret i8* %13
}