; LLVM IR from clausecker/24puzzle :: fsm (fsm_get_moves)

; 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.fsm_state = type { i32, i32 }
%struct.fsm = type { [25 x i32], [25 x ptr], [25 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, 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 uwtable
define dso_local i32 @fsm_get_moves(ptr noundef align 1 dereferenceable(4) %0, i64 %1, ptr noundef %2) #2 {
  %4 = alloca %struct.fsm_state, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store i64 %1, ptr %4, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 1 %10, i8 -1, i64 4, i1 false)
  %11 = getelementptr inbounds %struct.fsm_state, ptr %4, i32 0, i32 0
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %13 = zext i32 %12 to i64
  %14 = call ptr @get_moves(i64 noundef %13)
  store ptr %14, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !12
  store i32 0, ptr %7, align 4, !tbaa !12
  br label %15

15:                                               ; preds = %52, %3
  %16 = load i32, ptr %8, align 4, !tbaa !12
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds %struct.fsm_state, ptr %4, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = zext i32 %19 to i64
  %21 = call i64 @move_count(i64 noundef %20)
  %22 = icmp ult i64 %17, %21
  br i1 %22, label %23, label %55

23:                                               ; preds = %15
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.fsm, ptr %24, i32 0, i32 1
  %26 = getelementptr inbounds %struct.fsm_state, ptr %4, i32 0, i32 0
  %27 = load i32, ptr %26, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds [25 x ptr], ptr %25, i64 0, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.fsm_state, ptr %4, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [4 x i32], ptr %30, i64 %33
  %35 = load i32, ptr %8, align 4, !tbaa !12
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [4 x i32], ptr %34, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !12
  %39 = icmp ne i32 %38, -2
  br i1 %39, label %40, label %51

40:                                               ; preds = %23
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !12
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i8, ptr %41, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !14
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !12
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %7, align 4, !tbaa !12
  %49 = sext i32 %47 to i64
  %50 = getelementptr inbounds i8, ptr %46, i64 %49
  store i8 %45, ptr %50, align 1, !tbaa !14
  br label %51

51:                                               ; preds = %40, %23
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %8, align 4, !tbaa !12
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %8, align 4, !tbaa !12
  br label %15, !llvm.loop !15

55:                                               ; preds = %15
  %56 = load i32, ptr %7, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret i32 %56
}

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @fsm_get_moves(ptr nocapture noundef writeonly align 1 dereferenceable(4) %0, i64 %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %.sroa.4.0.extract.shift = lshr i64 %1, 32
  store i32 -1, ptr %0, align 1
  %4 = and i64 %1, 4294967295
  %5 = tail call ptr @get_moves(i64 noundef %4)
  %6 = tail call i64 @move_count(i64 noundef %4)
  %.not15 = icmp eq i64 %6, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %7 = getelementptr inbounds i8, ptr %2, i64 104
  %8 = getelementptr inbounds [25 x ptr], ptr %7, i64 0, i64 %4
  br label %9

9:                                                ; preds = %.lr.ph, %19
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %19 ]
  %.014 = phi i32 [ 0, %.lr.ph ], [ %.1, %19 ]
  %10 = load ptr, ptr %8, align 8, !tbaa !5
  %11 = getelementptr inbounds [4 x i32], ptr %10, i64 %.sroa.4.0.extract.shift, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !9
  %.not = icmp eq i32 %12, -2
  br i1 %.not, label %19, label %13

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = add nsw i32 %.014, 1
  %17 = sext i32 %.014 to i64
  %18 = getelementptr inbounds i8, ptr %0, i64 %17
  store i8 %15, ptr %18, align 1, !tbaa !11
  br label %19

19:                                               ; preds = %9, %13
  %.1 = phi i32 [ %16, %13 ], [ %.014, %9 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %20 = tail call i64 @move_count(i64 noundef %4)
  %21 = icmp ugt i64 %20, %indvars.iv.next
  br i1 %21, label %9, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %19, %3
  %.0.lcssa = phi i32 [ 0, %3 ], [ %.1, %19 ]
  ret i32 %.0.lcssa
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @move_count(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 = !{!"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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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