#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 *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
void ReplaceFirstC(char *str) {
  if (str[0] == '~') {
    char tmparg[1024];
    char *hd = getenv("HOME");
    if (hd != NULL) {
      strncpy(tmparg, hd, 1024);
      strncat(tmparg, str + 1, 1024 - strlen(hd) - 10);
      strncpy(str, tmparg, 1024);
    }
  }
}


x86 assembly (clang, -O0):
<ReplaceFirstC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    11f8 <ReplaceFirstC+0xb8>
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
je     11f3 <ReplaceFirstC+0xb3>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x420(%rbp)
mov    -0x418(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x428(%rbp),%rdi
mov    -0x420(%rbp),%rsi
mov    $0x400,%edx
sub    %eax,%edx
sub    $0xa,%edx
call   1060 <strncat@plt>
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
jmp    11f8 <ReplaceFirstC+0xb8>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReplaceFirstC>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
cmpb   $0x7e,(%rdi)
jne    11ab <ReplaceFirstC+0x6b>
mov    %rdi,%rbx
lea    0xea3(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ab <ReplaceFirstC+0x6b>
mov    %rax,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
lea    0x1(%rbx),%r12
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    $0x3f6,%edx
sub    %eax,%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strncat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
define void @ReplaceFirstC(i8* %0) {
%2 = alloca [1024 x i8]
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 126
br i1 %4, label %5, label %17
5: ; preds = %1
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = call i32 @strncpy(i8* %6, i8* %7, i32 1024)
%11 = getelementptr inbounds i8, i8* %0, i64 1
%12 = call i32 @strlen(i8* %7)
%13 = sub i32 1014, %12
%14 = call i32 @strncat(i8* %6, i8* %11, i32 %13)
%15 = call i32 @strncpy(i8* %0, i8* %6, i32 1024)
br label %16
16: ; preds = %9, %5
br label %17
17: ; preds = %16, %1
ret void
}