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

/* Type definitions */
struct TYPE_5__ {
  int sh_flags;
  int sh_addralign;
};
typedef TYPE_1__ Elf64_Shdr;

/* Variables and functions */
int SHF_ALLOC;
TYPE_1__ *get_s_header(char *, int);
int shdr_index(char *, TYPE_1__ *);
int get_align_bias(char *elf, Elf64_Shdr *shdr, int n) {
  if (!elf || !shdr)
    return 0;
  int bias;
  bias = n;
  if (shdr->sh_flags & SHF_ALLOC && shdr->sh_addralign) {
    bias += shdr->sh_addralign;
    bias -= (bias % shdr->sh_addralign);
  }
  Elf64_Shdr *shdr_next;
  int i;
  i = shdr_index(elf, shdr);
  shdr_next = get_s_header(elf, i + 1);
  if (!shdr_next)
    return bias;
  return get_align_bias(elf, shdr_next, bias);
}


x86 assembly (clang, -O0):
<get_align_bias>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1159 <get_align_bias+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1165 <get_align_bias+0x35>
movl   $0x0,-0x4(%rbp)
jmp    11fd <get_align_bias+0xcd>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e68(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11b1 <get_align_bias+0x81>
mov    -0x18(%rbp),%rax
cmpl   $0x0,0x4(%rax)
je     11b1 <get_align_bias+0x81>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x18(%rbp),%rcx
cltd
idivl  0x4(%rcx)
mov    -0x20(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <shdr_index@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
call   1050 <get_s_header@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ea <get_align_bias+0xba>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fd <get_align_bias+0xcd>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <get_align_bias@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_align_bias>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1179 <get_align_bias+0x59>
mov    %edx,%ebp
mov    %rdi,%rbx
mov    0x2eab(%rip),%r14
jmp    115b <get_align_bias+0x3b>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <shdr_index@plt>
lea    0x1(%rax),%esi
mov    %rbx,%rdi
call   1040 <get_s_header@plt>
mov    %rax,%rsi
test   %rax,%rax
je     117b <get_align_bias+0x5b>
test   %rsi,%rsi
je     1179 <get_align_bias+0x59>
mov    (%r14),%eax
test   %eax,(%rsi)
je     1140 <get_align_bias+0x20>
mov    0x4(%rsi),%ecx
test   %ecx,%ecx
je     1140 <get_align_bias+0x20>
add    %ecx,%ebp
mov    %ebp,%eax
cltd
idiv   %ecx
sub    %edx,%ebp
jmp    1140 <get_align_bias+0x20>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, i32 }
@SHF_ALLOC = global i32 0
define i32 @get_align_bias(i8* %0, %struct.TYPE_5__* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %29, label %5
5: ; preds = %3, %23
%6 = phi %struct.TYPE_5__* [ %27, %23 ], [ %1, %3 ]
%7 = phi i32 [ %24, %23 ], [ %2, %3 ]
%8 = icmp eq %struct.TYPE_5__* %6, null
br i1 %8, label %29, label %9
9: ; preds = %5
%10 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @SHF_ALLOC
%13 = and i32 %12, %11
%14 = icmp eq i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %9
%16 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %6, i64 0, i32 1
%17 = load i32, i32* %16
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = add nsw i32 %17, %7
%21 = srem i32 %20, %17
%22 = sub nsw i32 %20, %21
br label %23
23: ; preds = %19, %15, %9
%24 = phi i32 [ %22, %19 ], [ %7, %15 ], [ %7, %9 ]
%25 = tail call i32 @shdr_index(i8* %0, %struct.TYPE_5__* %6)
%26 = add nsw i32 %25, 1
%27 = tail call %struct.TYPE_5__* @get_s_header(i8* %0, i32 %26)
%28 = icmp eq %struct.TYPE_5__* %27, null
br i1 %28, label %29, label %5
29: ; preds = %5, %23, %3
%30 = phi i32 [ 0, %3 ], [ 0, %5 ], [ %24, %23 ]
ret i32 %30
}