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

; 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 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) #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z17bsgs_searchbinaryP11bsgs_xvaluePclPm(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #2 {
  %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 i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = 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
  store ptr %3, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  store i64 0, ptr %12, align 8, !tbaa !9
  %15 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %15, ptr %10, align 8, !tbaa !9
  %16 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %16, ptr %11, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %65, %4
  %18 = load i32, ptr %13, align 4, !tbaa !11
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %23, label %20

20:                                               ; preds = %17
  %21 = load i64, ptr %11, align 8, !tbaa !9
  %22 = icmp sge i64 %21, 1
  br label %23

23:                                               ; preds = %20, %17
  %24 = phi i1 [ false, %17 ], [ %22, %20 ]
  br i1 %24, label %25, label %66

25:                                               ; preds = %23
  %26 = load i64, ptr %10, align 8, !tbaa !9
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = sub nsw i64 %26, %27
  %29 = sdiv i64 %28, 2
  store i64 %29, ptr %11, align 8, !tbaa !9
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %30, i64 16
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i64, ptr %12, align 8, !tbaa !9
  %34 = load i64, ptr %11, align 8, !tbaa !9
  %35 = add nsw i64 %33, %34
  %36 = getelementptr inbounds %struct.bsgs_xvalue, ptr %32, i64 %35
  %37 = getelementptr inbounds %struct.bsgs_xvalue, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds [6 x i8], ptr %37, i64 0, i64 0
  %39 = load i64, ptr @BSGS_XVALUE_RAM, align 8, !tbaa !9
  %40 = call i32 @memcmp(ptr noundef %31, ptr noundef %38, i64 noundef %39) #4
  store i32 %40, ptr %14, align 4, !tbaa !11
  %41 = load i32, ptr %14, align 4, !tbaa !11
  %42 = icmp eq i32 %41, 0
  br i1 %42, label %43, label %52

43:                                               ; preds = %25
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = load i64, ptr %12, align 8, !tbaa !9
  %46 = load i64, ptr %11, align 8, !tbaa !9
  %47 = add nsw i64 %45, %46
  %48 = getelementptr inbounds %struct.bsgs_xvalue, ptr %44, i64 %47
  %49 = getelementptr inbounds %struct.bsgs_xvalue, ptr %48, i32 0, i32 1
  %50 = load i64, ptr %49, align 8, !tbaa !13
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 %50, ptr %51, align 8, !tbaa !9
  store i32 1, ptr %13, align 4, !tbaa !11
  br label %65

52:                                               ; preds = %25
  %53 = load i32, ptr %14, align 4, !tbaa !11
  %54 = icmp slt i32 %53, 0
  br i1 %54, label %55, label %59

55:                                               ; preds = %52
  %56 = load i64, ptr %10, align 8, !tbaa !9
  %57 = load i64, ptr %11, align 8, !tbaa !9
  %58 = sub nsw i64 %56, %57
  store i64 %58, ptr %10, align 8, !tbaa !9
  br label %63

59:                                               ; preds = %52
  %60 = load i64, ptr %9, align 8, !tbaa !9
  %61 = load i64, ptr %11, align 8, !tbaa !9
  %62 = add nsw i64 %60, %61
  store i64 %62, ptr %9, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %59, %55
  %64 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %64, ptr %12, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %63, %43
  br label %17, !llvm.loop !15

66:                                               ; preds = %23
  %67 = load i32, ptr %13, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i32 %67
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { nounwind }
attributes #4 = { 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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !10, i64 8}
!14 = !{!"_ZTS11bsgs_xvalue", !7, i64 0, !10, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%struct.bsgs_xvalue = type { [6 x i8], i64 }

@BSGS_XVALUE_RAM = external dso_local local_unnamed_addr global i64, align 8

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

; Function Attrs: mustprogress nofree nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef range(i32 0, 2) i32 @_Z17bsgs_searchbinaryP11bsgs_xvaluePclPm(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #1 {
  %5 = icmp sgt i64 %2, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %6 = getelementptr inbounds i8, ptr %1, i64 16
  %.pre33 = load i64, ptr @BSGS_XVALUE_RAM, align 8, !tbaa !5
  br label %7

7:                                                ; preds = %.lr.ph, %16
  %.032 = phi i64 [ 0, %.lr.ph ], [ %.1, %16 ]
  %.02630 = phi i64 [ %2, %.lr.ph ], [ %.127, %16 ]
  %8 = sub nsw i64 %.02630, %.032
  %9 = sdiv i64 %8, 2
  %10 = getelementptr %struct.bsgs_xvalue, ptr %0, i64 %.032
  %11 = getelementptr %struct.bsgs_xvalue, ptr %10, i64 %9
  %12 = tail call i32 @memcmp(ptr noundef nonnull %6, ptr noundef %11, i64 noundef %.pre33) #2
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %16, label %.thread

.thread:                                          ; preds = %7
  %14 = getelementptr inbounds i8, ptr %11, i64 8
  %15 = load i64, ptr %14, align 8, !tbaa !9
  store i64 %15, ptr %3, align 8, !tbaa !5
  br label %._crit_edge

16:                                               ; preds = %7
  %17 = icmp slt i32 %12, 0
  %18 = select i1 %17, i64 %9, i64 0
  %.127 = sub nsw i64 %.02630, %18
  %19 = select i1 %17, i64 0, i64 %9
  %.1 = add nsw i64 %19, %.032
  %20 = icmp sgt i64 %8, 1
  %21 = and i1 %20, %13
  br i1 %21, label %7, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %16, %.thread, %4
  %.021.lcssa = phi i32 [ 0, %4 ], [ 1, %.thread ], [ 0, %16 ]
  ret i32 %.021.lcssa
}

attributes #0 = { 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 #1 = { mustprogress nofree nounwind memory(read, argmem: readwrite, 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" }
attributes #2 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTS11bsgs_xvalue", !7, i64 0, !6, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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