#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*/ abort();
char *concat(char *, int, char const *);
char *getenv(char *);
int /*<<< orphan*/ strcmp(char *, char const *);
int handle_lmpl(char **dest, char *cur, size_t *pos) {
  char const *fname = getenv("BUILDSH_PRELOAD");
  if (fname == NULL)
    abort();
  if (cur != NULL && !strcmp(cur + sizeof("BUILDSH_PRELOAD=") - 1, fname)) {
    dest[*pos] = cur;
    *pos = -1;
    return 1;
  } else {
    char *mem =
        concat("BUILDSH_PRELOAD=", sizeof("BUILDSH_PRELOAD=") - 1, fname);
    dest[*pos] = mem;
    return mem != NULL;
  }
}


x86 assembly (clang, -O0):
<handle_lmpl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xea5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <handle_lmpl+0x36>
mov    $0x0,%al
call   1040 <abort@plt>
cmpq   $0x0,-0x18(%rbp)
je     11c9 <handle_lmpl+0x89>
mov    -0x18(%rbp),%rdi
add    $0x11,%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x28(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11c9 <handle_lmpl+0x89>
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movq   $0xffffffffffffffff,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1205 <handle_lmpl+0xc5>
mov    -0x28(%rbp),%rdx
lea    0xe3c(%rip),%rdi
mov    $0x10,%esi
call   1050 <concat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rdx,(%rax,%rcx,8)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_lmpl>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xea8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11c2 <handle_lmpl+0x82>
mov    %rax,%rbx
test   %r12,%r12
je     117b <handle_lmpl+0x3b>
lea    0x10(%r12),%rdi
mov    %rbx,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11a3 <handle_lmpl+0x63>
lea    0xe8e(%rip),%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
call   1050 <concat@plt>
mov    %rax,%rcx
mov    (%r15),%rax
mov    %rcx,(%r14,%rax,8)
xor    %eax,%eax
test   %rcx,%rcx
setne  %al
jmp    11b6 <handle_lmpl+0x76>
mov    (%r15),%rax
mov    %r12,(%r14,%rax,8)
movq   $0xffffffffffffffff,(%r15)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1040 <abort@plt>

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

LLVM IR (-O2):
@.str = [16 x i8] c"BUILDSH_PRELOAD\00"
@.str.1 = [17 x i8] c"BUILDSH_PRELOAD=\00"
define i32 @handle_lmpl(i8** nocapture writeonly %0, i8* %1, i64* nocapture %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (...) @abort()
unreachable
8: ; preds = %3
%9 = icmp eq i8* %1, null
br i1 %9, label %17, label %10
10: ; preds = %8
%11 = getelementptr inbounds i8, i8* %1, i64 16
%12 = tail call i32 @strcmp(i8* %11, i8* %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i64, i64* %2
%16 = getelementptr inbounds i8*, i8** %0, i64 %15
store i8* %1, i8** %16
store i64 -1, i64* %2
br label %23
17: ; preds = %10, %8
%18 = tail call i8* @concat(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 16, i8* %4)
%19 = load i64, i64* %2
%20 = getelementptr inbounds i8*, i8** %0, i64 %19
store i8* %18, i8** %20
%21 = icmp ne i8* %18, null
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %17, %14
%24 = phi i32 [ %22, %17 ], [ 1, %14 ]
ret i32 %24
}