#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 const *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *xdgGetEnv(const char *name, const char *defaultValue) {
  const char *env;
  char *value;
  env = getenv(name);
  if (env && env[0]) {
    if (!(value = (char *)malloc(strlen(env) + 1)))
      return 0;
    strcpy(value, env);
  } else {
    if (!(value = (char *)malloc(strlen(defaultValue) + 1)))
      return 0;
    strcpy(value, defaultValue);
  }
  return value;
}


x86 assembly (clang, -O0):
<xdgGetEnv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ba <xdgGetEnv+0x7a>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ba <xdgGetEnv+0x7a>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a8 <xdgGetEnv+0x68>
movq   $0x0,-0x8(%rbp)
jmp    11ff <xdgGetEnv+0xbf>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11f7 <xdgGetEnv+0xb7>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ea <xdgGetEnv+0xaa>
movq   $0x0,-0x8(%rbp)
jmp    11ff <xdgGetEnv+0xbf>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdgGetEnv>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
call   1030 <getenv@plt>
test   %rax,%rax
je     117b <xdgGetEnv+0x3b>
mov    %rax,%r15
cmpb   $0x0,(%rax)
je     117b <xdgGetEnv+0x3b>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a1 <xdgGetEnv+0x61>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
jmp    119a <xdgGetEnv+0x5a>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a1 <xdgGetEnv+0x61>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    11a3 <xdgGetEnv+0x63>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @xdgGetEnv(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i8* @getenv(i8* %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %30
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %12
%19 = load i8*, i8** %6
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %18
store i8* null, i8** %3
br label %44
26: ; preds = %18
%27 = load i8*, i8** %7
%28 = load i8*, i8** %6
%29 = call i32 @strcpy(i8* %27, i8* %28)
br label %42
30: ; preds = %12, %2
%31 = load i8*, i8** %5
%32 = call i64 @strlen(i8* %31)
%33 = add nsw i64 %32, 1
%34 = call i64 @malloc(i64 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %7
%36 = icmp ne i8* %35, null
br i1 %36, label %38, label %37
37: ; preds = %30
store i8* null, i8** %3
br label %44
38: ; preds = %30
%39 = load i8*, i8** %7
%40 = load i8*, i8** %5
%41 = call i32 @strcpy(i8* %39, i8* %40)
br label %42
42: ; preds = %38, %26
%43 = load i8*, i8** %7
store i8* %43, i8** %3
br label %44
44: ; preds = %42, %37, %25
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
define i8* @xdgGetEnv(i8* nocapture %0, i8* %1) {
%3 = tail call i8* @getenv(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = load i8, i8* %3
%7 = icmp eq i8 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %5
%9 = tail call i64 @strlen(i8* %3)
%10 = add nsw i64 %9, 1
%11 = tail call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %24, label %14
14: ; preds = %8
%15 = tail call i32 @strcpy(i8* %12, i8* %3)
br label %24
16: ; preds = %5, %2
%17 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%18 = add nsw i64 %17, 1
%19 = tail call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = tail call i32 @strcpy(i8* %20, i8* %1)
br label %24
24: ; preds = %14, %22, %16, %8
%25 = phi i8* [ null, %8 ], [ null, %16 ], [ %12, %14 ], [ %20, %22 ]
ret i8* %25
}