#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 */
void *dlsym(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *, ...);
void __attribute__((constructor)) so_ctor(void) {
  int *my_variable_sym;
  printf(
      "%s: Constructor\n",
      "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
      "2022-01-22/02-19-57/repos/frdeso/fun-with-so/refs/heads/master/aaa.c");
  void *obj = dlsym(NULL, "my_variable");
  if (obj == NULL) {
    printf("dlsym error\n");
    return;
  }
  my_variable_sym = (int *)obj;
  printf("my_variable = %d\n\n", *my_variable_sym);
}


x86 assembly (clang, -O0):
<so_ctor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xed1(%rip),%rdi
lea    0xedb(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xf4e(%rip),%rsi
call   1040 <dlsym@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116f <so_ctor+0x4f>
lea    0xf3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    118b <so_ctor+0x6b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xf2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<so_ctor>:
push   %rax
lea    0xec8(%rip),%rdi
lea    0xed2(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf49(%rip),%rsi
xor    %edi,%edi
call   1050 <dlsym@plt>
test   %rax,%rax
je     116a <so_ctor+0x3a>
mov    (%rax),%esi
lea    0xf40(%rip),%rdi
xor    %eax,%eax
pop    %rcx
jmp    1040 <printf@plt>
lea    0xf44(%rip),%rdi
pop    %rax
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"%s: Constructor\0A\00"
@.str.1 = [133 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/frdeso/fun-with-so/refs/heads/master/aaa.c\00"
@.str.2 = [12 x i8] c"my_variable\00"
@.str.3 = [13 x i8] c"dlsym error\0A\00"
@.str.4 = [19 x i8] c"my_variable = %d\0A\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @so_ctor, i8* null }]
define void @so_ctor() {
%1 = alloca i32*
%2 = alloca i8*
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0))
%4 = call i8* @dlsym(i32* null, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %15
9: ; preds = %0
%10 = load i8*, i8** %2
%11 = bitcast i8* %10 to i32*
store i32* %11, i32** %1
%12 = load i32*, i32** %1
%13 = load i32, i32* %12
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %9, %7
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"%s: Constructor\0A\00"
@.str.1 = [133 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/frdeso/fun-with-so/refs/heads/master/aaa.c\00"
@.str.2 = [12 x i8] c"my_variable\00"
@.str.4 = [19 x i8] c"my_variable = %d\0A\0A\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @so_ctor, i8* null }]
@str = [12 x i8] c"dlsym error\00"
define void @so_ctor() {
%1 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([133 x i8], [133 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i8* @dlsym(i32* null, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %10
6: ; preds = %0
%7 = bitcast i8* %2 to i32*
%8 = load i32, i32* %7
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %6, %4
ret void
}