#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*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
void *dlopen(char *, int /*<<< orphan*/);
int /*<<< orphan*/ mtrace();
int /*<<< orphan*/ puts(char *);
int main(void) {
  mtrace();
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    void *h =
        dlopen(i < 5 ? "./tst-leaks1.c" : "$ORIGIN/tst-leaks1.o", RTLD_LAZY);
    if (h != NULL) {
      puts("dlopen unexpectedly succeeded");
      ret = 1;
      dlclose(h);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <mtrace@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11d2 <main+0x92>
mov    -0xc(%rbp),%ecx
lea    0xe97(%rip),%rdi
lea    0xe81(%rip),%rax
cmp    $0x5,%ecx
cmovl  %rax,%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11bf <main+0x7f>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <dlclose@plt>
jmp    11c4 <main+0x84>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1164 <main+0x24>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <mtrace@plt>
mov    0x2e8c(%rip),%r14
mov    (%r14),%esi
lea    0xea2(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1184 <main+0x44>
mov    %rax,%rbx
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xe72(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11b4 <main+0x74>
mov    %rax,%rbx
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xe42(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11e4 <main+0xa4>
mov    %rax,%rbx
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xe12(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1214 <main+0xd4>
mov    %rax,%rbx
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xde2(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1244 <main+0x104>
mov    %rax,%rbx
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xdc1(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1274 <main+0x134>
mov    %rax,%rbx
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xd91(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     12a4 <main+0x164>
mov    %rax,%rbx
lea    0xd92(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xd61(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     12d4 <main+0x194>
mov    %rax,%rbx
lea    0xd62(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xd31(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1304 <main+0x1c4>
mov    %rax,%rbx
lea    0xd32(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
lea    0xd01(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     1334 <main+0x1f4>
mov    %rax,%rbx
lea    0xd02(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <dlclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"./tst-leaks1.c\00"
@.str.1 = [21 x i8] c"$ORIGIN/tst-leaks1.o\00"
@RTLD_LAZY = global i32 0
@.str.2 = [30 x i8] c"dlopen unexpectedly succeeded\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i32 (...) @mtrace()
store i32 0, i32* %2
store i32 0, i32* %3
br label %6
6: ; preds = %23, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 10
br i1 %8, label %9, label %26
9: ; preds = %6
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 5
%12 = zext i1 %11 to i64
%13 = select i1 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0)
%14 = load i32, i32* @RTLD_LAZY
%15 = call i8* @dlopen(i8* %13, i32 %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %9
%19 = call i32 @puts(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
%20 = load i8*, i8** %4
%21 = call i32 @dlclose(i8* %20)
br label %22
22: ; preds = %18, %9
br label %23
23: ; preds = %22
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %6
26: ; preds = %6
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [15 x i8] c"./tst-leaks1.c\00"
@.str.1 = [21 x i8] c"$ORIGIN/tst-leaks1.o\00"
@RTLD_LAZY = global i32 0
@.str.2 = [30 x i8] c"dlopen unexpectedly succeeded\00"
define i32 @main() {
%1 = tail call i32 (...) @mtrace()
%2 = load i32, i32* @RTLD_LAZY
%3 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @dlclose(i8* %3)
br label %8
8: ; preds = %5, %0
%9 = phi i32 [ 1, %5 ], [ 0, %0 ]
%10 = load i32, i32* @RTLD_LAZY
%11 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @dlclose(i8* %11)
br label %16
16: ; preds = %13, %8
%17 = phi i32 [ 1, %13 ], [ %9, %8 ]
%18 = load i32, i32* @RTLD_LAZY
%19 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @dlclose(i8* %19)
br label %24
24: ; preds = %21, %16
%25 = phi i32 [ 1, %21 ], [ %17, %16 ]
%26 = load i32, i32* @RTLD_LAZY
%27 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @dlclose(i8* %27)
br label %32
32: ; preds = %29, %24
%33 = phi i32 [ 1, %29 ], [ %25, %24 ]
%34 = load i32, i32* @RTLD_LAZY
%35 = tail call i8* @dlopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%39 = tail call i32 @dlclose(i8* %35)
br label %40
40: ; preds = %37, %32
%41 = phi i32 [ 1, %37 ], [ %33, %32 ]
%42 = load i32, i32* @RTLD_LAZY
%43 = tail call i8* @dlopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = icmp eq i8* %43, null
br i1 %44, label %48, label %45
45: ; preds = %40
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%47 = tail call i32 @dlclose(i8* %43)
br label %48
48: ; preds = %45, %40
%49 = phi i32 [ 1, %45 ], [ %41, %40 ]
%50 = load i32, i32* @RTLD_LAZY
%51 = tail call i8* @dlopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %50)
%52 = icmp eq i8* %51, null
br i1 %52, label %56, label %53
53: ; preds = %48
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%55 = tail call i32 @dlclose(i8* %51)
br label %56
56: ; preds = %53, %48
%57 = phi i32 [ 1, %53 ], [ %49, %48 ]
%58 = load i32, i32* @RTLD_LAZY
%59 = tail call i8* @dlopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %58)
%60 = icmp eq i8* %59, null
br i1 %60, label %64, label %61
61: ; preds = %56
%62 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%63 = tail call i32 @dlclose(i8* %59)
br label %64
64: ; preds = %61, %56
%65 = phi i32 [ 1, %61 ], [ %57, %56 ]
%66 = load i32, i32* @RTLD_LAZY
%67 = tail call i8* @dlopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %66)
%68 = icmp eq i8* %67, null
br i1 %68, label %72, label %69
69: ; preds = %64
%70 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%71 = tail call i32 @dlclose(i8* %67)
br label %72
72: ; preds = %69, %64
%73 = phi i32 [ 1, %69 ], [ %65, %64 ]
%74 = load i32, i32* @RTLD_LAZY
%75 = tail call i8* @dlopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %74)
%76 = icmp eq i8* %75, null
br i1 %76, label %80, label %77
77: ; preds = %72
%78 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%79 = tail call i32 @dlclose(i8* %75)
br label %80
80: ; preds = %77, %72
%81 = phi i32 [ 1, %77 ], [ %73, %72 ]
ret i32 %81
}