#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_6__ TYPE_2__;
typedef struct TYPE_5__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  int r_offset;
  int /*<<< orphan*/ r_info;
};
struct TYPE_5__ {
  int sh_size;
  scalar_t__ sh_offset;
};
typedef TYPE_1__ Elf32_Shdr;
typedef TYPE_2__ Elf32_Rel;

/* Variables and functions */
int ELF32_R_SYM(int /*<<< orphan*/);
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, TYPE_2__ *, int);
void do_rel(int *plt, int *offset, int fd, Elf32_Shdr *shdr, int sym) {
  Elf32_Rel *rel, *relp;
  int i;
  rel = (Elf32_Rel *)malloc(shdr->sh_size);
  if (rel == NULL) {
    perror("malloc");
    exit(1);
  }
  if (lseek(fd, shdr->sh_offset, SEEK_SET) != shdr->sh_offset) {
    perror("lseek");
    exit(1);
  }
  if (read(fd, rel, shdr->sh_size) != shdr->sh_size) {
    perror("read");
    exit(1);
  }
  relp = rel;
  for (i = 0; i < shdr->sh_size; i += sizeof(Elf32_Rel)) {
    if (ELF32_R_SYM(relp->r_info) == sym) {
      *plt = relp->r_offset;
      *offset = relp - rel;
      printf("offset %i", *offset);
      return;
    }
    ++relp;
  }
  *plt = -1;
  *offset = -1;
}


x86 assembly (clang, -O0):
<do_rel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bb <do_rel+0x4b>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
mov    -0x20(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     11f8 <do_rel+0x88>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
je     122c <do_rel+0xbc>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jge    12b5 <do_rel+0x145>
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
call   1030 <ELF32_R_SYM@plt>
cmp    -0x24(%rbp),%eax
jne    1299 <do_rel+0x129>
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
sar    $0x3,%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xd83(%rip),%rdi
call   1040 <printf@plt>
jmp    12c9 <do_rel+0x159>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
movslq -0x3c(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x3c(%rbp)
jmp    123b <do_rel+0xcb>
mov    -0x8(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r12d
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rcx),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     122f <do_rel+0xbf>
mov    %rax,%r13
mov    0x8(%rbx),%rsi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1050 <lseek@plt>
cmp    0x8(%rbx),%rax
jne    1238 <do_rel+0xc8>
mov    (%rbx),%edx
mov    %ebp,%edi
mov    %r13,%rsi
call   1060 <read@plt>
cmp    (%rbx),%eax
jne    1241 <do_rel+0xd1>
test   %eax,%eax
jle    11e7 <do_rel+0x77>
xor    %ebp,%ebp
mov    0x4(%r13,%rbp,1),%edi
call   1030 <ELF32_R_SYM@plt>
cmp    %r12d,%eax
je     1204 <do_rel+0x94>
add    $0x8,%rbp
cmp    (%rbx),%ebp
jl     11d0 <do_rel+0x60>
movl   $0xffffffff,(%r15)
movl   $0xffffffff,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%r13,%rbp,1),%eax
mov    %eax,(%r15)
shr    $0x3,%rbp
mov    %ebp,(%r14)
lea    0xdf8(%rip),%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
lea    0xdca(%rip),%rdi
jmp    1248 <do_rel+0xd8>
lea    0xdc8(%rip),%rdi
jmp    1248 <do_rel+0xd8>
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32, i64 }
%struct.TYPE_6__ = type { i32, i32 }
@.str = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [10 x i8] c"offset %i\00"
define void @do_rel(i32* %0, i32* %1, i32 %2, %struct.TYPE_5__* %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.TYPE_5__*
%10 = alloca i32
%11 = alloca %struct.TYPE_6__*
%12 = alloca %struct.TYPE_6__*
%13 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store %struct.TYPE_5__* %3, %struct.TYPE_5__** %9
store i32 %4, i32* %10
%14 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%15 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to %struct.TYPE_6__*
store %struct.TYPE_6__* %18, %struct.TYPE_6__** %11
%19 = load %struct.TYPE_6__*, %struct.TYPE_6__** %11
%20 = icmp eq %struct.TYPE_6__* %19, null
br i1 %20, label %21, label %24
21: ; preds = %5
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %5
%25 = load i32, i32* %8
%26 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%27 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %26, i32 0, i32 1
%28 = load i64, i64* %27
%29 = load i32, i32* @SEEK_SET
%30 = call i64 @lseek(i32 %25, i64 %28, i32 %29)
%31 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%32 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %31, i32 0, i32 1
%33 = load i64, i64* %32
%34 = icmp ne i64 %30, %33
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %24
%39 = load i32, i32* %8
%40 = load %struct.TYPE_6__*, %struct.TYPE_6__** %11
%41 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%42 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @read(i32 %39, %struct.TYPE_6__* %40, i32 %43)
%45 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%46 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = icmp ne i32 %44, %47
br i1 %48, label %49, label %52
49: ; preds = %38
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %38
%53 = load %struct.TYPE_6__*, %struct.TYPE_6__** %11
store %struct.TYPE_6__* %53, %struct.TYPE_6__** %12
store i32 0, i32* %13
br label %54
54: ; preds = %86, %52
%55 = load i32, i32* %13
%56 = load %struct.TYPE_5__*, %struct.TYPE_5__** %9
%57 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = icmp slt i32 %55, %58
br i1 %59, label %60, label %91
60: ; preds = %54
%61 = load %struct.TYPE_6__*, %struct.TYPE_6__** %12
%62 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %61, i32 0, i32 1
%63 = load i32, i32* %62
%64 = call i32 @ELF32_R_SYM(i32 %63)
%65 = load i32, i32* %10
%66 = icmp eq i32 %64, %65
br i1 %66, label %67, label %83
67: ; preds = %60
%68 = load %struct.TYPE_6__*, %struct.TYPE_6__** %12
%69 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %68, i32 0, i32 0
%70 = load i32, i32* %69
%71 = load i32*, i32** %6
store i32 %70, i32* %71
%72 = load %struct.TYPE_6__*, %struct.TYPE_6__** %12
%73 = load %struct.TYPE_6__*, %struct.TYPE_6__** %11
%74 = ptrtoint %struct.TYPE_6__* %72 to i64
%75 = ptrtoint %struct.TYPE_6__* %73 to i64
%76 = sub i64 %74, %75
%77 = sdiv exact i64 %76, 8
%78 = trunc i64 %77 to i32
%79 = load i32*, i32** %7
store i32 %78, i32* %79
%80 = load i32*, i32** %7
%81 = load i32, i32* %80
%82 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %81)
br label %94
83: ; preds = %60
%84 = load %struct.TYPE_6__*, %struct.TYPE_6__** %12
%85 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %84, i32 1
store %struct.TYPE_6__* %85, %struct.TYPE_6__** %12
br label %86
86: ; preds = %83
%87 = load i32, i32* %13
%88 = sext i32 %87 to i64
%89 = add i64 %88, 8
%90 = trunc i64 %89 to i32
store i32 %90, i32* %13
br label %54
91: ; preds = %54
%92 = load i32*, i32** %6
store i32 -1, i32* %92
%93 = load i32*, i32** %7
store i32 -1, i32* %93
br label %94
94: ; preds = %91, %67
ret void
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i64 }
%struct.TYPE_6__ = type { i32, i32 }
@.str = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [10 x i8] c"offset %i\00"
define void @do_rel(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i32 %2, %struct.TYPE_5__* nocapture %3, i32 %4) {
%6 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to %struct.TYPE_6__*
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 1
%16 = load i64, i64* %15
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @lseek(i32 %2, i64 %16, i32 %17)
%19 = load i64, i64* %15
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i32, i32* %6
%26 = tail call i32 @read(i32 %2, %struct.TYPE_6__* %9, i32 %25)
%27 = load i32, i32* %6
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = icmp sgt i32 %26, 0
br i1 %30, label %34, label %54
31: ; preds = %24
%32 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %29, %49
%35 = phi i32 [ %51, %49 ], [ 0, %29 ]
%36 = phi %struct.TYPE_6__* [ %50, %49 ], [ %9, %29 ]
%37 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i64 0, i32 1
%38 = load i32, i32* %37
%39 = tail call i32 @ELF32_R_SYM(i32 %38)
%40 = icmp eq i32 %39, %4
br i1 %40, label %41, label %49
41: ; preds = %34
%42 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i64 0, i32 0
%43 = load i32, i32* %42
store i32 %43, i32* %0
%44 = ptrtoint %struct.TYPE_6__* %36 to i64
%45 = sub i64 %44, %8
%46 = lshr exact i64 %45, 3
%47 = trunc i64 %46 to i32
store i32 %47, i32* %1
%48 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %55
49: ; preds = %34
%50 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i64 1
%51 = add i32 %35, 8
%52 = load i32, i32* %6
%53 = icmp slt i32 %51, %52
br i1 %53, label %34, label %54
54: ; preds = %49, %29
store i32 -1, i32* %0
store i32 -1, i32* %1
br label %55
55: ; preds = %54, %41
ret void
}