; LLVM IR from ryanlayer/giggle :: src (wah)

; 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.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.cache_handler = type { ptr, ptr, ptr }
%struct.bpt_node = type { ptr }
%struct.wah_bpt_leading_data = type { ptr }

@cache = external dso_local global %struct.cache_def, align 8
@wah_leading_cache_handler = external dso_local global %struct.cache_handler, align 8
@ORDER = external dso_local global i32, align 4
@wah_non_leading_cache_handler = external dso_local global %struct.cache_handler, align 8

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 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
declare dso_local ptr @wah_copy(ptr noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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

18:                                               ; preds = %3
  %19 = load ptr, ptr %6, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.bpt_node, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds i32, ptr %21, i64 2
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = icmp eq i32 %23, 1
  br i1 %24, label %25, label %107

25:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %26 = call noalias ptr @malloc(i64 noundef 8) #5
  store ptr %26, ptr %7, align 8, !tbaa !9
  %27 = load ptr, ptr %7, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.wah_bpt_leading_data, ptr %27, i32 0, i32 0
  store ptr null, ptr %28, align 8, !tbaa !13
  %29 = load ptr, ptr %5, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.bpt_node, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  %32 = getelementptr inbounds i32, ptr %31, i64 3
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %51

35:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %36 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 3), align 8, !tbaa !15
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = load ptr, ptr %5, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.bpt_node, ptr %38, i32 0, i32 0
  %40 = load ptr, ptr %39, align 8, !tbaa !11
  %41 = getelementptr inbounds i32, ptr %40, i64 3
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = sub i32 %42, 1
  %44 = call ptr %36(i32 noundef %37, i32 noundef %43, ptr noundef @wah_leading_cache_handler)
  store ptr %44, ptr %8, align 8, !tbaa !9
  %45 = load ptr, ptr %8, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.wah_bpt_leading_data, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = call ptr @wah_copy(ptr noundef %47)
  %49 = load ptr, ptr %7, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.wah_bpt_leading_data, ptr %49, i32 0, i32 0
  store ptr %48, ptr %50, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %51

51:                                               ; preds = %35, %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %81, %51
  %53 = load i32, ptr %9, align 4, !tbaa !5
  %54 = load ptr, ptr %5, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.bpt_node, ptr %54, i32 0, i32 0
  %56 = load ptr, ptr %55, align 8, !tbaa !11
  %57 = getelementptr inbounds i32, ptr %56, i64 5
  %58 = load i32, ptr %57, align 4, !tbaa !5
  %59 = icmp ult i32 %53, %58
  br i1 %59, label %60, label %84

60:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %61 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 3), align 8, !tbaa !15
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = load ptr, ptr %5, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.bpt_node, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  %66 = load i32, ptr @ORDER, align 4, !tbaa !5
  %67 = add i32 6, %66
  %68 = add i32 %67, 2
  %69 = zext i32 %68 to i64
  %70 = getelementptr inbounds i32, ptr %65, i64 %69
  %71 = load i32, ptr %9, align 4, !tbaa !5
  %72 = zext i32 %71 to i64
  %73 = getelementptr inbounds i32, ptr %70, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !5
  %75 = sub i32 %74, 1
  %76 = call ptr %61(i32 noundef %62, i32 noundef %75, ptr noundef @wah_non_leading_cache_handler)
  store ptr %76, ptr %10, align 8, !tbaa !9
  %77 = load i32, ptr %4, align 4, !tbaa !5
  %78 = load ptr, ptr %7, align 8, !tbaa !9
  %79 = getelementptr inbounds %struct.wah_bpt_leading_data, ptr %78, i32 0, i32 0
  %80 = load ptr, ptr %10, align 8, !tbaa !9
  call void @wah_non_leading_union_with_SA_subtract_SE(i32 noundef %77, ptr noundef %79, ptr noundef %80)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %81

81:                                               ; preds = %60
  %82 = load i32, ptr %9, align 4, !tbaa !5
  %83 = add i32 %82, 1
  store i32 %83, ptr %9, align 4, !tbaa !5
  br label %52, !llvm.loop !17

84:                                               ; preds = %52
  %85 = load ptr, ptr %7, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.wah_bpt_leading_data, ptr %85, i32 0, i32 0
  %87 = load ptr, ptr %86, align 8, !tbaa !13
  %88 = icmp ne ptr %87, null
  br i1 %88, label %89, label %104

89:                                               ; preds = %84
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %90 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 1), align 8, !tbaa !19
  %91 = load i32, ptr %4, align 4, !tbaa !5
  %92 = call i32 %90(i32 noundef %91)
  %93 = add i32 %92, 1
  store i32 %93, ptr %11, align 4, !tbaa !5
  %94 = load ptr, ptr getelementptr inbounds (%struct.cache_def, ptr @cache, i32 0, i32 2), align 8, !tbaa !20
  %95 = load i32, ptr %4, align 4, !tbaa !5
  %96 = load i32, ptr %11, align 4, !tbaa !5
  %97 = sub i32 %96, 1
  %98 = load ptr, ptr %7, align 8, !tbaa !9
  call void %94(i32 noundef %95, i32 noundef %97, ptr noundef %98, i64 noundef 8, ptr noundef @wah_leading_cache_handler)
  %99 = load i32, ptr %11, align 4, !tbaa !5
  %100 = load ptr, ptr %6, align 8, !tbaa !9
  %101 = getelementptr inbounds %struct.bpt_node, ptr %100, i32 0, i32 0
  %102 = load ptr, ptr %101, align 8, !tbaa !11
  %103 = getelementptr inbounds i32, ptr %102, i64 3
  store i32 %99, ptr %103, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %106

104:                                              ; preds = %84
  %105 = load ptr, ptr %7, align 8, !tbaa !9
  call void @free(ptr noundef %105) #4
  br label %106

106:                                              ; preds = %104, %89
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %107

107:                                              ; preds = %106, %18, %3
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 = { 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 "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(0) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"bpt_node", !10, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"wah_bpt_leading_data", !10, i64 0}
!15 = !{!16, !10, i64 24}
!16 = !{!"cache_def", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!16, !10, i64 8}
!20 = !{!16, !10, 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"

%struct.cache_def = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.cache_handler = type { ptr, ptr, ptr }

@cache = external dso_local local_unnamed_addr global %struct.cache_def, align 8
@wah_leading_cache_handler = external dso_local global %struct.cache_handler, align 8
@ORDER = external dso_local local_unnamed_addr global i32, align 4
@wah_non_leading_cache_handler = external dso_local global %struct.cache_handler, align 8

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @wah_copy(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @wah_leading_repair(i32 noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2) local_unnamed_addr #1 {
  %4 = load ptr, ptr %1, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i32, ptr %5, align 4, !tbaa !10
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %8, label %52

8:                                                ; preds = %3
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %12 = icmp eq i32 %11, 1
  br i1 %12, label %13, label %52

13:                                               ; preds = %8
  %14 = tail call noalias dereferenceable_or_null(8) ptr @malloc(i64 noundef 8) #3
  store ptr null, ptr %14, align 8, !tbaa !12
  %15 = getelementptr inbounds i8, ptr %4, i64 12
  %16 = load i32, ptr %15, align 4, !tbaa !10
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %23, label %17

17:                                               ; preds = %13
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 24), align 8, !tbaa !14
  %19 = add i32 %16, -1
  %20 = tail call ptr %18(i32 noundef %0, i32 noundef %19, ptr noundef nonnull @wah_leading_cache_handler) #4
  %21 = load ptr, ptr %20, align 8, !tbaa !12
  %22 = tail call ptr @wah_copy(ptr noundef %21)
  store ptr %22, ptr %14, align 8, !tbaa !12
  %.pre = load ptr, ptr %1, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %17, %13
  %24 = phi ptr [ %22, %17 ], [ null, %13 ]
  %25 = phi ptr [ %.pre, %17 ], [ %4, %13 ]
  %26 = getelementptr inbounds i8, ptr %25, i64 20
  %27 = load i32, ptr %26, align 4, !tbaa !10
  %.not27 = icmp eq i32 %27, 0
  br i1 %.not27, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %23, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %23 ]
  %28 = phi ptr [ %38, %.lr.ph ], [ %25, %23 ]
  %29 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 24), align 8, !tbaa !14
  %30 = load i32, ptr @ORDER, align 4, !tbaa !10
  %31 = add i32 %30, 8
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds i32, ptr %28, i64 %32
  %34 = getelementptr inbounds i32, ptr %33, i64 %indvars.iv
  %35 = load i32, ptr %34, align 4, !tbaa !10
  %36 = add i32 %35, -1
  %37 = tail call ptr %29(i32 noundef %0, i32 noundef %36, ptr noundef nonnull @wah_non_leading_cache_handler) #4
  tail call void @wah_non_leading_union_with_SA_subtract_SE(i32 noundef %0, ptr noundef nonnull %14, ptr noundef %37)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %38 = load ptr, ptr %1, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 20
  %40 = load i32, ptr %39, align 4, !tbaa !10
  %41 = zext i32 %40 to i64
  %42 = icmp ult i64 %indvars.iv.next, %41
  br i1 %42, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre29 = load ptr, ptr %14, align 8, !tbaa !12
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %23
  %43 = phi ptr [ %.pre29, %._crit_edge.loopexit ], [ %24, %23 ]
  %.not25 = icmp eq ptr %43, null
  br i1 %.not25, label %51, label %44

44:                                               ; preds = %._crit_edge
  %45 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 8), align 8, !tbaa !18
  %46 = tail call i32 %45(i32 noundef %0) #4
  %47 = add i32 %46, 1
  %48 = load ptr, ptr getelementptr inbounds (i8, ptr @cache, i64 16), align 8, !tbaa !19
  tail call void %48(i32 noundef %0, i32 noundef %46, ptr noundef nonnull %14, i64 noundef 8, ptr noundef nonnull @wah_leading_cache_handler) #4
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds i8, ptr %49, i64 12
  store i32 %47, ptr %50, align 4, !tbaa !10
  br label %52

51:                                               ; preds = %._crit_edge
  tail call void @free(ptr noundef nonnull %14) #4
  br label %52

52:                                               ; preds = %44, %51, %8, %3
  ret void
}

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

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #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("free") 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(0) }
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, !7, i64 0}
!6 = !{!"bpt_node", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"wah_bpt_leading_data", !7, i64 0}
!14 = !{!15, !7, i64 24}
!15 = !{!"cache_def", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!15, !7, i64 8}
!19 = !{!15, !7, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass