; LLVM IR from AlgoGenesis/C :: Trie/Maximum XOR (Program)

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

11:                                               ; preds = %49, %2
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = icmp sge i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %52

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = ashr i32 %16, %17
  %19 = and i32 %18, 1
  store i32 %19, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = sub nsw i32 1, %20
  store i32 %21, ptr %9, align 4, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.TrieNode, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [2 x ptr], ptr %23, i64 0, i64 %25
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %39

29:                                               ; preds = %15
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = shl i32 %30, 1
  %32 = or i32 %31, 1
  store i32 %32, ptr %6, align 4, !tbaa !9
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.TrieNode, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [2 x ptr], ptr %34, i64 0, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  store ptr %38, ptr %5, align 8, !tbaa !5
  br label %48

39:                                               ; preds = %15
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = shl i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !9
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.TrieNode, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [2 x ptr], ptr %43, i64 0, i64 %45
  %47 = load ptr, ptr %46, align 8, !tbaa !5
  store ptr %47, ptr %5, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %39, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %49

49:                                               ; preds = %48
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = add nsw i32 %50, -1
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %11, !llvm.loop !11

52:                                               ; preds = %14
  %53 = load i32, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i32 %53
}

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @findMaxXOR(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %4

3:                                                ; preds = %33
  ret i32 %.115.1

4:                                                ; preds = %33, %2
  %.020 = phi ptr [ %0, %2 ], [ %.1.1, %33 ]
  %.01419 = phi i32 [ 0, %2 ], [ %.115.1, %33 ]
  %.01618 = phi i32 [ 31, %2 ], [ %34, %33 ]
  %5 = lshr i32 %1, %.01618
  %6 = and i32 %5, 1
  %7 = xor i32 %6, 1
  %8 = zext nneg i32 %7 to i64
  %9 = getelementptr inbounds [2 x ptr], ptr %.020, i64 0, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %.not = icmp eq ptr %10, null
  %11 = shl i32 %.01419, 1
  br i1 %.not, label %14, label %12

12:                                               ; preds = %4
  %13 = or disjoint i32 %11, 1
  br label %18

14:                                               ; preds = %4
  %15 = zext nneg i32 %6 to i64
  %16 = getelementptr inbounds [2 x ptr], ptr %.020, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %14, %12
  %.115 = phi i32 [ %13, %12 ], [ %11, %14 ]
  %.1 = phi ptr [ %10, %12 ], [ %17, %14 ]
  %19 = add nsw i32 %.01618, -1
  %20 = lshr i32 %1, %19
  %21 = and i32 %20, 1
  %22 = xor i32 %21, 1
  %23 = zext nneg i32 %22 to i64
  %24 = getelementptr inbounds [2 x ptr], ptr %.1, i64 0, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %.not.1 = icmp eq ptr %25, null
  %26 = shl i32 %.115, 1
  br i1 %.not.1, label %29, label %27

27:                                               ; preds = %18
  %28 = or disjoint i32 %26, 1
  br label %33

29:                                               ; preds = %18
  %30 = zext nneg i32 %21 to i64
  %31 = getelementptr inbounds [2 x ptr], ptr %.1, i64 0, i64 %30
  %32 = load ptr, ptr %31, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %29, %27
  %.115.1 = phi i32 [ %28, %27 ], [ %26, %29 ]
  %.1.1 = phi ptr [ %25, %27 ], [ %32, %29 ]
  %34 = add nsw i32 %.01618, -2
  %.not21.1 = icmp eq i32 %19, 0
  br i1 %.not21.1, label %3, label %4, !llvm.loop !9
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: 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" }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass