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

; 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.index_aux = type { [32 x i8], [16 x i8], i32, i32, i32, i32, i32, ptr, [16 x i8] }
%struct.index = type { i32, i32, i32 }
%struct.index_table = type { [25 x i8], i8, i32 }

@factorials = external constant [13 x i32], align 16
@combination_count = external constant [26 x i32], align 16

; 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 uwtable
declare dso_local i64 @random64() #1

; Function Attrs: nounwind uwtable
define dso_local void @random_index(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %7 = call i64 @random64()
  store i64 %7, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.index_aux, ptr %8, i32 0, i32 6
  %10 = load i32, ptr %9, align 32, !tbaa !11
  %11 = call i32 @tileset_remove(i32 noundef %10, i32 noundef 0)
  store i32 %11, ptr %6, align 4, !tbaa !14
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !14
  %14 = call i32 @tileset_count(i32 noundef %13)
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [13 x i32], ptr @factorials, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !14
  %18 = zext i32 %17 to i64
  %19 = urem i64 %12, %18
  %20 = trunc i64 %19 to i32
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.index, ptr %21, i32 0, i32 0
  store i32 %20, ptr %22, align 4, !tbaa !15
  %23 = load i32, ptr %6, align 4, !tbaa !14
  %24 = call i32 @tileset_count(i32 noundef %23)
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds [13 x i32], ptr @factorials, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = zext i32 %27 to i64
  %29 = load i64, ptr %5, align 8, !tbaa !9
  %30 = udiv i64 %29, %28
  store i64 %30, ptr %5, align 8, !tbaa !9
  %31 = load i64, ptr %5, align 8, !tbaa !9
  %32 = load i32, ptr %6, align 4, !tbaa !14
  %33 = call i32 @tileset_count(i32 noundef %32)
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds [26 x i32], ptr @combination_count, i64 0, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !14
  %37 = zext i32 %36 to i64
  %38 = urem i64 %31, %37
  %39 = trunc i64 %38 to i32
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.index, ptr %40, i32 0, i32 1
  store i32 %39, ptr %41, align 4, !tbaa !17
  %42 = load i32, ptr %6, align 4, !tbaa !14
  %43 = call i32 @tileset_count(i32 noundef %42)
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds [26 x i32], ptr @combination_count, i64 0, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !14
  %47 = zext i32 %46 to i64
  %48 = load i64, ptr %5, align 8, !tbaa !9
  %49 = udiv i64 %48, %47
  store i64 %49, ptr %5, align 8, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.index_aux, ptr %50, i32 0, i32 6
  %52 = load i32, ptr %51, align 32, !tbaa !11
  %53 = call i32 @tileset_has(i32 noundef %52, i32 noundef 0)
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %72

55:                                               ; preds = %2
  %56 = load i64, ptr %5, align 8, !tbaa !9
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.index_aux, ptr %57, i32 0, i32 7
  %59 = load ptr, ptr %58, align 8, !tbaa !18
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.index, ptr %60, i32 0, i32 1
  %62 = load i32, ptr %61, align 4, !tbaa !17
  %63 = zext i32 %62 to i64
  %64 = getelementptr inbounds %struct.index_table, ptr %59, i64 %63
  %65 = getelementptr inbounds %struct.index_table, ptr %64, i32 0, i32 1
  %66 = load i8, ptr %65, align 1, !tbaa !19
  %67 = zext i8 %66 to i64
  %68 = urem i64 %56, %67
  %69 = trunc i64 %68 to i32
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.index, ptr %70, i32 0, i32 2
  store i32 %69, ptr %71, align 4, !tbaa !21
  br label %75

72:                                               ; preds = %2
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.index, ptr %73, i32 0, i32 2
  store i32 -1, ptr %74, align 4, !tbaa !21
  br label %75

75:                                               ; preds = %72, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @tileset_remove(i32 noundef, i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @tileset_count(i32 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @tileset_has(i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"long long", !7, i64 0}
!11 = !{!12, !13, i64 64}
!12 = !{!"index_aux", !7, i64 0, !7, i64 32, !13, i64 48, !13, i64 52, !13, i64 56, !13, i64 60, !13, i64 64, !6, i64 72}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!16, !13, i64 0}
!16 = !{!"index", !13, i64 0, !13, i64 4, !13, i64 8}
!17 = !{!16, !13, i64 4}
!18 = !{!12, !6, i64 72}
!19 = !{!20, !7, i64 25}
!20 = !{!"index_table", !7, i64 0, !7, i64 25, !13, i64 28}
!21 = !{!16, !13, i64 8}


; 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.index_table = type { [25 x i8], i8, i32 }

@factorials = external local_unnamed_addr constant [13 x i32], align 16
@combination_count = external local_unnamed_addr constant [26 x i32], align 16

; Function Attrs: nounwind uwtable
declare dso_local i64 @random64() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @random_index(ptr nocapture noundef readonly %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = tail call i64 @random64()
  %4 = getelementptr inbounds i8, ptr %0, i64 64
  %5 = load i32, ptr %4, align 32, !tbaa !5
  %6 = tail call i32 @tileset_remove(i32 noundef %5, i32 noundef 0)
  %7 = tail call i32 @tileset_count(i32 noundef %6)
  %8 = zext i32 %7 to i64
  %9 = getelementptr inbounds [13 x i32], ptr @factorials, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = zext i32 %10 to i64
  %12 = urem i64 %3, %11
  %13 = trunc nuw i64 %12 to i32
  store i32 %13, ptr %1, align 4, !tbaa !12
  %14 = tail call i32 @tileset_count(i32 noundef %6)
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [13 x i32], ptr @factorials, i64 0, i64 %15
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = zext i32 %17 to i64
  %19 = udiv i64 %3, %18
  %20 = tail call i32 @tileset_count(i32 noundef %6)
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds [26 x i32], ptr @combination_count, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = zext i32 %23 to i64
  %25 = urem i64 %19, %24
  %26 = trunc nuw i64 %25 to i32
  %27 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 %26, ptr %27, align 4, !tbaa !14
  %28 = tail call i32 @tileset_count(i32 noundef %6)
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds [26 x i32], ptr @combination_count, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = load i32, ptr %4, align 32, !tbaa !5
  %33 = tail call i32 @tileset_has(i32 noundef %32, i32 noundef 0)
  %.not = icmp eq i32 %33, 0
  br i1 %.not, label %46, label %34

34:                                               ; preds = %2
  %35 = zext i32 %31 to i64
  %36 = udiv i64 %19, %35
  %37 = getelementptr inbounds i8, ptr %0, i64 72
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  %39 = load i32, ptr %27, align 4, !tbaa !14
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds %struct.index_table, ptr %38, i64 %40, i32 1
  %42 = load i8, ptr %41, align 1, !tbaa !16
  %43 = zext i8 %42 to i64
  %44 = urem i64 %36, %43
  %45 = trunc nuw nsw i64 %44 to i32
  br label %46

46:                                               ; preds = %2, %34
  %.sink = phi i32 [ %45, %34 ], [ -1, %2 ]
  %47 = getelementptr inbounds i8, ptr %1, i64 8
  store i32 %.sink, ptr %47, align 4
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @tileset_remove(i32 noundef, i32 noundef) local_unnamed_addr #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @tileset_has(i32 noundef, i32 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, !9, i64 64}
!6 = !{!"index_aux", !7, i64 0, !7, i64 32, !9, i64 48, !9, i64 52, !9, i64 56, !9, i64 60, !9, i64 64, !10, i64 72}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!9, !9, i64 0}
!12 = !{!13, !9, i64 0}
!13 = !{!"index", !9, i64 0, !9, i64 4, !9, i64 8}
!14 = !{!13, !9, i64 4}
!15 = !{!6, !10, i64 72}
!16 = !{!17, !7, i64 25}
!17 = !{!"index_table", !7, i64 0, !7, i64 25, !9, i64 28}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass