#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_1__;

/* Type definitions */
struct TYPE_8__ {
  scalar_t__ sh_type;
  size_t sh_size;
  unsigned long long sh_offset;
};
typedef TYPE_1__ Elf64_Shdr;

/* Variables and functions */
scalar_t__ SHT_DYNAMIC;
int /*<<< orphan*/ expand_segments(char *, void *, int);
TYPE_1__ *get_s_header(char *, int);
int shdr_index(char *, TYPE_1__ *);
int shift_sh_offsets(char *, TYPE_1__ *, TYPE_1__ *, size_t);
int expand_section(char *elf, Elf64_Shdr *shdr, Elf64_Shdr *dyn_shdr,
                   size_t n) {
  if (!elf || !shdr)
    return 0;
  if (dyn_shdr) {
    if (dyn_shdr->sh_type != SHT_DYNAMIC)
      return 0;
  }
  int shdr_ind;
  shdr_ind = shdr_index(elf, shdr);
  if (shdr_ind == -1)
    return 0;
  int ret;
  Elf64_Shdr *next;
  next = get_s_header(elf, shdr_ind + 1);
  if (next)
    ret = shift_sh_offsets(elf, next, dyn_shdr, n);
  else
    ret = n;
  shdr->sh_size += n;
  unsigned long long shdata_addr;
  shdata_addr = (unsigned long long)elf + shdr->sh_offset;
  expand_segments(elf, (void *)shdata_addr, ret);
  return ret;
}


x86 assembly (clang, -O0):
<expand_section>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116e <expand_section+0x2e>
cmpq   $0x0,-0x18(%rbp)
jne    117a <expand_section+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    124b <expand_section+0x10b>
cmpq   $0x0,-0x20(%rbp)
je     11ad <expand_section+0x6d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <expand_section+0x68>
movl   $0x0,-0x4(%rbp)
jmp    124b <expand_section+0x10b>
jmp    11ad <expand_section+0x6d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <shdr_index@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11d3 <expand_section+0x93>
movl   $0x0,-0x4(%rbp)
jmp    124b <expand_section+0x10b>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
call   1060 <get_s_header@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     120e <expand_section+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <shift_sh_offsets@plt>
mov    %eax,-0x30(%rbp)
jmp    1215 <expand_section+0xd5>
mov    -0x28(%rbp),%rax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    0x8(%rax),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    0x10(%rcx),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <expand_segments@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_section>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebp,%ebp
test   %rdi,%rdi
je     11c4 <expand_section+0x84>
mov    %rsi,%r15
test   %rsi,%rsi
je     11c4 <expand_section+0x84>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rdi,%rbx
test   %rdx,%rdx
je     1175 <expand_section+0x35>
mov    (%r12),%rax
mov    0x2e80(%rip),%rcx
cmp    (%rcx),%rax
jne    11c4 <expand_section+0x84>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <shdr_index@plt>
cmp    $0xffffffff,%eax
je     11c4 <expand_section+0x84>
add    $0x1,%eax
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <get_s_header@plt>
test   %rax,%rax
je     11ac <expand_section+0x6c>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1040 <shift_sh_offsets@plt>
mov    %eax,%ebp
jmp    11af <expand_section+0x6f>
mov    %r14d,%ebp
add    %r14,0x8(%r15)
mov    0x10(%r15),%rsi
add    %rbx,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1050 <expand_segments@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_8__ = type { i64, i64, i64 }
@SHT_DYNAMIC = global i64 0
define i32 @expand_section(i8* %0, %struct.TYPE_8__* %1, %struct.TYPE_8__* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.TYPE_8__*
%8 = alloca %struct.TYPE_8__*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.TYPE_8__*
%13 = alloca i64
store i8* %0, i8** %6
store %struct.TYPE_8__* %1, %struct.TYPE_8__** %7
store %struct.TYPE_8__* %2, %struct.TYPE_8__** %8
store i64 %3, i64* %9
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load %struct.TYPE_8__*, %struct.TYPE_8__** %7
%18 = icmp ne %struct.TYPE_8__* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16, %4
store i32 0, i32* %5
br label %72
20: ; preds = %16
%21 = load %struct.TYPE_8__*, %struct.TYPE_8__** %8
%22 = icmp ne %struct.TYPE_8__* %21, null
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load %struct.TYPE_8__*, %struct.TYPE_8__** %8
%25 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @SHT_DYNAMIC
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 0, i32* %5
br label %72
30: ; preds = %23
br label %31
31: ; preds = %30, %20
%32 = load i8*, i8** %6
%33 = load %struct.TYPE_8__*, %struct.TYPE_8__** %7
%34 = call i32 @shdr_index(i8* %32, %struct.TYPE_8__* %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 0, i32* %5
br label %72
38: ; preds = %31
%39 = load i8*, i8** %6
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
%42 = call %struct.TYPE_8__* @get_s_header(i8* %39, i32 %41)
store %struct.TYPE_8__* %42, %struct.TYPE_8__** %12
%43 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%44 = icmp ne %struct.TYPE_8__* %43, null
br i1 %44, label %45, label %51
45: ; preds = %38
%46 = load i8*, i8** %6
%47 = load %struct.TYPE_8__*, %struct.TYPE_8__** %12
%48 = load %struct.TYPE_8__*, %struct.TYPE_8__** %8
%49 = load i64, i64* %9
%50 = call i32 @shift_sh_offsets(i8* %46, %struct.TYPE_8__* %47, %struct.TYPE_8__* %48, i64 %49)
store i32 %50, i32* %11
br label %54
51: ; preds = %38
%52 = load i64, i64* %9
%53 = trunc i64 %52 to i32
store i32 %53, i32* %11
br label %54
54: ; preds = %51, %45
%55 = load i64, i64* %9
%56 = load %struct.TYPE_8__*, %struct.TYPE_8__** %7
%57 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %56, i32 0, i32 1
%58 = load i64, i64* %57
%59 = add i64 %58, %55
store i64 %59, i64* %57
%60 = load i8*, i8** %6
%61 = ptrtoint i8* %60 to i64
%62 = load %struct.TYPE_8__*, %struct.TYPE_8__** %7
%63 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %62, i32 0, i32 2
%64 = load i64, i64* %63
%65 = add i64 %61, %64
store i64 %65, i64* %13
%66 = load i8*, i8** %6
%67 = load i64, i64* %13
%68 = inttoptr i64 %67 to i8*
%69 = load i32, i32* %11
%70 = call i32 @expand_segments(i8* %66, i8* %68, i32 %69)
%71 = load i32, i32* %11
store i32 %71, i32* %5
br label %72
72: ; preds = %54, %37, %29, %19
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
%struct.TYPE_8__ = type { i64, i64, i64 }
@SHT_DYNAMIC = global i64 0
define i32 @expand_section(i8* %0, %struct.TYPE_8__* %1, %struct.TYPE_8__* %2, i64 %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne %struct.TYPE_8__* %1, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %37
8: ; preds = %4
%9 = icmp eq %struct.TYPE_8__* %2, null
br i1 %9, label %15, label %10
10: ; preds = %8
%11 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @SHT_DYNAMIC
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %37
15: ; preds = %10, %8
%16 = tail call i32 @shdr_index(i8* %0, %struct.TYPE_8__* %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %37, label %18
18: ; preds = %15
%19 = add nuw nsw i32 %16, 1
%20 = tail call %struct.TYPE_8__* @get_s_header(i8* %0, i32 %19)
%21 = icmp eq %struct.TYPE_8__* %20, null
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 @shift_sh_offsets(i8* %0, %struct.TYPE_8__* %20, %struct.TYPE_8__* %2, i64 %3)
br label %26
24: ; preds = %18
%25 = trunc i64 %3 to i32
br label %26
26: ; preds = %24, %22
%27 = phi i32 [ %23, %22 ], [ %25, %24 ]
%28 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %1, i64 0, i32 1
%29 = load i64, i64* %28
%30 = add i64 %29, %3
store i64 %30, i64* %28
%31 = ptrtoint i8* %0 to i64
%32 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %1, i64 0, i32 2
%33 = load i64, i64* %32
%34 = add i64 %33, %31
%35 = inttoptr i64 %34 to i8*
%36 = tail call i32 @expand_segments(i8* %0, i8* %35, i32 %27)
br label %37
37: ; preds = %26, %15, %10, %4
%38 = phi i32 [ 0, %4 ], [ 0, %10 ], [ %27, %26 ], [ 0, %15 ]
ret i32 %38
}