#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 *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, size_t);
int gethdir(char *home, size_t homelen) {
  struct passwd *pp = getpwnam(home);
  if (pp == 0)
    return (1);
  strncpy(home, pp->pw_dir, homelen - 1);
  home[homelen - 1] = '\0';
  return (0);
}


x86 assembly (clang, -O0):
<gethdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1154 <gethdir+0x34>
movl   $0x1,-0x4(%rbp)
jmp    1182 <gethdir+0x62>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethdir>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <getpwnam@plt>
test   %rax,%rax
je     114c <gethdir+0x2c>
mov    (%rax),%esi
lea    -0x1(%r14),%rdx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
xor    %eax,%eax
jmp    1151 <gethdir+0x31>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
define i32 @gethdir(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.passwd*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = call %struct.passwd* @getpwnam(i8* %7)
store %struct.passwd* %8, %struct.passwd** %6
%9 = load %struct.passwd*, %struct.passwd** %6
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %24
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load %struct.passwd*, %struct.passwd** %6
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i64, i64* %5
%18 = sub i64 %17, 1
%19 = call i32 @strncpy(i8* %13, i32 %16, i64 %18)
%20 = load i8*, i8** %4
%21 = load i64, i64* %5
%22 = sub i64 %21, 1
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
store i32 0, i32* %3
br label %24
24: ; preds = %12, %11
%25 = load i32, i32* %3
ret i32 %25
}

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