; LLVM IR from clausecker/24puzzle :: random (random_walk)

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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.fsm_state = type { i32, i32 }

@seed_lock = external dso_local global %union.pthread_mutex_t, align 8
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [62 x i8], align 1
@__PRETTY_FUNCTION__.random_walk = external hidden unnamed_addr constant [58 x i8], align 1

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden i32 @random_step() #4

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @random_walk(ptr noundef %0, i32 noundef %1, ptr noundef %2) #4 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca %struct.fsm_state, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca [4 x i8], align 1
  %15 = alloca %struct.fsm_state, align 4
  %16 = alloca %struct.fsm_state, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #7
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #7
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #7
  store i32 0, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #7
  %17 = call i32 @pthread_mutex_lock(ptr noundef @seed_lock) #7
  store i32 %17, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr %9, align 4, !tbaa !9
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %3
  br label %22

21:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 269, ptr noundef @__PRETTY_FUNCTION__.random_walk) #8
  unreachable

22:                                               ; preds = %20
  %23 = call i32 @random_step()
  store i32 %23, ptr %8, align 4, !tbaa !9
  store i32 16, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #7
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = call i64 @zero_location(ptr noundef %24)
  %26 = call i64 @fsm_start_state(i64 noundef %25)
  store i64 %26, ptr %15, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %15, i64 8, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #7
  br label %27

27:                                               ; preds = %57, %22
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = icmp sgt i32 %28, 0
  br i1 %29, label %30, label %74

30:                                               ; preds = %27
  %31 = getelementptr inbounds [4 x i8], ptr %14, i64 0, i64 0
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = load i64, ptr %7, align 4
  %35 = call i32 @fsm_get_moves_moribund(ptr noundef %31, i64 %34, ptr noundef %32, i32 noundef %33)
  store i32 %35, ptr %12, align 4, !tbaa !9
  %36 = load i32, ptr %12, align 4, !tbaa !9
  switch i32 %36, label %39 [
    i32 0, label %37
    i32 1, label %38
  ]

37:                                               ; preds = %30
  br label %75

38:                                               ; preds = %30
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %57

39:                                               ; preds = %30
  br label %40

40:                                               ; preds = %52, %39
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = icmp eq i32 %41, 0
  br i1 %42, label %43, label %45

43:                                               ; preds = %40
  %44 = call i32 @random_step()
  store i32 %44, ptr %8, align 4, !tbaa !9
  store i32 16, ptr %13, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %43, %40
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = and i32 %46, 3
  store i32 %47, ptr %11, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = lshr i32 %48, 2
  store i32 %49, ptr %8, align 4, !tbaa !9
  %50 = load i32, ptr %13, align 4, !tbaa !9
  %51 = add nsw i32 %50, -1
  store i32 %51, ptr %13, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %45
  %53 = load i32, ptr %11, align 4, !tbaa !9
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = icmp sge i32 %53, %54
  br i1 %55, label %40, label %56, !llvm.loop !12

56:                                               ; preds = %52
  br label %57

57:                                               ; preds = %56, %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #7
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load i32, ptr %11, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [4 x i8], ptr %14, i64 0, i64 %60
  %62 = load i8, ptr %61, align 1, !tbaa !14
  %63 = sext i8 %62 to i64
  %64 = load i64, ptr %7, align 4
  %65 = call i64 @fsm_advance(ptr noundef %58, i64 %64, i64 noundef %63)
  store i64 %65, ptr %16, align 4
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %16, i64 8, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #7
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = load i32, ptr %11, align 4, !tbaa !9
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [4 x i8], ptr %14, i64 0, i64 %68
  %70 = load i8, ptr %69, align 1, !tbaa !14
  %71 = sext i8 %70 to i64
  call void @move(ptr noundef %66, i64 noundef %71)
  %72 = load i32, ptr %5, align 4, !tbaa !9
  %73 = add nsw i32 %72, -1
  store i32 %73, ptr %5, align 4, !tbaa !9
  br label %27, !llvm.loop !15

74:                                               ; preds = %27
  store i32 1, ptr %10, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %74, %37
  %76 = call i32 @pthread_mutex_unlock(ptr noundef @seed_lock) #7
  store i32 %76, ptr %9, align 4, !tbaa !9
  %77 = load i32, ptr %9, align 4, !tbaa !9
  %78 = icmp eq i32 %77, 0
  br i1 %78, label %79, label %80

79:                                               ; preds = %75
  br label %81

80:                                               ; preds = %75
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 306, ptr noundef @__PRETTY_FUNCTION__.random_walk) #8
  unreachable

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @fsm_start_state(i64 noundef) #5

declare i32 @fsm_get_moves_moribund(ptr noundef, i64, ptr noundef, i32 noundef) #6

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @fsm_advance(ptr noundef, i64, i64 noundef) #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @move(ptr noundef, i64 noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { noreturn 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { 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 #6 = { "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 #7 = { nounwind }
attributes #8 = { noreturn 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 = !{i64 0, i64 4, !9, i64 4, i64 4, !9}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !13}


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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@seed_lock = external dso_local global %union.pthread_mutex_t, align 8
@.str = external hidden unnamed_addr constant [9 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [62 x i8], align 1
@__PRETTY_FUNCTION__.random_walk = external hidden unnamed_addr constant [58 x i8], align 1

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @random_step() local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #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: inlinehint nounwind uwtable
declare hidden i64 @zero_location(ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @random_walk(ptr noundef %0, i32 noundef %1, ptr noundef %2) local_unnamed_addr #3 {
  %4 = alloca [4 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #6
  %5 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @seed_lock) #6
  %6 = icmp eq i32 %5, 0
  br i1 %6, label %8, label %7

7:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 269, ptr noundef nonnull @__PRETTY_FUNCTION__.random_walk) #7
  unreachable

8:                                                ; preds = %3
  %9 = tail call i32 @random_step()
  %10 = tail call i64 @zero_location(ptr noundef %0)
  %11 = tail call i64 @fsm_start_state(i64 noundef %10)
  %12 = icmp sgt i32 %1, 0
  br i1 %12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %8, %.loopexit
  %.034 = phi i32 [ %28, %.loopexit ], [ %1, %8 ]
  %.sroa.013.033 = phi i64 [ %25, %.loopexit ], [ %11, %8 ]
  %.02132 = phi i32 [ %.3, %.loopexit ], [ 16, %8 ]
  %.02231 = phi i32 [ %.325, %.loopexit ], [ %9, %8 ]
  %13 = call i32 @fsm_get_moves_moribund(ptr noundef nonnull %4, i64 %.sroa.013.033, ptr noundef %2, i32 noundef %.034) #6
  switch i32 %13, label %.preheader [
    i32 0, label %._crit_edge
    i32 1, label %.loopexit
  ]

.preheader:                                       ; preds = %.lr.ph, %17
  %.123 = phi i32 [ %19, %17 ], [ %.02231, %.lr.ph ]
  %.1 = phi i32 [ %20, %17 ], [ %.02132, %.lr.ph ]
  %14 = icmp eq i32 %.1, 0
  br i1 %14, label %15, label %17

15:                                               ; preds = %.preheader
  %16 = call i32 @random_step()
  br label %17

17:                                               ; preds = %15, %.preheader
  %.224 = phi i32 [ %16, %15 ], [ %.123, %.preheader ]
  %.2 = phi i32 [ 16, %15 ], [ %.1, %.preheader ]
  %18 = and i32 %.224, 3
  %19 = lshr i32 %.224, 2
  %20 = add nsw i32 %.2, -1
  %.not = icmp slt i32 %18, %13
  br i1 %.not, label %.loopexit, label %.preheader, !llvm.loop !5

.loopexit:                                        ; preds = %17, %.lr.ph
  %.026 = phi i32 [ 0, %.lr.ph ], [ %18, %17 ]
  %.325 = phi i32 [ %.02231, %.lr.ph ], [ %19, %17 ]
  %.3 = phi i32 [ %.02132, %.lr.ph ], [ %20, %17 ]
  %21 = zext nneg i32 %.026 to i64
  %22 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !7
  %24 = sext i8 %23 to i64
  %25 = call i64 @fsm_advance(ptr noundef %2, i64 %.sroa.013.033, i64 noundef %24)
  %26 = load i8, ptr %22, align 1, !tbaa !7
  %27 = sext i8 %26 to i64
  call void @move(ptr noundef %0, i64 noundef %27)
  %28 = add nsw i32 %.034, -1
  %29 = icmp sgt i32 %.034, 1
  br i1 %29, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %.loopexit, %8
  %.027 = phi i32 [ 1, %8 ], [ 1, %.loopexit ], [ %13, %.lr.ph ]
  %30 = call i32 @pthread_mutex_unlock(ptr noundef nonnull @seed_lock) #6
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %33, label %32

32:                                               ; preds = %._crit_edge
  call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 306, ptr noundef nonnull @__PRETTY_FUNCTION__.random_walk) #7
  unreachable

33:                                               ; preds = %._crit_edge
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #6
  ret i32 %.027
}

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

declare i32 @fsm_get_moves_moribund(ptr noundef, i64, ptr noundef, i32 noundef) local_unnamed_addr #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @fsm_advance(ptr noundef, i64, i64 noundef) local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @move(ptr noundef, i64 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { noreturn 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 #3 = { 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 = { 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 #5 = { "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 #6 = { nounwind }
attributes #7 = { noreturn 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !6}


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