; LLVM IR from libtcod/libtcod :: src/libtcod/color_.ll (ZN9TCODColor6genMapEPS_iPKS_PKi)

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

%class.TCODColor = type { i8, i8, i8 }

; 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: 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) #1

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

15:                                               ; preds = %67, %4
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = sub nsw i32 %17, 1
  %19 = icmp slt i32 %16, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %70

21:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  store i32 %26, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = add nsw i32 %28, 1
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %27, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  store i32 %32, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %33 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %33, ptr %13, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %63, %21
  %35 = load i32, ptr %13, align 4, !tbaa !9
  %36 = load i32, ptr %12, align 4, !tbaa !9
  %37 = icmp sle i32 %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %66

39:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 3, ptr %14) #3
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds %class.TCODColor, ptr %40, i64 %42
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %class.TCODColor, ptr %44, i64 %47
  %49 = load i32, ptr %13, align 4, !tbaa !9
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = sub nsw i32 %49, %50
  %52 = sitofp i32 %51 to float
  %53 = load i32, ptr %12, align 4, !tbaa !9
  %54 = load i32, ptr %11, align 4, !tbaa !9
  %55 = sub nsw i32 %53, %54
  %56 = sitofp i32 %55 to float
  %57 = fdiv float %52, %56
  %58 = call i24 @_ZN9TCODColor4lerpERKS_S1_f(ptr noundef nonnull align 1 dereferenceable(3) %43, ptr noundef nonnull align 1 dereferenceable(3) %48, float noundef %57) #3
  store i24 %58, ptr %14, align 1
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = load i32, ptr %13, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds %class.TCODColor, ptr %59, i64 %61
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %62, ptr align 1 %14, i64 3, i1 false), !tbaa.struct !11
  call void @llvm.lifetime.end.p0(i64 3, ptr %14) #3
  br label %63

63:                                               ; preds = %39
  %64 = load i32, ptr %13, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %13, align 4, !tbaa !9
  br label %34, !llvm.loop !13

66:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %9, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %9, align 4, !tbaa !9
  br label %15, !llvm.loop !15

70:                                               ; preds = %20
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i24 @_ZN9TCODColor4lerpERKS_S1_f(ptr noundef nonnull align 1 dereferenceable(3), ptr noundef nonnull align 1 dereferenceable(3), float noundef) #2 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{i64 0, i64 1, !12, i64 1, i64 1, !12, i64 2, i64 1, !12}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


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

%class.TCODColor = type { i8, i8, i8 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN9TCODColor6genMapEPS_iPKS_PKi(ptr nocapture noundef writeonly %0, i32 noundef %1, ptr noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 align 2 {
  %5 = icmp sgt i32 %1, 1
  br i1 %5, label %.lr.ph25.preheader, label %._crit_edge

.lr.ph25.preheader:                               ; preds = %4
  %6 = add nsw i32 %1, -1
  %wide.trip.count = zext nneg i32 %6 to i64
  br label %.lr.ph25

.loopexit:                                        ; preds = %17, %.lr.ph25
  %exitcond30.not = icmp eq i64 %indvars.iv.next28, %wide.trip.count
  br i1 %exitcond30.not, label %._crit_edge, label %.lr.ph25, !llvm.loop !5

._crit_edge:                                      ; preds = %.loopexit, %4
  ret void

.lr.ph25:                                         ; preds = %.lr.ph25.preheader, %.loopexit
  %indvars.iv27 = phi i64 [ 0, %.lr.ph25.preheader ], [ %indvars.iv.next28, %.loopexit ]
  %7 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv27
  %8 = load i32, ptr %7, align 4, !tbaa !7
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %9 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv.next28
  %10 = load i32, ptr %9, align 4, !tbaa !7
  %.not22 = icmp sgt i32 %8, %10
  br i1 %.not22, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph25
  %11 = getelementptr inbounds %class.TCODColor, ptr %2, i64 %indvars.iv27
  %12 = getelementptr inbounds %class.TCODColor, ptr %2, i64 %indvars.iv.next28
  %13 = sub nsw i32 %10, %8
  %14 = sitofp i32 %13 to float
  %15 = sext i32 %8 to i64
  %16 = add i32 %10, 1
  br label %17

17:                                               ; preds = %.lr.ph, %17
  %indvars.iv = phi i64 [ %15, %.lr.ph ], [ %indvars.iv.next, %17 ]
  %18 = trunc i64 %indvars.iv to i32
  %19 = sub i32 %18, %8
  %20 = sitofp i32 %19 to float
  %21 = fdiv float %20, %14
  %22 = tail call i24 @_ZN9TCODColor4lerpERKS_S1_f(ptr noundef nonnull align 1 dereferenceable(3) %11, ptr noundef nonnull align 1 dereferenceable(3) %12, float noundef %21) #1
  %23 = getelementptr inbounds %class.TCODColor, ptr %0, i64 %indvars.iv
  store i24 %22, ptr %23, align 1
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %16, %lftr.wideiv
  br i1 %exitcond.not, label %.loopexit, label %17, !llvm.loop !11
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local i24 @_ZN9TCODColor4lerpERKS_S1_f(ptr noundef nonnull align 1 dereferenceable(3), ptr noundef nonnull align 1 dereferenceable(3), float noundef) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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 = { 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 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = distinct !{!11, !6}


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