#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 *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void sys_expandpath(const char *from, char *to, int bufsize) {
  if ((strlen(from) == 1 && from[0] == '~') || (strncmp(from, "~/", 2) == 0)) {
    const char *home = getenv("HOME");
    if (home) {
      strncpy(to, home, bufsize);
      to[bufsize - 1] = 0;
      strncpy(to + strlen(to), from + 1, bufsize - strlen(to));
      to[bufsize - 1] = 0;
    }
  } else {
    strncpy(to, from, bufsize);
    to[bufsize - 1] = 0;
  }
}


x86 assembly (clang, -O0):
<sys_expandpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x1,%eax
jne    1175 <sys_expandpath+0x35>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     1194 <sys_expandpath+0x54>
mov    -0x8(%rbp),%rdi
lea    0xe80(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1234 <sys_expandpath+0xf4>
lea    0xe68(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     122f <sys_expandpath+0xef>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
sub    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1255 <sys_expandpath+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_expandpath>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1060 <strlen@plt>
cmp    $0x1,%eax
jne    1160 <sys_expandpath+0x20>
cmpb   $0x7e,(%rbx)
je     1194 <sys_expandpath+0x54>
lea    0xe99(%rip),%rsi
mov    %rbx,%rdi
mov    $0x2,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     1194 <sys_expandpath+0x54>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
movslq %r14d,%rax
lea    (%rax,%rbp,1),%r12
add    $0xffffffffffffffff,%r12
jmp    11ee <sys_expandpath+0xae>
lea    0xe68(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f3 <sys_expandpath+0xb3>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
movslq %r14d,%rax
lea    (%rax,%rbp,1),%r12
add    $0xffffffffffffffff,%r12
movb   $0x0,-0x1(%rax,%rbp,1)
mov    %rbp,%rdi
call   1060 <strlen@plt>
movslq %eax,%r15
add    %rbp,%r15
add    $0x1,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
sub    %eax,%r14d
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
movb   $0x0,(%r12)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"~/\00"
@.str.1 = [5 x i8] c"HOME\00"
define void @sys_expandpath(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 126
br i1 %16, label %21, label %17
17: ; preds = %11, %3
%18 = load i8*, i8** %4
%19 = call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %53
21: ; preds = %17, %11
%22 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %52
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = load i8*, i8** %7
%28 = load i32, i32* %6
%29 = call i32 @strncpy(i8* %26, i8* %27, i32 %28)
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %5
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i32, i32* %6
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = sub nsw i32 %42, %44
%46 = call i32 @strncpy(i8* %39, i8* %41, i32 %45)
%47 = load i8*, i8** %5
%48 = load i32, i32* %6
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %25, %21
br label %63
53: ; preds = %17
%54 = load i8*, i8** %5
%55 = load i8*, i8** %4
%56 = load i32, i32* %6
%57 = call i32 @strncpy(i8* %54, i8* %55, i32 %56)
%58 = load i8*, i8** %5
%59 = load i32, i32* %6
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
store i8 0, i8* %62
br label %63
63: ; preds = %53, %52
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"~/\00"
@.str.1 = [5 x i8] c"HOME\00"
define void @sys_expandpath(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 126
br i1 %8, label %12, label %9
9: ; preds = %6, %3
%10 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %9, %6
%13 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %34, label %15
15: ; preds = %12
%16 = tail call i32 @strncpy(i8* %1, i8* %13, i32 %2)
%17 = add nsw i32 %2, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %1, i64 %18
store i8 0, i8* %19
%20 = tail call i32 @strlen(i8* %1)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = getelementptr inbounds i8, i8* %0, i64 1
%24 = tail call i32 @strlen(i8* %1)
%25 = sub nsw i32 %2, %24
%26 = tail call i32 @strncpy(i8* %22, i8* %23, i32 %25)
br label %32
27: ; preds = %9
%28 = tail call i32 @strncpy(i8* %1, i8* %0, i32 %2)
%29 = add nsw i32 %2, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %1, i64 %30
br label %32
32: ; preds = %27, %15
%33 = phi i8* [ %19, %15 ], [ %31, %27 ]
store i8 0, i8* %33
br label %34
34: ; preds = %32, %12
ret void
}