#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 */
typedef struct TYPE_8__ TYPE_3__;
typedef struct TYPE_7__ TYPE_2__;
typedef struct TYPE_6__ TYPE_1__;

/* Type definitions */
struct TYPE_8__ {
  int /*<<< orphan*/ r_offset;
  int /*<<< orphan*/ r_info;
};
struct TYPE_7__ {
  int sh_offset;
  int sh_size;
};
struct TYPE_6__ {
  int st_name;
};
typedef int /*<<< orphan*/ Elf32_Word;
typedef TYPE_1__ Elf32_Sym;
typedef TYPE_2__ Elf32_Shdr;
typedef TYPE_3__ Elf32_Rel;

/* Variables and functions */
size_t ELF32_R_SYM(int /*<<< orphan*/);
int ELF32_R_TYPE(int /*<<< orphan*/);
#define R_386_GLOB_DAT 129
#define R_386_JMP_SLOT 128
int /*<<< orphan*/ resolve(char const *);
void relocate(Elf32_Shdr *shdr, const Elf32_Sym *syms, const char *strings,
              const char *src, char *dst) {
  Elf32_Rel *rel = (Elf32_Rel *)(src + shdr->sh_offset);
  int j;
  for (j = 0; j < shdr->sh_size / sizeof(Elf32_Rel); j += 1) {
    const char *sym = strings + syms[ELF32_R_SYM(rel[j].r_info)].st_name;
    switch (ELF32_R_TYPE(rel[j].r_info)) {
    case R_386_JMP_SLOT:
    case R_386_GLOB_DAT:
      *(Elf32_Word *)(dst + rel[j].r_offset) = (Elf32_Word)resolve(sym);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<relocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq 0x4(%rcx),%rcx
shr    $0x3,%rcx
cmp    %rcx,%rax
jae    1208 <relocate+0xd8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
call   1030 <ELF32_R_SYM@plt>
mov    -0x50(%rbp),%rcx
mov    %rax,%rdx
mov    -0x48(%rbp),%rax
movslq (%rcx,%rdx,4),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
call   1050 <ELF32_R_TYPE@plt>
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11f5 <relocate+0xc5>
jmp    11d7 <relocate+0xa7>
mov    -0x40(%rbp),%rdi
call   1040 <resolve@plt>
mov    %eax,%edx
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movslq -0x34(%rbp),%rsi
movslq (%rcx,%rsi,8),%rcx
mov    %edx,(%rax,%rcx,1)
jmp    11fa <relocate+0xca>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1165 <relocate+0x35>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<relocate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,(%rsp)
cmpl   $0x8,0x4(%rdi)
jae    1154 <relocate+0x24>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
movslq (%rdi),%rax
lea    (%rax,%rcx,1),%rbp
add    $0x4,%rbp
xor    %r14d,%r14d
jmp    1181 <relocate+0x51>
nopl   (%rax)
add    $0x1,%r14
movslq 0x4(%r13),%rax
shr    $0x3,%rax
cmp    %r14,%rax
jbe    1145 <relocate+0x15>
mov    0x0(%rbp,%r14,8),%edi
call   1030 <ELF32_R_SYM@plt>
movslq (%r12,%rax,4),%rbx
mov    0x0(%rbp,%r14,8),%edi
call   1050 <ELF32_R_TYPE@plt>
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    1170 <relocate+0x40>
add    %r15,%rbx
mov    %rbx,%rdi
call   1040 <resolve@plt>
movslq -0x4(%rbp,%r14,8),%rcx
mov    (%rsp),%rdx
mov    %eax,(%rdx,%rcx,1)
jmp    1170 <relocate+0x40>

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32, i32 }
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_8__ = type { i32, i32 }
define void @relocate(%struct.TYPE_7__* %0, %struct.TYPE_6__* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca %struct.TYPE_7__*
%7 = alloca %struct.TYPE_6__*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.TYPE_8__*
%12 = alloca i32
%13 = alloca i8*
store %struct.TYPE_7__* %0, %struct.TYPE_7__** %6
store %struct.TYPE_6__* %1, %struct.TYPE_6__** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%14 = load i8*, i8** %9
%15 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%16 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %14, i64 %18
%20 = bitcast i8* %19 to %struct.TYPE_8__*
store %struct.TYPE_8__* %20, %struct.TYPE_8__** %11
store i32 0, i32* %12
br label %21
21: ; preds = %66, %5
%22 = load i32, i32* %12
%23 = sext i32 %22 to i64
%24 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%25 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = udiv i64 %27, 8
%29 = icmp ult i64 %23, %28
br i1 %29, label %30, label %69
30: ; preds = %21
%31 = load i8*, i8** %8
%32 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%33 = load %struct.TYPE_8__*, %struct.TYPE_8__** %11
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %33, i64 %35
%37 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i64 @ELF32_R_SYM(i32 %38)
%40 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %32, i64 %39
%41 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %31, i64 %43
store i8* %44, i8** %13
%45 = load %struct.TYPE_8__*, %struct.TYPE_8__** %11
%46 = load i32, i32* %12
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %45, i64 %47
%49 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i32 @ELF32_R_TYPE(i32 %50)
switch i32 %51, label %65 [
i32 128, label %52
i32 129, label %52
]
52: ; preds = %30, %30
%53 = load i8*, i8** %13
%54 = call i32 @resolve(i8* %53)
%55 = load i8*, i8** %10
%56 = load %struct.TYPE_8__*, %struct.TYPE_8__** %11
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %56, i64 %58
%60 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %55, i64 %62
%64 = bitcast i8* %63 to i32*
store i32 %54, i32* %64
br label %65
65: ; preds = %30, %52
br label %66
66: ; preds = %65
%67 = load i32, i32* %12
%68 = add nsw i32 %67, 1
store i32 %68, i32* %12
br label %21
69: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32, i32 }
%struct.TYPE_6__ = type { i32 }
%struct.TYPE_8__ = type { i32, i32 }
define void @relocate(%struct.TYPE_7__* nocapture %0, %struct.TYPE_6__* nocapture %1, i8* %2, i8* nocapture %3, i8* nocapture writeonly %4) {
%6 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 0
%7 = load i32, i32* %6
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %3, i64 %8
%10 = bitcast i8* %9 to %struct.TYPE_8__*
%11 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 1
%12 = load i32, i32* %11
%13 = icmp ult i32 %12, 8
br i1 %13, label %40, label %14
14: ; preds = %5, %34
%15 = phi i64 [ %35, %34 ], [ 0, %5 ]
%16 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %10, i64 %15, i32 1
%17 = load i32, i32* %16
%18 = tail call i64 @ELF32_R_SYM(i32 %17)
%19 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %1, i64 %18, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* %16
%22 = tail call i32 @ELF32_R_TYPE(i32 %21)
%23 = and i32 %22, -2
%24 = icmp eq i32 %23, 128
br i1 %24, label %25, label %34
25: ; preds = %14
%26 = sext i32 %20 to i64
%27 = getelementptr inbounds i8, i8* %2, i64 %26
%28 = tail call i32 @resolve(i8* %27)
%29 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %10, i64 %15, i32 0
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %4, i64 %31
%33 = bitcast i8* %32 to i32*
store i32 %28, i32* %33
br label %34
34: ; preds = %14, %25
%35 = add nuw nsw i64 %15, 1
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = lshr i64 %37, 3
%39 = icmp ugt i64 %38, %35
br i1 %39, label %14, label %40
40: ; preds = %34, %5
ret void
}