#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 */
typedef int /*<<< orphan*/ username;

/* Variables and functions */
scalar_t__ getlogin_r(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char const *);
int get_home_path(const char *in_dir, char *ret_dir) {
  char username[128] = {
      0,
  };
  if (getlogin_r(username, sizeof(username))) {
    printf("getlogin_r error.\n");
    return -1;
  }
  sprintf(ret_dir, "/home/%s/", username);
  if (in_dir[1] == '/') {
    strcat(ret_dir, in_dir + 2);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_home_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x80,%esi
call   1050 <getlogin_r@plt>
cmp    $0x0,%rax
je     11a9 <get_home_path+0x59>
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <get_home_path+0x99>
mov    -0x18(%rbp),%rdi
lea    -0xa0(%rbp),%rdx
lea    0xe58(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    11e2 <get_home_path+0x92>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
call   1060 <strcat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_home_path>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
mov    $0x80,%esi
call   1040 <getlogin_r@plt>
test   %rax,%rax
je     11a0 <get_home_path+0x60>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
jmp    11c9 <get_home_path+0x89>
lea    0xe59(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1060 <sprintf@plt>
xor    %ebp,%ebp
cmpb   $0x2f,0x1(%rbx)
jne    11c9 <get_home_path+0x89>
add    $0x2,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strcat@plt>
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"getlogin_r error.\0A\00"
@.str.1 = [10 x i8] c"/home/%s/\00"
define i32 @get_home_path(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [128 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = bitcast [128 x i8]* %6 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = call i64 @getlogin_r(i8* %8, i32 128)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 47
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load i8*, i8** %5
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = call i32 @strcat(i8* %23, i8* %25)
br label %27
27: ; preds = %22, %13
store i32 0, i32* %3
br label %28
28: ; preds = %27, %11
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"/home/%s/\00"
@str = [18 x i8] c"getlogin_r error.\00"
define i32 @get_home_path(i8* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i64 @getlogin_r(i8* %4, i32 128)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %17
9: ; preds = %2
%10 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%11 = getelementptr inbounds i8, i8* %0, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 47
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %0, i64 2
%16 = call i32 @strcat(i8* %1, i8* %15)
br label %17
17: ; preds = %9, %14, %7
%18 = phi i32 [ -1, %7 ], [ 0, %14 ], [ 0, %9 ]
ret i32 %18
}