#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 *env_lookup(char const *, char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *append_env(char *to, const char *p, const char *q) {
  char *foo;
  int len;
  foo = env_lookup(p, q);
  if (foo) {
    len = strlen(foo);
    strcpy(to, foo);
    free(foo);
  } else {
    len = 0;
  }
  return (to + len);
}


x86 assembly (clang, -O0):
<append_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <env_lookup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1197 <append_env+0x57>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    119e <append_env+0x5e>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_env>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1050 <env_lookup@plt>
test   %rax,%rax
je     117c <append_env+0x3c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
movslq %ebp,%rax
jmp    117e <append_env+0x3e>
xor    %eax,%eax
add    %rax,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @append_env(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = call i8* @env_lookup(i8* %9, i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %22
14: ; preds = %3
%15 = load i8*, i8** %7
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %8
%17 = load i8*, i8** %4
%18 = load i8*, i8** %7
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @free(i8* %20)
br label %23
22: ; preds = %3
store i32 0, i32* %8
br label %23
23: ; preds = %22, %14
%24 = load i8*, i8** %4
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
ret i8* %27
}

LLVM IR (-O2):
define i8* @append_env(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @env_lookup(i8* %1, i8* %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %4)
%8 = tail call i32 @strcpy(i8* %0, i8* %4)
%9 = tail call i32 @free(i8* %4)
%10 = sext i32 %7 to i64
br label %11
11: ; preds = %3, %6
%12 = phi i64 [ %10, %6 ], [ 0, %3 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
ret i8* %13
}