; LLVM IR from HarshCasper/NeoAlgo :: C/Bit--Manipulation (minimum_xor_value)

; 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"

@.str = external hidden unnamed_addr constant [32 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @min_xor(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %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 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 2147483647, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %46, %2
  %11 = load i32, ptr %8, align 4, !tbaa !9
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = sub nsw i32 %12, 1
  %14 = icmp slt i32 %11, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %49

16:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i32, ptr %17, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i32, ptr %8, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i32, ptr %22, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = xor i32 %21, %27
  store i32 %28, ptr %9, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %45

32:                                               ; preds = %16
  %33 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %33, ptr %7, align 4, !tbaa !9
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  store i32 %38, ptr %5, align 4, !tbaa !9
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i32, ptr %39, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  store i32 %44, ptr %6, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %32, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %8, align 4, !tbaa !9
  br label %10, !llvm.loop !11

49:                                               ; preds = %15
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %50)
  %52 = load i32, ptr %5, align 4, !tbaa !9
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %52, i32 noundef %53)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

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

declare i32 @printf(ptr noundef, ...) #2

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 = { "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 }

!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"

@.str = external hidden unnamed_addr constant [32 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local void @min_xor(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 1
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %4 = add nsw i32 %1, -1
  %wide.trip.count = zext nneg i32 %4 to i64
  %.pre = load i32, ptr %0, align 4, !tbaa !5
  %xtraiter = and i64 %wide.trip.count, 1
  %5 = icmp eq i32 %4, 1
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa.loopexit:          ; preds = %.lr.ph
  %6 = add nuw nsw i64 %indvars.iv, 3
  br label %._crit_edge.loopexit.unr-lcssa

._crit_edge.loopexit.unr-lcssa:                   ; preds = %._crit_edge.loopexit.unr-lcssa.loopexit, %.lr.ph.preheader
  %.120.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.120.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.118.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.118.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.1.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.unr = phi i32 [ %.pre, %.lr.ph.preheader ], [ %19, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %6, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.025.unr = phi i32 [ undef, %.lr.ph.preheader ], [ %.1.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01723.unr = phi i32 [ 2147483647, %.lr.ph.preheader ], [ %.118.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %.01922.unr = phi i32 [ undef, %.lr.ph.preheader ], [ %.120.1, %._crit_edge.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %7 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = xor i32 %8, %.unr
  %10 = icmp slt i32 %9, %.01723.unr
  %.120.epil = select i1 %10, i32 %8, i32 %.01922.unr
  %.118.epil = tail call i32 @llvm.smin.i32(i32 %9, i32 %.01723.unr)
  %.1.epil = select i1 %10, i32 %.unr, i32 %.025.unr
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %2
  %.019.lcssa = phi i32 [ undef, %2 ], [ %.120.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.120.epil, %.lr.ph.epil ]
  %.017.lcssa = phi i32 [ 2147483647, %2 ], [ %.118.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.118.epil, %.lr.ph.epil ]
  %.0.lcssa = phi i32 [ undef, %2 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.1.epil, %.lr.ph.epil ]
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %.017.lcssa)
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.0.lcssa, i32 noundef %.019.lcssa)
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %13 = phi i32 [ %.pre, %.lr.ph.preheader.new ], [ %19, %.lr.ph ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %.025 = phi i32 [ undef, %.lr.ph.preheader.new ], [ %.1.1, %.lr.ph ]
  %.01723 = phi i32 [ 2147483647, %.lr.ph.preheader.new ], [ %.118.1, %.lr.ph ]
  %.01922 = phi i32 [ undef, %.lr.ph.preheader.new ], [ %.120.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = xor i32 %15, %13
  %17 = icmp slt i32 %16, %.01723
  %.120 = select i1 %17, i32 %15, i32 %.01922
  %.118 = tail call i32 @llvm.smin.i32(i32 %16, i32 %.01723)
  %.1 = select i1 %17, i32 %13, i32 %.025
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %18 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = xor i32 %19, %15
  %21 = icmp slt i32 %20, %.118
  %.120.1 = select i1 %21, i32 %19, i32 %.120
  %.118.1 = tail call i32 @llvm.smin.i32(i32 %20, i32 %.118)
  %.1.1 = select i1 %21, i32 %15, i32 %.1
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !9
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #2

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 = { nofree nounwind "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"int", !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: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass