; LLVM IR from clausecker/24puzzle :: compact (pack_puzzle_masked)

; 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.compact_puzzle = type { i64, i64 }

; Function Attrs: nounwind uwtable
declare dso_local void @pack_puzzle(ptr noalias noundef, ptr noalias noundef) #0

; 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

; Function Attrs: nounwind uwtable
define dso_local void @pack_puzzle_masked(ptr noalias noundef %0, ptr noalias noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  call void @pack_puzzle(ptr noundef %11, ptr noundef %12)
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = call i64 @zero_location(ptr noundef %13)
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %9, align 4, !tbaa !9
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = call i64 @move_count(i64 noundef %17)
  store i64 %18, ptr %8, align 8, !tbaa !11
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = call ptr @get_moves(i64 noundef %20)
  store ptr %21, ptr %10, align 8, !tbaa !5
  store i64 0, ptr %7, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %44, %3
  %23 = load i64, ptr %7, align 8, !tbaa !11
  %24 = load i64, ptr %8, align 8, !tbaa !11
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %26, label %47

26:                                               ; preds = %22
  %27 = load ptr, ptr %10, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = sext i8 %30 to i32
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = icmp eq i32 %31, %32
  br i1 %33, label %34, label %43

34:                                               ; preds = %26
  %35 = load i64, ptr %7, align 8, !tbaa !11
  %36 = trunc i64 %35 to i32
  %37 = shl i32 1, %36
  %38 = sext i32 %37 to i64
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.compact_puzzle, ptr %39, i32 0, i32 0
  %41 = load i64, ptr %40, align 8, !tbaa !14
  %42 = or i64 %41, %38
  store i64 %42, ptr %40, align 8, !tbaa !14
  br label %43

43:                                               ; preds = %34, %26
  br label %44

44:                                               ; preds = %43
  %45 = load i64, ptr %7, align 8, !tbaa !11
  %46 = add i64 %45, 1
  store i64 %46, ptr %7, align 8, !tbaa !11
  br label %22, !llvm.loop !17

47:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @zero_location(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @move_count(i64 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @get_moves(i64 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 = { inlinehint 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 }

!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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !16, i64 0}
!15 = !{!"compact_puzzle", !16, i64 0, !16, i64 8}
!16 = !{!"long long", !7, 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
declare dso_local void @pack_puzzle(ptr noalias noundef, ptr noalias noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @pack_puzzle_masked(ptr noalias noundef %0, ptr noalias noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  tail call void @pack_puzzle(ptr noundef %0, ptr noundef %1)
  %4 = tail call i64 @zero_location(ptr noundef %1)
  %sext = shl i64 %4, 32
  %5 = ashr exact i64 %sext, 32
  %6 = tail call i64 @move_count(i64 noundef %5)
  %7 = tail call ptr @get_moves(i64 noundef %5)
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i64 %6, 1
  %8 = icmp eq i64 %6, 1
  br i1 %8, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %6, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %30, %.lr.ph.preheader.new
  %.012 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %31, %30 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %30 ]
  %9 = getelementptr inbounds i8, ptr %7, i64 %.012
  %10 = load i8, ptr %9, align 1, !tbaa !5
  %11 = sext i8 %10 to i32
  %12 = icmp eq i32 %11, %2
  br i1 %12, label %13, label %.lr.ph.1

13:                                               ; preds = %.lr.ph
  %14 = trunc i64 %.012 to i32
  %15 = shl nuw i32 1, %14
  %16 = zext nneg i32 %15 to i64
  %17 = load i64, ptr %0, align 8, !tbaa !8
  %18 = or i64 %17, %16
  store i64 %18, ptr %0, align 8, !tbaa !8
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %13
  %19 = or disjoint i64 %.012, 1
  %20 = getelementptr inbounds i8, ptr %7, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %22 = sext i8 %21 to i32
  %23 = icmp eq i32 %22, %2
  br i1 %23, label %24, label %30

24:                                               ; preds = %.lr.ph.1
  %25 = trunc i64 %19 to i32
  %26 = shl nuw i32 1, %25
  %27 = sext i32 %26 to i64
  %28 = load i64, ptr %0, align 8, !tbaa !8
  %29 = or i64 %28, %27
  store i64 %29, ptr %0, align 8, !tbaa !8
  br label %30

30:                                               ; preds = %24, %.lr.ph.1
  %31 = add nuw i64 %.012, 2
  %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, label %.lr.ph, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %30, %.lr.ph.preheader
  %.012.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %31, %30 ]
  %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
  %32 = getelementptr inbounds i8, ptr %7, i64 %.012.unr
  %33 = load i8, ptr %32, align 1, !tbaa !5
  %34 = sext i8 %33 to i32
  %35 = icmp eq i32 %34, %2
  br i1 %35, label %36, label %._crit_edge

36:                                               ; preds = %.lr.ph.epil
  %37 = trunc i64 %.012.unr to i32
  %38 = shl nuw i32 1, %37
  %39 = sext i32 %38 to i64
  %40 = load i64, ptr %0, align 8, !tbaa !8
  %41 = or i64 %40, %39
  store i64 %41, ptr %0, align 8, !tbaa !8
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %36, %.lr.ph.epil, %3
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @zero_location(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @move_count(i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @get_moves(i64 noundef) local_unnamed_addr #1

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

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


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