#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 */

/* Type definitions */

/* Variables and functions */
void *realloc(void **, size_t);
int tglist_grow_if_needed(void *lst, size_t itemsize) {
  struct {
    size_t count;
    size_t capa;
    void **items;
    union {
      void **vt;
      size_t s;
    } tmp;
  } *l = lst;
  void *temp;
  if (l->count == l->capa) {
    size_t newsz = l->capa == 0 ? 4 : l->capa * 2;
    temp = realloc(l->items, newsz * itemsize);
    if (!temp)
      return 0;
    l->capa = newsz;
    l->items = temp;
  }
  return 1;
}


x86 assembly (clang, -O0):
<tglist_grow_if_needed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
cmp    0x8(%rcx),%rax
jne    11bb <tglist_grow_if_needed+0xab>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    115a <tglist_grow_if_needed+0x4a>
mov    $0x4,%eax
mov    %rax,-0x38(%rbp)
jmp    116a <tglist_grow_if_needed+0x5a>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
shl    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x30(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a3 <tglist_grow_if_needed+0x93>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <tglist_grow_if_needed+0xb2>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tglist_grow_if_needed>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%rax
mov    $0x1,%r14d
cmp    0x8(%rdi),%rax
jne    1155 <tglist_grow_if_needed+0x45>
mov    %rdi,%rbx
lea    (%rax,%rax,1),%rcx
test   %rax,%rax
mov    $0x4,%ebp
cmovne %rcx,%rbp
mov    0x10(%rdi),%rdi
imul   %rbp,%rsi
call   1030 <realloc@plt>
test   %rax,%rax
je     1152 <tglist_grow_if_needed+0x42>
mov    %rbp,0x8(%rbx)
mov    %rax,0x10(%rbx)
jmp    1155 <tglist_grow_if_needed+0x45>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.anon = type { i64, i64, i8**, %union.anon }
%union.anon = type { i8** }
define i32 @tglist_grow_if_needed(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.anon*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to %struct.anon*
store %struct.anon* %10, %struct.anon** %6
%11 = load %struct.anon*, %struct.anon** %6
%12 = getelementptr inbounds %struct.anon, %struct.anon* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = load %struct.anon*, %struct.anon** %6
%15 = getelementptr inbounds %struct.anon, %struct.anon* %14, i32 0, i32 1
%16 = load i64, i64* %15
%17 = icmp eq i64 %13, %16
br i1 %17, label %18, label %49
18: ; preds = %2
%19 = load %struct.anon*, %struct.anon** %6
%20 = getelementptr inbounds %struct.anon, %struct.anon* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %29
24: ; preds = %18
%25 = load %struct.anon*, %struct.anon** %6
%26 = getelementptr inbounds %struct.anon, %struct.anon* %25, i32 0, i32 1
%27 = load i64, i64* %26
%28 = mul i64 %27, 2
br label %29
29: ; preds = %24, %23
%30 = phi i64 [ 4, %23 ], [ %28, %24 ]
store i64 %30, i64* %8
%31 = load %struct.anon*, %struct.anon** %6
%32 = getelementptr inbounds %struct.anon, %struct.anon* %31, i32 0, i32 2
%33 = load i8**, i8*** %32
%34 = load i64, i64* %8
%35 = load i64, i64* %5
%36 = mul i64 %34, %35
%37 = call i8* @realloc(i8** %33, i64 %36)
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %29
store i32 0, i32* %3
br label %50
41: ; preds = %29
%42 = load i64, i64* %8
%43 = load %struct.anon*, %struct.anon** %6
%44 = getelementptr inbounds %struct.anon, %struct.anon* %43, i32 0, i32 1
store i64 %42, i64* %44
%45 = load i8*, i8** %7
%46 = bitcast i8* %45 to i8**
%47 = load %struct.anon*, %struct.anon** %6
%48 = getelementptr inbounds %struct.anon, %struct.anon* %47, i32 0, i32 2
store i8** %46, i8*** %48
br label %49
49: ; preds = %41, %2
store i32 1, i32* %3
br label %50
50: ; preds = %49, %40
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @tglist_grow_if_needed(i8* nocapture %0, i64 %1) {
%3 = bitcast i8* %0 to i64*
%4 = load i64, i64* %3
%5 = getelementptr inbounds i8, i8* %0, i64 8
%6 = bitcast i8* %5 to i64*
%7 = load i64, i64* %6
%8 = icmp eq i64 %4, %7
br i1 %8, label %9, label %21
9: ; preds = %2
%10 = icmp eq i64 %4, 0
%11 = shl i64 %4, 1
%12 = select i1 %10, i64 4, i64 %11
%13 = getelementptr inbounds i8, i8* %0, i64 16
%14 = bitcast i8* %13 to i8***
%15 = load i8**, i8*** %14
%16 = mul i64 %12, %1
%17 = tail call i8* @realloc(i8** %15, i64 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %9
store i64 %12, i64* %6
%20 = bitcast i8* %13 to i8**
store i8* %17, i8** %20
br label %21
21: ; preds = %2, %19, %9
%22 = phi i32 [ 0, %9 ], [ 1, %19 ], [ 1, %2 ]
ret i32 %22
}