; LLVM IR from libtcod/libtcod :: src/libtcod (heapq)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.TCOD_Heap = type { ptr, i32, i32, i64, i64, i64, i32 }

@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [39 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare i32 @TCOD_set_errorf(ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @TCOD_minheap_push(ptr noalias noundef %0, i32 noundef %1, ptr noalias noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.TCOD_Heap, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.TCOD_Heap, ptr %14, i32 0, i32 2
  %16 = load i32, ptr %15, align 4, !tbaa !14
  %17 = icmp eq i32 %13, %16
  br i1 %17, label %18, label %55

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.TCOD_Heap, ptr %19, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !14
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %28

23:                                               ; preds = %18
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.TCOD_Heap, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 4, !tbaa !14
  %27 = mul nsw i32 %26, 2
  br label %29

28:                                               ; preds = %18
  br label %29

29:                                               ; preds = %28, %23
  %30 = phi i32 [ %27, %23 ], [ 256, %28 ]
  store i32 %30, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.TCOD_Heap, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !15
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.TCOD_Heap, ptr %34, i32 0, i32 3
  %36 = load i64, ptr %35, align 8, !tbaa !16
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = mul i64 %36, %38
  %40 = call ptr @realloc(ptr noundef %33, i64 noundef %39) #5
  store ptr %40, ptr %9, align 8, !tbaa !5
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = icmp ne ptr %41, null
  br i1 %42, label %45, label %43

43:                                               ; preds = %29
  %44 = call i32 (ptr, ...) @TCOD_set_errorf(ptr noundef @.str.2, ptr noundef @.str.1, i32 noundef 186, ptr noundef @.str.3)
  store i32 -3, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %52

45:                                               ; preds = %29
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.TCOD_Heap, ptr %47, i32 0, i32 2
  store i32 %46, ptr %48, align 4, !tbaa !14
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.TCOD_Heap, ptr %50, i32 0, i32 0
  store ptr %49, ptr %51, align 8, !tbaa !15
  store i32 0, ptr %10, align 4
  br label %52

52:                                               ; preds = %45, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %53 = load i32, ptr %10, align 4
  switch i32 %53, label %74 [
    i32 0, label %54
    i32 1, label %72
  ]

54:                                               ; preds = %52
  br label %55

55:                                               ; preds = %54, %3
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.TCOD_Heap, ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 8, !tbaa !11
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %57, align 8, !tbaa !11
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.TCOD_Heap, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %62, align 8, !tbaa !11
  %64 = sub nsw i32 %63, 1
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  call void @TCOD_heap_set_(ptr noundef %60, i32 noundef %64, i32 noundef %65, ptr noundef %66)
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.TCOD_Heap, ptr %68, i32 0, i32 1
  %70 = load i32, ptr %69, align 8, !tbaa !11
  %71 = sub nsw i32 %70, 1
  call void @TCOD_TCOD_minheap_heapify_up_(ptr noundef %67, i32 noundef %71)
  store i32 0, ptr %4, align 4
  br label %72

72:                                               ; preds = %55, %52
  %73 = load i32, ptr %4, align 4
  ret i32 %73

74:                                               ; preds = %52
  unreachable
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @TCOD_heap_set_(ptr noalias noundef, i32 noundef, i32 noundef, ptr noalias noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @TCOD_TCOD_minheap_heapify_up_(ptr noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind allocsize(1) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind }
attributes #5 = { nounwind allocsize(1) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"TCOD_Heap", !6, i64 0, !10, i64 8, !10, i64 12, !13, i64 16, !13, i64 24, !13, i64 32, !10, i64 40}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !10, i64 12}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !13, i64 16}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@.str.1 = external hidden unnamed_addr constant [83 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [39 x i8], align 1

declare i32 @TCOD_set_errorf(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -3, 1) i32 @TCOD_minheap_push(ptr noalias noundef %0, i32 noundef %1, ptr noalias noundef %2) local_unnamed_addr #1 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !12
  %8 = icmp eq i32 %5, %7
  br i1 %8, label %9, label %19

9:                                                ; preds = %3
  %.not = icmp eq i32 %5, 0
  %10 = shl nsw i32 %5, 1
  %spec.select = select i1 %.not, i32 256, i32 %10
  %11 = load ptr, ptr %0, align 8, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load i64, ptr %12, align 8, !tbaa !14
  %14 = sext i32 %spec.select to i64
  %15 = mul i64 %13, %14
  %16 = tail call ptr @realloc(ptr noundef %11, i64 noundef %15) #3
  %.not23 = icmp eq ptr %16, null
  br i1 %.not23, label %17, label %.thread

.thread:                                          ; preds = %9
  store i32 %spec.select, ptr %6, align 4, !tbaa !12
  store ptr %16, ptr %0, align 8, !tbaa !13
  br label %19

17:                                               ; preds = %9
  %18 = tail call i32 (ptr, ...) @TCOD_set_errorf(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, i32 noundef 186, ptr noundef nonnull @.str.3) #4
  br label %23

19:                                               ; preds = %.thread, %3
  %20 = add nsw i32 %5, 1
  store i32 %20, ptr %4, align 8, !tbaa !5
  tail call void @TCOD_heap_set_(ptr noundef nonnull %0, i32 noundef %5, i32 noundef %1, ptr noundef %2)
  %21 = load i32, ptr %4, align 8, !tbaa !5
  %22 = add nsw i32 %21, -1
  tail call void @TCOD_TCOD_minheap_heapify_up_(ptr noundef nonnull %0, i32 noundef %22)
  br label %23

23:                                               ; preds = %17, %19
  %.1 = phi i32 [ -3, %17 ], [ 0, %19 ]
  ret i32 %.1
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @TCOD_heap_set_(ptr noalias noundef, i32 noundef, i32 noundef, ptr noalias noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @TCOD_TCOD_minheap_heapify_up_(ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nounwind allocsize(1) }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !10, i64 8}
!6 = !{!"TCOD_Heap", !7, i64 0, !10, i64 8, !10, i64 12, !11, i64 16, !11, i64 24, !11, i64 32, !10, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !10, i64 12}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !11, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass