#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 */
typedef int /*<<< orphan*/ pathname;
typedef int /*<<< orphan*/ ld_preload;

/* Variables and functions */
int PATH_MAX;
char *getenv(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
scalar_t__ strlen(char const *);
void ldpreload_extend(const char *lib_path, const char *file) {
  char pathname[PATH_MAX];
  if (strlen(lib_path) > 0) {
    snprintf(pathname, sizeof(pathname),
             "%s"
             "/"
             "%s",
             lib_path, file);
  } else {
    snprintf(pathname, sizeof(pathname), "%s", file);
  }
  char ld_preload[PATH_MAX];
  char *prev_ld_preload = getenv("LD_PRELOAD");
  if (prev_ld_preload == NULL) {
    snprintf(ld_preload, sizeof(ld_preload), "%s", pathname);
  } else {
    snprintf(ld_preload, sizeof(ld_preload), "%s %s", pathname,
             prev_ld_preload);
  }
  setenv("LD_PRELOAD", ld_preload, 1);
}


x86 assembly (clang, -O0):
<ldpreload_extend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    11b8 <ldpreload_extend+0x78>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe54(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    11d4 <ldpreload_extend+0x94>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe47(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe02(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1239 <ldpreload_extend+0xf9>
mov    -0x38(%rbp),%rcx
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
lea    0xde7(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    1259 <ldpreload_extend+0x119>
mov    -0x38(%rbp),%rcx
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%r8
lea    0xdbf(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x48(%rbp),%rsi
lea    0xda2(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ldpreload_extend>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e95(%rip),%r13
mov    0x0(%r13),%ebx
mov    %rsp,%r14
mov    %rbx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
call   1050 <strlen@plt>
test   %rax,%rax
jle    1198 <ldpreload_extend+0x58>
lea    0xe7c(%rip),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r12,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    11ae <ldpreload_extend+0x6e>
lea    0xe75(%rip),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x0(%r13),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe39(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f3 <ldpreload_extend+0xb3>
lea    0xe33(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    120a <ldpreload_extend+0xca>
lea    0xe1a(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
lea    0xdf5(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [11 x i8] c"LD_PRELOAD\00"
@.str.3 = [6 x i8] c"%s %s\00"
define void @ldpreload_extend(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i8*, i8** %3
%14 = call i64 @strlen(i8* %13)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = trunc i64 %10 to i32
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %12, i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19)
br label %25
21: ; preds = %2
%22 = trunc i64 %10 to i32
%23 = load i8*, i8** %4
%24 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %12, i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %16
%26 = load i32, i32* @PATH_MAX
%27 = zext i32 %26 to i64
%28 = alloca i8, i64 %27
store i64 %27, i64* %7
%29 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %25
%33 = trunc i64 %27 to i32
%34 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %28, i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %39
35: ; preds = %25
%36 = trunc i64 %27 to i32
%37 = load i8*, i8** %8
%38 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %28, i32 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %37)
br label %39
39: ; preds = %35, %32
%40 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 1)
%41 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [11 x i8] c"LD_PRELOAD\00"
@.str.3 = [6 x i8] c"%s %s\00"
define void @ldpreload_extend(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %12
10: ; preds = %2
%11 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %10, %8
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
%16 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %15, i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %22
20: ; preds = %12
%21 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %15, i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %16)
br label %22
22: ; preds = %20, %18
%23 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %15, i32 1)
ret void
}