#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(int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int strlen(char *);
void set_LD_PRELOAD(void) {
  char *preload = getenv("LD_PRELOAD");
  char *capturepath = getenv("OBSERVE_CAPTURE_SO");
  char *newpreload = NULL;
  if (preload == NULL) {
    newpreload = capturepath;
  } else {
    newpreload = (char *)malloc(
        (strlen(capturepath) + 1 + strlen(preload) + 1) * sizeof(char));
    sprintf(newpreload, "%s %s", capturepath, preload);
  }
  setenv("LD_PRELOAD", newpreload, 1);
}


x86 assembly (clang, -O0):
<set_LD_PRELOAD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
lea    0xe9c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1198 <set_LD_PRELOAD+0x48>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11e3 <set_LD_PRELOAD+0x93>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe40(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_LD_PRELOAD>:
push   %r15
push   %r14
push   %rbx
lea    0xea4(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
lea    0xea0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %r14,%r14
je     11b2 <set_LD_PRELOAD+0x62>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe7d(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1070 <sprintf@plt>
mov    %r15,%rbx
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <setenv@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"LD_PRELOAD\00"
@.str.1 = [19 x i8] c"OBSERVE_CAPTURE_SO\00"
@.str.2 = [6 x i8] c"%s %s\00"
define void @set_LD_PRELOAD() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %1
%5 = call i8* @getenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i8* %5, i8** %2
store i8* null, i8** %3
%6 = load i8*, i8** %1
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i8*, i8** %2
store i8* %9, i8** %3
br label %27
10: ; preds = %0
%11 = load i8*, i8** %2
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %12, 1
%14 = load i8*, i8** %1
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %13, %15
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 1
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = load i8*, i8** %2
%25 = load i8*, i8** %1
%26 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %25)
br label %27
27: ; preds = %10, %8
%28 = load i8*, i8** %3
%29 = call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %28, i32 1)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"LD_PRELOAD\00"
@.str.1 = [19 x i8] c"OBSERVE_CAPTURE_SO\00"
@.str.2 = [6 x i8] c"%s %s\00"
define void @set_LD_PRELOAD() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i8* %1, null
br i1 %3, label %12, label %4
4: ; preds = %0
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strlen(i8* %1)
%7 = add i32 %5, 2
%8 = add i32 %7, %6
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %1)
br label %12
12: ; preds = %0, %4
%13 = phi i8* [ %10, %4 ], [ %2, %0 ]
%14 = tail call i32 @setenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %13, i32 1)
ret void
}