; LLVM IR from attractivechaos/klib :: test (kbtree_test)

; 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.kbtree_int_t = type { ptr, i32, i32, i32, i32, i32, i32, i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local ptr @kb_init_int(i32 noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %6 = call noalias ptr @calloc(i64 noundef 1, i64 noundef 40) #5
  store ptr %6, ptr %4, align 8, !tbaa !9
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = sub nsw i32 %7, 4
  %9 = sext i32 %8 to i64
  %10 = sub i64 %9, 8
  %11 = udiv i64 %10, 12
  %12 = add i64 %11, 1
  %13 = lshr i64 %12, 1
  %14 = trunc i64 %13 to i32
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.kbtree_int_t, ptr %15, i32 0, i32 6
  store i32 %14, ptr %16, align 4, !tbaa !11
  %17 = load ptr, ptr %4, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.kbtree_int_t, ptr %17, i32 0, i32 6
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = icmp slt i32 %19, 2
  br i1 %20, label %21, label %23

21:                                               ; preds = %1
  %22 = load ptr, ptr %4, align 8, !tbaa !9
  call void @free(ptr noundef %22) #4
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %72

23:                                               ; preds = %1
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.kbtree_int_t, ptr %24, i32 0, i32 6
  %26 = load i32, ptr %25, align 4, !tbaa !11
  %27 = mul nsw i32 2, %26
  %28 = sub nsw i32 %27, 1
  %29 = load ptr, ptr %4, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.kbtree_int_t, ptr %29, i32 0, i32 5
  store i32 %28, ptr %30, align 8, !tbaa !13
  %31 = load ptr, ptr %4, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.kbtree_int_t, ptr %31, i32 0, i32 5
  %33 = load i32, ptr %32, align 8, !tbaa !13
  %34 = sext i32 %33 to i64
  %35 = mul i64 %34, 4
  %36 = add i64 4, %35
  %37 = trunc i64 %36 to i32
  %38 = load ptr, ptr %4, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.kbtree_int_t, ptr %38, i32 0, i32 2
  store i32 %37, ptr %39, align 4, !tbaa !14
  %40 = load ptr, ptr %4, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.kbtree_int_t, ptr %40, i32 0, i32 5
  %42 = load i32, ptr %41, align 8, !tbaa !13
  %43 = sext i32 %42 to i64
  %44 = mul i64 %43, 12
  %45 = add i64 12, %44
  %46 = add i64 %45, 3
  %47 = lshr i64 %46, 2
  %48 = shl i64 %47, 2
  %49 = trunc i64 %48 to i32
  %50 = load ptr, ptr %4, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.kbtree_int_t, ptr %50, i32 0, i32 3
  store i32 %49, ptr %51, align 8, !tbaa !15
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.kbtree_int_t, ptr %52, i32 0, i32 2
  %54 = load i32, ptr %53, align 4, !tbaa !14
  %55 = add nsw i32 %54, 3
  %56 = ashr i32 %55, 2
  %57 = shl i32 %56, 2
  %58 = load ptr, ptr %4, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.kbtree_int_t, ptr %58, i32 0, i32 4
  store i32 %57, ptr %59, align 4, !tbaa !16
  %60 = load ptr, ptr %4, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.kbtree_int_t, ptr %60, i32 0, i32 3
  %62 = load i32, ptr %61, align 8, !tbaa !15
  %63 = sext i32 %62 to i64
  %64 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %63) #5
  %65 = load ptr, ptr %4, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.kbtree_int_t, ptr %65, i32 0, i32 0
  store ptr %64, ptr %66, align 8, !tbaa !17
  %67 = load ptr, ptr %4, align 8, !tbaa !9
  %68 = getelementptr inbounds %struct.kbtree_int_t, ptr %67, i32 0, i32 8
  %69 = load i32, ptr %68, align 4, !tbaa !18
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %68, align 4, !tbaa !18
  %71 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %71, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %72

72:                                               ; preds = %23, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %73 = load ptr, ptr %2, align 8
  ret ptr %73
}

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

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(0,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 #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,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 = !{!"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, !6, i64 28}
!12 = !{!"", !10, i64 0, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36}
!13 = !{!12, !6, i64 24}
!14 = !{!12, !6, i64 12}
!15 = !{!12, !6, i64 16}
!16 = !{!12, !6, i64 20}
!17 = !{!12, !10, i64 0}
!18 = !{!12, !6, i64 36}


; 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"

; Function Attrs: mustprogress nounwind willreturn memory(readwrite, argmem: none) uwtable
define dso_local noalias noundef ptr @kb_init_int(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(40) ptr @calloc(i64 noundef 1, i64 noundef 40) #3
  %3 = sext i32 %0 to i64
  %4 = add nsw i64 %3, -12
  %5 = udiv i64 %4, 12
  %6 = add nuw nsw i64 %5, 1
  %7 = lshr i64 %6, 1
  %8 = trunc i64 %7 to i32
  %9 = getelementptr inbounds i8, ptr %2, i64 28
  store i32 %8, ptr %9, align 4, !tbaa !5
  %10 = icmp slt i32 %8, 2
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  tail call void @free(ptr noundef nonnull %2) #4
  br label %29

12:                                               ; preds = %1
  %13 = shl nuw nsw i32 %8, 1
  %14 = add nsw i32 %13, -1
  %15 = getelementptr inbounds i8, ptr %2, i64 24
  store i32 %14, ptr %15, align 8, !tbaa !11
  %16 = shl i32 %14, 2
  %17 = add i32 %16, 4
  %18 = getelementptr inbounds i8, ptr %2, i64 12
  store i32 %17, ptr %18, align 4, !tbaa !12
  %19 = mul i32 %14, 12
  %20 = add i32 %19, 15
  %21 = and i32 %20, -8
  %22 = getelementptr inbounds i8, ptr %2, i64 16
  store i32 %21, ptr %22, align 8, !tbaa !13
  %23 = add i32 %16, 7
  %24 = and i32 %23, -8
  %25 = getelementptr inbounds i8, ptr %2, i64 20
  store i32 %24, ptr %25, align 4, !tbaa !14
  %26 = sext i32 %21 to i64
  %27 = tail call noalias ptr @calloc(i64 noundef 1, i64 noundef %26) #3
  store ptr %27, ptr %2, align 8, !tbaa !15
  %28 = getelementptr inbounds i8, ptr %2, i64 36
  store i32 1, ptr %28, align 4, !tbaa !16
  br label %29

29:                                               ; preds = %12, %11
  %.0 = phi ptr [ null, %11 ], [ %2, %12 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 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

attributes #0 = { mustprogress nounwind willreturn memory(readwrite, argmem: none) 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 #1 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #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,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 28}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = !{!6, !10, i64 12}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !10, i64 20}
!15 = !{!6, !7, i64 0}
!16 = !{!6, !10, i64 36}


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