#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__ malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
char *xdg_user_dir_lookup_with_fallback(char const *, int /*<<< orphan*/ *);
char *xdg_user_dir_lookup(const char *type) {
  char *dir, *home_dir, *user_dir;
  dir = xdg_user_dir_lookup_with_fallback(type, NULL);
  if (dir != NULL)
    return dir;
  home_dir = getenv("HOME");
  if (home_dir == NULL)
    return strdup("/tmp");
  if (strcmp(type, "DESKTOP") == 0) {
    user_dir = (char *)malloc(strlen(home_dir) + strlen("/Desktop") + 1);
    if (user_dir == NULL)
      return NULL;
    strcpy(user_dir, home_dir);
    strcat(user_dir, "/Desktop");
    return user_dir;
  }
  return strdup(home_dir);
}


x86 assembly (clang, -O0):
<xdg_user_dir_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <xdg_user_dir_lookup_with_fallback@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b5 <xdg_user_dir_lookup+0x35>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    127b <xdg_user_dir_lookup+0xfb>
lea    0xe44(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e5 <xdg_user_dir_lookup+0x65>
lea    0xe2e(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    127b <xdg_user_dir_lookup+0xfb>
mov    -0x10(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    126e <xdg_user_dir_lookup+0xee>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    0xdff(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1244 <xdg_user_dir_lookup+0xc4>
movq   $0x0,-0x8(%rbp)
jmp    127b <xdg_user_dir_lookup+0xfb>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xdb6(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    127b <xdg_user_dir_lookup+0xfb>
mov    -0x20(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xdg_user_dir_lookup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %esi,%esi
call   10a0 <xdg_user_dir_lookup_with_fallback@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a0 <xdg_user_dir_lookup+0x20>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe59(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11cd <xdg_user_dir_lookup+0x4d>
mov    %rax,%r14
lea    0xe4f(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11de <xdg_user_dir_lookup+0x5e>
mov    %r14,%rdi
jmp    11d4 <xdg_user_dir_lookup+0x54>
lea    0xe31(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <strdup@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x9(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1213 <xdg_user_dir_lookup+0x93>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0xe09(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
jmp    1197 <xdg_user_dir_lookup+0x17>
xor    %ebx,%ebx
jmp    1197 <xdg_user_dir_lookup+0x17>

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

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [8 x i8] c"DESKTOP\00"
@.str.3 = [9 x i8] c"/Desktop\00"
define i8* @xdg_user_dir_lookup(i8* %0) {
%2 = tail call i8* @xdg_user_dir_lookup_with_fallback(i8* %0, i32* null)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %23
4: ; preds = %1
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call dereferenceable_or_null(5) i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
9: ; preds = %4
%10 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = tail call i64 @strlen(i8* %5)
%14 = add nsw i64 %13, 9
%15 = tail call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %23, label %18
18: ; preds = %12
%19 = tail call i32 @strcpy(i8* %16, i8* %5)
%20 = tail call i32 @strcat(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %23
21: ; preds = %9
%22 = tail call i8* @strdup(i8* %5)
br label %23
23: ; preds = %12, %1, %21, %18, %7
%24 = phi i8* [ %8, %7 ], [ %16, %18 ], [ %22, %21 ], [ %2, %1 ], [ null, %12 ]
ret i8* %24
}