; LLVM IR from albertobsd/keyhunt :: bsgsd.ll (Z14bsgs_partitionP11bsgs_xvaluel)

; 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.bsgs_xvalue = type { [6 x i8], i64 }

@BSGS_XVALUE_RAM = external dso_local global i64, 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z9bsgs_swapP11bsgs_xvalueS0_(ptr noundef, ptr noundef) #3

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z14bsgs_partitionP11bsgs_xvaluel(ptr noundef %0, i64 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca %struct.bsgs_xvalue, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %9 = load i64, ptr %4, align 8, !tbaa !9
  %10 = sdiv i64 %9, 2
  store i64 %10, ptr %6, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = getelementptr inbounds %struct.bsgs_xvalue, ptr %11, i64 %12
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 8 %13, i64 16, i1 false), !tbaa.struct !11
  store i64 0, ptr %7, align 8, !tbaa !9
  %14 = load i64, ptr %4, align 8, !tbaa !9
  %15 = sub nsw i64 %14, 1
  store i64 %15, ptr %8, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %91, %2
  br label %17

17:                                               ; preds = %34, %16
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = icmp slt i64 %18, %19
  br i1 %20, label %21, label %32

21:                                               ; preds = %17
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.bsgs_xvalue, ptr %22, i64 %23
  %25 = getelementptr inbounds %struct.bsgs_xvalue, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds [6 x i8], ptr %25, i64 0, i64 0
  %27 = getelementptr inbounds %struct.bsgs_xvalue, ptr %5, i32 0, i32 0
  %28 = getelementptr inbounds [6 x i8], ptr %27, i64 0, i64 0
  %29 = load i64, ptr @BSGS_XVALUE_RAM, align 8, !tbaa !9
  %30 = call i32 @memcmp(ptr noundef %26, ptr noundef %28, i64 noundef %29) #5
  %31 = icmp sle i32 %30, 0
  br label %32

32:                                               ; preds = %21, %17
  %33 = phi i1 [ false, %17 ], [ %31, %21 ]
  br i1 %33, label %34, label %37

34:                                               ; preds = %32
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = add nsw i64 %35, 1
  store i64 %36, ptr %7, align 8, !tbaa !9
  br label %17, !llvm.loop !13

37:                                               ; preds = %32
  br label %38

38:                                               ; preds = %55, %37
  %39 = load i64, ptr %8, align 8, !tbaa !9
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = icmp sge i64 %39, %40
  br i1 %41, label %42, label %53

42:                                               ; preds = %38
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = load i64, ptr %8, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.bsgs_xvalue, ptr %43, i64 %44
  %46 = getelementptr inbounds %struct.bsgs_xvalue, ptr %45, i32 0, i32 0
  %47 = getelementptr inbounds [6 x i8], ptr %46, i64 0, i64 0
  %48 = getelementptr inbounds %struct.bsgs_xvalue, ptr %5, i32 0, i32 0
  %49 = getelementptr inbounds [6 x i8], ptr %48, i64 0, i64 0
  %50 = load i64, ptr @BSGS_XVALUE_RAM, align 8, !tbaa !9
  %51 = call i32 @memcmp(ptr noundef %47, ptr noundef %49, i64 noundef %50) #5
  %52 = icmp sgt i32 %51, 0
  br label %53

53:                                               ; preds = %42, %38
  %54 = phi i1 [ false, %38 ], [ %52, %42 ]
  br i1 %54, label %55, label %58

55:                                               ; preds = %53
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = add nsw i64 %56, -1
  store i64 %57, ptr %8, align 8, !tbaa !9
  br label %38, !llvm.loop !15

58:                                               ; preds = %53
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = icmp slt i64 %59, %60
  br i1 %61, label %62, label %90

62:                                               ; preds = %58
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = load i64, ptr %6, align 8, !tbaa !9
  %65 = icmp eq i64 %63, %64
  br i1 %65, label %70, label %66

66:                                               ; preds = %62
  %67 = load i64, ptr %8, align 8, !tbaa !9
  %68 = load i64, ptr %6, align 8, !tbaa !9
  %69 = icmp eq i64 %67, %68
  br i1 %69, label %70, label %83

70:                                               ; preds = %66, %62
  %71 = load i64, ptr %7, align 8, !tbaa !9
  %72 = load i64, ptr %6, align 8, !tbaa !9
  %73 = icmp eq i64 %71, %72
  br i1 %73, label %74, label %76

74:                                               ; preds = %70
  %75 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %75, ptr %6, align 8, !tbaa !9
  br label %76

76:                                               ; preds = %74, %70
  %77 = load i64, ptr %8, align 8, !tbaa !9
  %78 = load i64, ptr %6, align 8, !tbaa !9
  %79 = icmp eq i64 %77, %78
  br i1 %79, label %80, label %82

80:                                               ; preds = %76
  %81 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %81, ptr %6, align 8, !tbaa !9
  br label %82

82:                                               ; preds = %80, %76
  br label %83

83:                                               ; preds = %82, %66
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = load i64, ptr %8, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.bsgs_xvalue, ptr %84, i64 %85
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = load i64, ptr %7, align 8, !tbaa !9
  %89 = getelementptr inbounds %struct.bsgs_xvalue, ptr %87, i64 %88
  call void @_Z9bsgs_swapP11bsgs_xvalueS0_(ptr noundef %86, ptr noundef %89)
  br label %90

90:                                               ; preds = %83, %58
  br label %91

91:                                               ; preds = %90
  %92 = load i64, ptr %7, align 8, !tbaa !9
  %93 = load i64, ptr %8, align 8, !tbaa !9
  %94 = icmp slt i64 %92, %93
  br i1 %94, label %16, label %95, !llvm.loop !16

95:                                               ; preds = %91
  %96 = load i64, ptr %8, align 8, !tbaa !9
  %97 = load i64, ptr %6, align 8, !tbaa !9
  %98 = icmp ne i64 %96, %97
  br i1 %98, label %99, label %106

99:                                               ; preds = %95
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = load i64, ptr %8, align 8, !tbaa !9
  %102 = getelementptr inbounds %struct.bsgs_xvalue, ptr %100, i64 %101
  %103 = load ptr, ptr %3, align 8, !tbaa !5
  %104 = load i64, ptr %6, align 8, !tbaa !9
  %105 = getelementptr inbounds %struct.bsgs_xvalue, ptr %103, i64 %104
  call void @_Z9bsgs_swapP11bsgs_xvalueS0_(ptr noundef %102, ptr noundef %105)
  br label %106

106:                                              ; preds = %99, %95
  %107 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #4
  ret i64 %107
}

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

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{i64 0, i64 6, !12, i64 8, i64 8, !9}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


; 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.bsgs_xvalue = type { [6 x i8], i64 }

@BSGS_XVALUE_RAM = external dso_local local_unnamed_addr global i64, align 8

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @memcmp(ptr nocapture noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_Z9bsgs_swapP11bsgs_xvalueS0_(ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i64 -9223372036854775808, 9223372036854775807) i64 @_Z14bsgs_partitionP11bsgs_xvaluel(ptr noundef %0, i64 noundef %1) local_unnamed_addr #3 {
  %3 = alloca %struct.bsgs_xvalue, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  %4 = sdiv i64 %1, 2
  %5 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %3, ptr noundef nonnull align 8 dereferenceable(16) %5, i64 16, i1 false), !tbaa.struct !5
  %6 = add nsw i64 %1, -1
  br label %7

7:                                                ; preds = %25, %2
  %.042 = phi i64 [ %4, %2 ], [ %.2, %25 ]
  %.040 = phi i64 [ 0, %2 ], [ %.141.lcssa, %25 ]
  %.0 = phi i64 [ %6, %2 ], [ %.1.lcssa, %25 ]
  %8 = icmp slt i64 %.040, %.0
  br i1 %8, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %7
  %9 = load i64, ptr @BSGS_XVALUE_RAM, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %.lr.ph, %14
  %.14148 = phi i64 [ %.040, %.lr.ph ], [ %15, %14 ]
  %11 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %.14148
  %12 = call i32 @memcmp(ptr noundef %11, ptr noundef nonnull %3, i64 noundef %9) #5
  %13 = icmp slt i32 %12, 1
  br i1 %13, label %14, label %.critedge

14:                                               ; preds = %10
  %15 = add i64 %.14148, 1
  %exitcond.not = icmp eq i64 %15, %.0
  br i1 %exitcond.not, label %.critedge, label %10, !llvm.loop !11

.critedge:                                        ; preds = %10, %14, %7
  %.141.lcssa = phi i64 [ %.040, %7 ], [ %.0, %14 ], [ %.14148, %10 ]
  %.not51 = icmp slt i64 %.0, %.141.lcssa
  br i1 %.not51, label %.critedge2, label %.lr.ph53

.lr.ph53:                                         ; preds = %.critedge
  %16 = load i64, ptr @BSGS_XVALUE_RAM, align 8, !tbaa !9
  %17 = add i64 %.141.lcssa, -1
  br label %18

18:                                               ; preds = %.lr.ph53, %22
  %.152 = phi i64 [ %.0, %.lr.ph53 ], [ %23, %22 ]
  %19 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %.152
  %20 = call i32 @memcmp(ptr noundef %19, ptr noundef nonnull %3, i64 noundef %16) #5
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %22, label %.critedge2

22:                                               ; preds = %18
  %23 = add nsw i64 %.152, -1
  %.not.not = icmp sgt i64 %.152, %.141.lcssa
  br i1 %.not.not, label %18, label %.critedge2, !llvm.loop !13

.critedge2:                                       ; preds = %18, %22, %.critedge
  %.1.lcssa = phi i64 [ %.0, %.critedge ], [ %17, %22 ], [ %.152, %18 ]
  %24 = icmp slt i64 %.141.lcssa, %.1.lcssa
  br i1 %24, label %25, label %31

25:                                               ; preds = %.critedge2
  %26 = icmp eq i64 %.141.lcssa, %.042
  %27 = icmp eq i64 %.1.lcssa, %.042
  %or.cond = or i1 %26, %27
  %spec.select = select i1 %26, i64 %.1.lcssa, i64 %.042
  %28 = icmp eq i64 %.1.lcssa, %spec.select
  %spec.select45 = select i1 %28, i64 %.141.lcssa, i64 %spec.select
  %.2 = select i1 %or.cond, i64 %spec.select45, i64 %.042
  %29 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %.1.lcssa
  %30 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %.141.lcssa
  tail call void @_Z9bsgs_swapP11bsgs_xvalueS0_(ptr noundef %29, ptr noundef %30)
  br label %7

31:                                               ; preds = %.critedge2
  %.not44 = icmp eq i64 %.1.lcssa, %.042
  br i1 %.not44, label %35, label %32

32:                                               ; preds = %31
  %33 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %.1.lcssa
  %34 = getelementptr inbounds %struct.bsgs_xvalue, ptr %0, i64 %.042
  tail call void @_Z9bsgs_swapP11bsgs_xvalueS0_(ptr noundef %33, ptr noundef %34)
  br label %35

35:                                               ; preds = %32, %31
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  ret i64 %.1.lcssa
}

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

!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 = !{i64 0, i64 6, !6, i64 8, i64 8, !9}
!6 = !{!7, !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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