; LLVM IR from yudi-matsuzake/libgenerics :: src (trie)

; 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.trie_t = type { i64, i64, %struct.tnode_t }
%struct.tnode_t = type { ptr, [256 x ptr] }

; Function Attrs: nounwind uwtable
define dso_local ptr @trie_get_node_or_allocate(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %18

15:                                               ; preds = %3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %19, label %18

18:                                               ; preds = %15, %3
  store ptr null, ptr %4, align 8
  br label %83

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.trie_t, ptr %20, i32 0, i32 2
  store ptr %21, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %22, ptr %10, align 8, !tbaa !5
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %78, %19
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = icmp ult i64 %24, %25
  br i1 %26, label %27, label %81

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = load i64, ptr %9, align 8, !tbaa !9
  %30 = getelementptr inbounds i8, ptr %28, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = sext i8 %31 to i32
  store i32 %32, ptr %11, align 4, !tbaa !12
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.tnode_t, ptr %33, i32 0, i32 1
  %35 = load i32, ptr %11, align 4, !tbaa !12
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [256 x ptr], ptr %34, i64 0, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = icmp eq ptr %38, null
  br i1 %39, label %40, label %71

40:                                               ; preds = %27
  %41 = call noalias ptr @malloc(i64 noundef 2056) #4
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.tnode_t, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %11, align 4, !tbaa !12
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [256 x ptr], ptr %43, i64 0, i64 %45
  store ptr %41, ptr %46, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.tnode_t, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %11, align 4, !tbaa !12
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [256 x ptr], ptr %48, i64 0, i64 %50
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.tnode_t, ptr %52, i32 0, i32 0
  store ptr null, ptr %53, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !12
  br label %54

54:                                               ; preds = %67, %40
  %55 = load i32, ptr %12, align 4, !tbaa !12
  %56 = icmp slt i32 %55, 256
  br i1 %56, label %57, label %70

57:                                               ; preds = %54
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.tnode_t, ptr %58, i32 0, i32 1
  %60 = load i32, ptr %11, align 4, !tbaa !12
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [256 x ptr], ptr %59, i64 0, i64 %61
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.tnode_t, ptr %63, i32 0, i32 1
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = getelementptr inbounds [256 x ptr], ptr %64, i64 0, i64 %65
  store ptr null, ptr %66, align 8, !tbaa !5
  br label %67

67:                                               ; preds = %57
  %68 = load i32, ptr %12, align 4, !tbaa !12
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %12, align 4, !tbaa !12
  br label %54, !llvm.loop !16

70:                                               ; preds = %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %71

71:                                               ; preds = %70, %27
  %72 = load ptr, ptr %8, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.tnode_t, ptr %72, i32 0, i32 1
  %74 = load i32, ptr %11, align 4, !tbaa !12
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [256 x ptr], ptr %73, i64 0, i64 %75
  %77 = load ptr, ptr %76, align 8, !tbaa !5
  store ptr %77, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %78

78:                                               ; preds = %71
  %79 = load i64, ptr %9, align 8, !tbaa !9
  %80 = add i64 %79, 1
  store i64 %80, ptr %9, align 8, !tbaa !9
  br label %23, !llvm.loop !18

81:                                               ; preds = %23
  %82 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %82, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %83

83:                                               ; preds = %81, %18
  %84 = load ptr, ptr %4, align 8
  ret ptr %84
}

; 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)
declare noalias ptr @malloc(i64 noundef) #2

; 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) "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 }
attributes #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"tnode_t", !6, i64 0, !7, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; 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: nofree nounwind uwtable
define dso_local ptr @trie_get_node_or_allocate(ptr noundef %0, ptr noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ne ptr %0, null
  %5 = icmp ne ptr %1, null
  %or.cond = and i1 %4, %5
  br i1 %or.cond, label %6, label %.loopexit29

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %.loopexit29, label %.lr.ph

.lr.ph:                                           ; preds = %6, %.loopexit
  %.02532 = phi ptr [ %31, %.loopexit ], [ %7, %6 ]
  %.02631 = phi i64 [ %32, %.loopexit ], [ 0, %6 ]
  %8 = getelementptr inbounds i8, ptr %1, i64 %.02631
  %9 = load i8, ptr %8, align 1, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %.02532, i64 8
  %11 = sext i8 %9 to i64
  %12 = getelementptr inbounds [256 x ptr], ptr %10, i64 0, i64 %11
  %13 = load ptr, ptr %12, align 8, !tbaa !8
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %.loopexit

15:                                               ; preds = %.lr.ph
  %16 = tail call noalias dereferenceable_or_null(2056) ptr @malloc(i64 noundef 2056) #2
  store ptr %16, ptr %12, align 8, !tbaa !8
  store ptr null, ptr %16, align 8, !tbaa !10
  br label %17

17:                                               ; preds = %17, %15
  %.030 = phi i32 [ 0, %15 ], [ %30, %17 ]
  %18 = load ptr, ptr %12, align 8, !tbaa !8
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = getelementptr inbounds [256 x ptr], ptr %19, i64 0, i64 %.02631
  store ptr null, ptr %20, align 8, !tbaa !8
  %21 = load ptr, ptr %12, align 8, !tbaa !8
  %22 = getelementptr inbounds i8, ptr %21, i64 8
  %23 = getelementptr inbounds [256 x ptr], ptr %22, i64 0, i64 %.02631
  store ptr null, ptr %23, align 8, !tbaa !8
  %24 = load ptr, ptr %12, align 8, !tbaa !8
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  %26 = getelementptr inbounds [256 x ptr], ptr %25, i64 0, i64 %.02631
  store ptr null, ptr %26, align 8, !tbaa !8
  %27 = load ptr, ptr %12, align 8, !tbaa !8
  %28 = getelementptr inbounds i8, ptr %27, i64 8
  %29 = getelementptr inbounds [256 x ptr], ptr %28, i64 0, i64 %.02631
  store ptr null, ptr %29, align 8, !tbaa !8
  %30 = add nuw nsw i32 %.030, 4
  %exitcond.not.3 = icmp eq i32 %30, 256
  br i1 %exitcond.not.3, label %.loopexit.loopexit, label %17, !llvm.loop !12

.loopexit.loopexit:                               ; preds = %17
  %.pre = load ptr, ptr %12, align 8, !tbaa !8
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %.lr.ph
  %31 = phi ptr [ %.pre, %.loopexit.loopexit ], [ %13, %.lr.ph ]
  %32 = add nuw i64 %.02631, 1
  %exitcond33.not = icmp eq i64 %32, %2
  br i1 %exitcond33.not, label %.loopexit29, label %.lr.ph, !llvm.loop !14

.loopexit29:                                      ; preds = %.loopexit, %6, %3
  %.024 = phi ptr [ null, %3 ], [ %7, %6 ], [ %31, %.loopexit ]
  ret ptr %.024
}

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

attributes #0 = { nofree 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 = { 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 #2 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !9, i64 0}
!11 = !{!"tnode_t", !9, i64 0, !6, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass