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

; 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.jsw_avltree = type { ptr, ptr, ptr, ptr, i64 }
%struct.jsw_avlnode = type { i32, ptr, [2 x ptr] }

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

; 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 ptr @jsw_avlfind(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.jsw_avltree, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %10, ptr %5, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %37, %2
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %38

14:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.jsw_avltree, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.jsw_avlnode, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = call i32 %17(ptr noundef %20, ptr noundef %21)
  store i32 %22, ptr %6, align 4, !tbaa !16
  %23 = load i32, ptr %6, align 4, !tbaa !16
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %14
  store i32 3, ptr %7, align 4
  br label %35

26:                                               ; preds = %14
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.jsw_avlnode, ptr %27, i32 0, i32 2
  %29 = load i32, ptr %6, align 4, !tbaa !16
  %30 = icmp slt i32 %29, 0
  %31 = zext i1 %30 to i32
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [2 x ptr], ptr %28, i64 0, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  store ptr %34, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %7, align 4
  br label %35

35:                                               ; preds = %26, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %36 = load i32, ptr %7, align 4
  switch i32 %36, label %48 [
    i32 0, label %37
    i32 3, label %38
  ]

37:                                               ; preds = %35
  br label %11, !llvm.loop !17

38:                                               ; preds = %35, %11
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = icmp eq ptr %39, null
  br i1 %40, label %41, label %42

41:                                               ; preds = %38
  br label %46

42:                                               ; preds = %38
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.jsw_avlnode, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !13
  br label %46

46:                                               ; preds = %42, %41
  %47 = phi ptr [ null, %41 ], [ %45, %42 ]
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %47

48:                                               ; preds = %35
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"jsw_avltree", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !6, i64 8}
!13 = !{!14, !6, i64 8}
!14 = !{!"jsw_avlnode", !15, i64 0, !6, i64 8, !7, i64 16}
!15 = !{!"int", !7, i64 0}
!16 = !{!15, !15, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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: nounwind uwtable
define dso_local ptr @jsw_avlfind(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #0 {
  %.01018 = load ptr, ptr %0, align 8, !tbaa !5
  %.not19 = icmp eq ptr %.01018, null
  br i1 %.not19, label %.thread15, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  br label %4

4:                                                ; preds = %.lr.ph, %10
  %.01020 = phi ptr [ %.01018, %.lr.ph ], [ %.010, %10 ]
  %5 = load ptr, ptr %3, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %.01020, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = tail call i32 %5(ptr noundef %7, ptr noundef %1) #1
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %14, label %10

10:                                               ; preds = %4
  %11 = getelementptr inbounds i8, ptr %.01020, i64 16
  %.lobit = lshr i32 %8, 31
  %12 = zext nneg i32 %.lobit to i64
  %13 = getelementptr inbounds [2 x ptr], ptr %11, i64 0, i64 %12
  %.010 = load ptr, ptr %13, align 8, !tbaa !5
  %.not = icmp eq ptr %.010, null
  br i1 %.not, label %.thread15, label %4

14:                                               ; preds = %4
  %15 = getelementptr inbounds i8, ptr %.01020, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !12
  br label %.thread15

.thread15:                                        ; preds = %10, %2, %14
  %17 = phi ptr [ %16, %14 ], [ null, %2 ], [ null, %10 ]
  ret ptr %17
}

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 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"jsw_avltree", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"jsw_avlnode", !14, i64 0, !6, i64 8, !7, i64 16}
!14 = !{!"int", !7, i64 0}


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