#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *getenv(char *);
int /*<<< orphan*/ loadso(char const *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int main(int argc, char *argv[]) {
  const char *so_name = "libfoo.so";
  const char *so_dir = "/home/extend/test/libs";
  char ld_path[1024] = {0};
  const char *psz_ld_path = getenv("LD_LIBRARY_PATH");
  const char *psz_path = getenv("PATH");
  if (psz_ld_path != NULL)
    strcpy(ld_path, psz_ld_path);
  sprintf(ld_path + strlen(ld_path), ":%s", so_dir);
  fprintf(stdout, "org: ld_path = %s\n", psz_ld_path);
  loadso(so_name);
  setenv("LD_LIBRARY_PATH", ld_path, 1);
  psz_ld_path = getenv("LD_LIBRARY_PATH");
  fprintf(stdout, "new: ld_path = %s\n", psz_ld_path);
  loadso(so_name);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe5f(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe58(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x428(%rbp)
lea    0xe50(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     1209 <main+0x89>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x420(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x438(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x20(%rbp),%rdx
lea    0xdf7(%rip),%rsi
call   1090 <sprintf@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xddf(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <loadso@plt>
lea    -0x420(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
lea    0xd9e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x428(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xda2(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <loadso@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
test   %rbx,%rbx
je     11c8 <main+0x48>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
lea    0xe51(%rip),%rsi
lea    0xe23(%rip),%rdx
call   1090 <sprintf@plt>
mov    0x2ded(%rip),%r12
mov    (%r12),%edi
lea    0xe37(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
lea    0xdf3(%rip),%r15
mov    %r15,%rdi
call   10a0 <loadso@plt>
lea    0xe05(%rip),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbx,%rdi
call   1030 <getenv@plt>
mov    (%r12),%edi
lea    0xe09(%rip),%rsi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   10a0 <loadso@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"libfoo.so\00"
@.str.1 = [23 x i8] c"/home/extend/test/libs\00"
@.str.2 = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [4 x i8] c":%s\00"
@stdout = global i32 0
@.str.5 = [19 x i8] c"org: ld_path = %s\0A\00"
@.str.6 = [19 x i8] c"new: ld_path = %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8** %7
%11 = bitcast [1024 x i8]* %8 to i8*
%12 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i8* %12, i8** %9
%13 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %13, i8** %10
%14 = load i8*, i8** %9
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = load i8*, i8** %9
%19 = call i32 @strcpy(i8* %17, i8* %18)
br label %20
20: ; preds = %16, %2
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = load i8*, i8** %7
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @stdout
%29 = load i8*, i8** %9
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %6
%32 = call i32 @loadso(i8* %31)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%34 = call i32 @setenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 1)
%35 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %9
%36 = load i32, i32* @stdout
%37 = load i8*, i8** %9
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %6
%40 = call i32 @loadso(i8* %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"libfoo.so\00"
@.str.1 = [23 x i8] c"/home/extend/test/libs\00"
@.str.2 = [16 x i8] c"LD_LIBRARY_PATH\00"
@.str.3 = [5 x i8] c"PATH\00"
@.str.4 = [4 x i8] c":%s\00"
@stdout = global i32 0
@.str.5 = [19 x i8] c"org: ld_path = %s\0A\00"
@.str.6 = [19 x i8] c"new: ld_path = %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%7 = icmp eq i8* %5, null
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 @strcpy(i8* %4, i8* %5)
br label %10
10: ; preds = %8, %2
%11 = call i32 @strlen(i8* %4)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %12
%14 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%17 = call i32 @loadso(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @setenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 1)
%19 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @stdout
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %19)
%22 = call i32 @loadso(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
ret i32 0
}