#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 *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *globit(char *toglob) {
  char *out, *pout, tilda = 0, *ptr = toglob;
  pout = out = malloc(strlen(toglob) + strlen(getenv("HOME")) + 2);
  while (*ptr) {
    if (*ptr == '~' && (tilda == 0)) {
      strcat(out, getenv("HOME"));
      while (*pout) {
        pout++;
      }
      ptr++;
      tilda = 1;
    } else {
      *(pout++) = *(ptr++);
    }
  }
  return out;
}


x86 assembly (clang, -O0):
<globit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    0xe94(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1235 <globit+0xf5>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    120e <globit+0xce>
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    120e <globit+0xce>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe36(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1060 <strcat@plt>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f9 <globit+0xb9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11db <globit+0x9b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
movb   $0x1,-0x19(%rbp)
jmp    1230 <globit+0xf0>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1191 <globit+0x51>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<globit>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%r14
lea    0xea6(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    (%r14,%rax,1),%rdi
add    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    (%r12),%cl
test   %cl,%cl
je     11de <globit+0x9e>
xor    %eax,%eax
lea    0xe78(%rip),%r15
mov    %r14,%rbx
jmp    11a3 <globit+0x63>
nopl   (%rax)
mov    %cl,(%rbx)
add    $0x1,%rbx
mov    0x1(%r12),%cl
add    $0x1,%r12
test   %cl,%cl
je     11de <globit+0x9e>
cmp    $0x7e,%cl
jne    1190 <globit+0x50>
test   %al,%al
jne    1190 <globit+0x50>
mov    %r15,%rdi
call   1030 <getenv@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <strcat@plt>
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    11d0 <globit+0x90>
mov    $0x1,%al
jmp    1196 <globit+0x56>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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