#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 */

/* Variables and functions */
char *getenv(char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int gethomepath(const char *homeenv, char *homepath, int size) {
  char *pc;
  int len;
  pc = getenv(homeenv);
  if (!pc)
    return 0;
  len = strlen(pc);
  if (len < size) {
    strcpy(homepath, pc);
    if (homepath[len - 1] == '/') {
      homepath[len - 1] = 0;
      --len;
    }
    return len;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<gethomepath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1167 <gethomepath+0x37>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <gethomepath+0xa2>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11cb <gethomepath+0x9b>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11c0 <gethomepath+0x90>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <gethomepath+0xa2>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethomepath>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
call   1030 <getenv@plt>
test   %rax,%rax
je     1183 <gethomepath+0x53>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    $0xffffffff,%eax
cmp    %r15d,%ebp
jge    1185 <gethomepath+0x55>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
movslq %ebp,%rcx
cmpb   $0x2f,-0x1(%r14,%rcx,1)
mov    %ebp,%eax
jne    1185 <gethomepath+0x55>
add    $0xffffffffffffffff,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %ecx,%eax
jmp    1185 <gethomepath+0x55>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gethomepath(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i8* @getenv(i8* %10)
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %3
store i32 0, i32* %4
br label %44
15: ; preds = %3
%16 = load i8*, i8** %8
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = load i32, i32* %7
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %43
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = load i8*, i8** %8
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i8*, i8** %6
%26 = load i32, i32* %9
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 47
br i1 %32, label %33, label %41
33: ; preds = %21
%34 = load i8*, i8** %6
%35 = load i32, i32* %9
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %9
%40 = add nsw i32 %39, -1
store i32 %40, i32* %9
br label %41
41: ; preds = %33, %21
%42 = load i32, i32* %9
store i32 %42, i32* %4
br label %44
43: ; preds = %15
store i32 -1, i32* %4
br label %44
44: ; preds = %43, %41, %14
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
define i32 @gethomepath(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i8* @getenv(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %4)
%8 = icmp slt i32 %7, %2
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = tail call i32 @strcpy(i8* %1, i8* %4)
%11 = add nsw i32 %7, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 47
br i1 %15, label %16, label %17
16: ; preds = %9
store i8 0, i8* %13
br label %17
17: ; preds = %6, %9, %16, %3
%18 = phi i32 [ 0, %3 ], [ %11, %16 ], [ %7, %9 ], [ -1, %6 ]
ret i32 %18
}