#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_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  size_t sh_link;
  scalar_t__ sh_type;
  size_t sh_info;
};
typedef TYPE_1__ Elf64_Shdr;

/* Variables and functions */
scalar_t__ SHT_REL;
scalar_t__ SHT_RELA;
TYPE_1__ *get_s_header(char *, int /*<<< orphan*/);
int fix_sh_links(char *elf, size_t index) {
  if (!elf)
    return 1;
  Elf64_Shdr *shdr;
  int i;
  i = 0;
  while ((shdr = get_s_header(elf, i++))) {
    if (shdr->sh_link >= index)
      shdr->sh_link++;
    if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
      if (shdr->sh_info >= index)
        shdr->sh_info++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fix_sh_links>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1137 <fix_sh_links+0x27>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <fix_sh_links+0xd2>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
call   1030 <get_s_header@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11db <fix_sh_links+0xcb>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    -0x18(%rbp),%rax
jb     117f <fix_sh_links+0x6f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
je     11af <fix_sh_links+0x9f>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11d6 <fix_sh_links+0xc6>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
cmp    -0x18(%rbp),%rax
jb     11d1 <fix_sh_links+0xc1>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rax)
jmp    11d6 <fix_sh_links+0xc6>
jmp    113e <fix_sh_links+0x2e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_sh_links>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1193 <fix_sh_links+0x83>
mov    %rsi,%rbx
mov    %rdi,%r13
xor    %r14d,%r14d
xor    %esi,%esi
call   1030 <get_s_header@plt>
test   %rax,%rax
je     1199 <fix_sh_links+0x89>
mov    $0x1,%esi
mov    0x2e9f(%rip),%r15
mov    0x2ea0(%rip),%r12
jmp    1162 <fix_sh_links+0x52>
nopw   0x0(%rax,%rax,1)
lea    0x1(%rsi),%ebp
mov    %r13,%rdi
call   1030 <get_s_header@plt>
mov    %ebp,%esi
test   %rax,%rax
je     1199 <fix_sh_links+0x89>
mov    (%rax),%rcx
cmp    %rbx,%rcx
jb     1171 <fix_sh_links+0x61>
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    0x8(%rax),%rcx
cmp    (%r15),%rcx
je     1180 <fix_sh_links+0x70>
cmp    (%r12),%rcx
jne    1150 <fix_sh_links+0x40>
mov    0x10(%rax),%rcx
cmp    %rbx,%rcx
jb     1150 <fix_sh_links+0x40>
add    $0x1,%rcx
mov    %rcx,0x10(%rax)
jmp    1150 <fix_sh_links+0x40>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64, i64 }
@SHT_REL = global i64 0
@SHT_RELA = global i64 0
define i32 @fix_sh_links(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.TYPE_3__*
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 1, i32* %3
br label %55
11: ; preds = %2
store i32 0, i32* %7
br label %12
12: ; preds = %53, %11
%13 = load i8*, i8** %4
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
%16 = call %struct.TYPE_3__* @get_s_header(i8* %13, i32 %14)
store %struct.TYPE_3__* %16, %struct.TYPE_3__** %6
%17 = icmp ne %struct.TYPE_3__* %16, null
br i1 %17, label %18, label %54
18: ; preds = %12
%19 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* %5
%23 = icmp uge i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = add i64 %27, 1
store i64 %28, i64* %26
br label %29
29: ; preds = %24, %18
%30 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %30, i32 0, i32 1
%32 = load i64, i64* %31
%33 = load i64, i64* @SHT_REL
%34 = icmp eq i64 %32, %33
br i1 %34, label %41, label %35
35: ; preds = %29
%36 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = load i64, i64* @SHT_RELA
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %53
41: ; preds = %35, %29
%42 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %42, i32 0, i32 2
%44 = load i64, i64* %43
%45 = load i64, i64* %5
%46 = icmp uge i64 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%49 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %48, i32 0, i32 2
%50 = load i64, i64* %49
%51 = add i64 %50, 1
store i64 %51, i64* %49
br label %52
52: ; preds = %47, %41
br label %53
53: ; preds = %52, %35
br label %12
54: ; preds = %12
store i32 0, i32* %3
br label %55
55: ; preds = %54, %10
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64, i64 }
@SHT_REL = global i64 0
@SHT_RELA = global i64 0
define i32 @fix_sh_links(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %33, label %4
4: ; preds = %2
%5 = tail call %struct.TYPE_3__* @get_s_header(i8* %0, i32 0)
%6 = icmp eq %struct.TYPE_3__* %5, null
br i1 %6, label %33, label %7
7: ; preds = %4, %29
%8 = phi %struct.TYPE_3__* [ %31, %29 ], [ %5, %4 ]
%9 = phi i32 [ %30, %29 ], [ 1, %4 ]
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i64 0, i32 0
%11 = load i64, i64* %10
%12 = icmp ult i64 %11, %1
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = add i64 %11, 1
store i64 %14, i64* %10
br label %15
15: ; preds = %13, %7
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i64 0, i32 1
%17 = load i64, i64* %16
%18 = load i64, i64* @SHT_REL
%19 = icmp eq i64 %17, %18
%20 = load i64, i64* @SHT_RELA
%21 = icmp eq i64 %17, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %29
23: ; preds = %15
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i64 0, i32 2
%25 = load i64, i64* %24
%26 = icmp ult i64 %25, %1
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = add i64 %25, 1
store i64 %28, i64* %24
br label %29
29: ; preds = %15, %23, %27
%30 = add nuw nsw i32 %9, 1
%31 = tail call %struct.TYPE_3__* @get_s_header(i8* %0, i32 %9)
%32 = icmp eq %struct.TYPE_3__* %31, null
br i1 %32, label %33, label %7
33: ; preds = %29, %4, %2
%34 = phi i32 [ 1, %2 ], [ 0, %4 ], [ 0, %29 ]
ret i32 %34
}