#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 *);
scalar_t__ isalnum(char const);
scalar_t__ isalpha(char const);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *umalloc(size_t);
char *expand_tilde(const char *in) {
  if (*in != '~')
    return NULL;
  const char *user = in + 1;
  if (isalpha(*user)) {
    const char *user_end;
    for (user_end = user; isalnum(*user_end); user_end++)
      ;
    return NULL;
  } else {
    const char *home = getenv("HOME");
    if (!home)
      return NULL;
    size_t homelen = strlen(home);
    size_t userlen = strlen(user);
    size_t len = homelen + userlen + 1;
    char *ret = umalloc(len);
    memcpy(ret, home, homelen);
    memcpy(ret + homelen, user, userlen);
    ret[homelen + userlen] = '\0';
    return ret;
  }
}


x86 assembly (clang, -O0):
<expand_tilde>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     1189 <expand_tilde+0x29>
movq   $0x0,-0x8(%rbp)
jmp    1289 <expand_tilde+0x129>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1080 <isalpha@plt>
cmp    $0x0,%rax
je     11ec <expand_tilde+0x8c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%rax
je     11df <expand_tilde+0x7f>
jmp    11ce <expand_tilde+0x6e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b3 <expand_tilde+0x53>
movq   $0x0,-0x8(%rbp)
jmp    1289 <expand_tilde+0x129>
lea    0xe0d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1214 <expand_tilde+0xb4>
movq   $0x0,-0x8(%rbp)
jmp    1289 <expand_tilde+0x129>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1070 <umalloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_tilde>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmpb   $0x7e,(%rdi)
jne    1207 <expand_tilde+0xa7>
mov    %rdi,%rbx
movsbl 0x1(%rdi),%edi
add    $0x1,%rbx
call   1080 <isalpha@plt>
test   %rax,%rax
je     11a3 <expand_tilde+0x43>
xor    %r14d,%r14d
nopl   0x0(%rax)
movsbl (%rbx),%edi
call   1050 <isalnum@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1190 <expand_tilde+0x30>
jmp    120a <expand_tilde+0xaa>
lea    0xe56(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1207 <expand_tilde+0xa7>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r12,1),%rbp
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
call   1070 <umalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
add    %r14,%r12
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r14,%rbp,1)
jmp    120a <expand_tilde+0xaa>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
define i8* @expand_tilde(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 126
br i1 %3, label %4, label %28
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = tail call i64 @isalpha(i8 signext %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %4, %9
%10 = phi i8* [ %14, %9 ], [ %5, %4 ]
%11 = load i8, i8* %10
%12 = tail call i64 @isalnum(i8 signext %11)
%13 = icmp eq i64 %12, 0
%14 = getelementptr inbounds i8, i8* %10, i64 1
br i1 %13, label %28, label %9
15: ; preds = %4
%16 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = tail call i64 @strlen(i8* %16)
%20 = tail call i64 @strlen(i8* %5)
%21 = add i64 %20, %19
%22 = add i64 %21, 1
%23 = tail call i8* @umalloc(i64 %22)
%24 = tail call i32 @memcpy(i8* %23, i8* %16, i64 %19)
%25 = getelementptr inbounds i8, i8* %23, i64 %19
%26 = tail call i32 @memcpy(i8* %25, i8* %5, i64 %20)
%27 = getelementptr inbounds i8, i8* %23, i64 %21
store i8 0, i8* %27
br label %28
28: ; preds = %9, %15, %18, %1
%29 = phi i8* [ null, %1 ], [ %23, %18 ], [ null, %15 ], [ null, %9 ]
ret i8* %29
}