#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 */
typedef void (*func)();

/* Variables and functions */
char *lt_dlerror();
int /*<<< orphan*/ lt_dlinit();
void *lt_dlopen(int /*<<< orphan*/);
scalar_t__ lt_dlsym(void *, char *);
int /*<<< orphan*/ printf(char *, char *);
int main() {
  typedef void (*func)();
  void *soh = 0;
  lt_dlinit();
  soh = lt_dlopen(0);
  if (!soh) {
    printf("could not open main app: %s\n", lt_dlerror());
    return 1;
  }
  void *sym = (func)lt_dlsym(soh, "foo_function");
  if (0 == sym) {
    printf("could not find test symbol: %s\n", lt_dlerror());
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1060 <lt_dlinit@plt>
xor    %edi,%edi
call   1050 <lt_dlopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <main+0x56>
mov    $0x0,%al
call   1030 <lt_dlerror@plt>
mov    %rax,%rsi
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ef <main+0x9f>
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <lt_dlsym@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11e8 <main+0x98>
mov    $0x0,%al
call   1030 <lt_dlerror@plt>
mov    %rax,%rsi
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    11ef <main+0x9f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <lt_dlinit@plt>
xor    %edi,%edi
call   1050 <lt_dlopen@plt>
test   %rax,%rax
je     118b <main+0x3b>
lea    0xead(%rip),%rsi
mov    %rax,%rdi
call   1070 <lt_dlsym@plt>
test   %rax,%rax
jne    11a9 <main+0x59>
mov    $0x2,%ebx
lea    0xea1(%rip),%r14
jmp    1197 <main+0x47>
mov    $0x1,%ebx
lea    0xe69(%rip),%r14
xor    %eax,%eax
call   1030 <lt_dlerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [29 x i8] c"could not open main app: %s\0A\00"
@.str.1 = [13 x i8] c"foo_function\00"
@.str.2 = [32 x i8] c"could not find test symbol: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
store i32 0, i32* %1
store i8* null, i8** %2
%4 = call i32 (...) @lt_dlinit()
%5 = call i8* @lt_dlopen(i32 0)
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i8* (...) @lt_dlerror()
%10 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %9)
store i32 1, i32* %1
br label %22
11: ; preds = %0
%12 = load i8*, i8** %2
%13 = call i64 @lt_dlsym(i8* %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%14 = inttoptr i64 %13 to void (...)*
%15 = bitcast void (...)* %14 to i8*
store i8* %15, i8** %3
%16 = load i8*, i8** %3
%17 = icmp eq i8* null, %16
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i8* (...) @lt_dlerror()
%20 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %19)
store i32 2, i32* %1
br label %22
21: ; preds = %11
store i32 0, i32* %1
br label %22
22: ; preds = %21, %18, %8
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [29 x i8] c"could not open main app: %s\0A\00"
@.str.1 = [13 x i8] c"foo_function\00"
@.str.2 = [32 x i8] c"could not find test symbol: %s\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @lt_dlinit()
%2 = tail call i8* @lt_dlopen(i32 0)
%3 = icmp eq i8* %2, null
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i64 @lt_dlsym(i8* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4, %0
%8 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), %4 ]
%9 = phi i32 [ 1, %0 ], [ 2, %4 ]
%10 = tail call i8* (...) @lt_dlerror()
%11 = tail call i32 @printf(i8* %8, i8* %10)
br label %12
12: ; preds = %7, %4
%13 = phi i32 [ 0, %4 ], [ %9, %7 ]
ret i32 %13
}