#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 EXIT_SUCCESS;
int /*<<< orphan*/ FAIL(char *, ...);
int /*<<< orphan*/ PASS(char *);
int /*<<< orphan*/ RTLD_LAZY;
int /*<<< orphan*/ dlclose(void *);
int /*<<< orphan*/ dlerror();
void *dlopen(char *, int /*<<< orphan*/);
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  void *handle = dlopen("foo.bundle", RTLD_LAZY);
  if (handle == NULL) {
    FAIL("zero-fill-segment: dlopen(\"%s\") failed with: %s", "foo.bundle",
         dlerror());
    exit(0);
  }
  void *sym = dlsym(handle, "getfoo");
  if (sym == NULL) {
    FAIL("zero-fill-segment: dlsym(handle, \"getfoo\") failed");
    exit(0);
  }
  dlclose(handle);
  PASS("zero-fill-segment");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1040 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cf <main+0x5f>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %eax,%edx
lea    0xe51(%rip),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <main+0x93>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dlclose@plt>
lea    0xe61(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1040 <dlopen@plt>
test   %rax,%rax
je     11c1 <main+0x51>
mov    %rax,%rbx
lea    0xea6(%rip),%rsi
mov    %rax,%rdi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11e6 <main+0x76>
mov    %rbx,%rdi
call   1080 <dlclose@plt>
lea    0xec3(%rip),%rdi
call   1050 <PASS@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
lea    0xe3c(%rip),%rdi
lea    0xe2a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1090 <FAIL@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"foo.bundle\00"
@RTLD_LAZY = global i32 0
@.str.1 = [48 x i8] c"zero-fill-segment: dlopen(\22%s\22) failed with: %s\00"
@.str.2 = [7 x i8] c"getfoo\00"
@.str.3 = [50 x i8] c"zero-fill-segment: dlsym(handle, \22getfoo\22) failed\00"
@.str.4 = [18 x i8] c"zero-fill-segment\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @RTLD_LAZY
%9 = call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 (...) @dlerror()
%14 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = call i8* @dlsym(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %16
%25 = load i8*, i8** %6
%26 = call i32 @dlclose(i8* %25)
%27 = call i32 @PASS(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32, i32* @EXIT_SUCCESS
ret i32 %28
}

LLVM IR (-O2):
@.str = [11 x i8] c"foo.bundle\00"
@RTLD_LAZY = global i32 0
@.str.1 = [48 x i8] c"zero-fill-segment: dlopen(\22%s\22) failed with: %s\00"
@.str.2 = [7 x i8] c"getfoo\00"
@.str.3 = [50 x i8] c"zero-fill-segment: dlsym(handle, \22getfoo\22) failed\00"
@.str.4 = [18 x i8] c"zero-fill-segment\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @RTLD_LAZY
%4 = tail call i8* @dlopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 (...) @dlerror()
%8 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = tail call i8* @dlsym(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @FAIL(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %10
%17 = tail call i32 @dlclose(i8* %4)
%18 = tail call i32 @PASS(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32, i32* @EXIT_SUCCESS
ret i32 %19
}