; LLVM IR from libtcod/libtcod :: src/libtcod (color)

; 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.TCOD_ColorRGB = type { i8, i8, i8 }

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

; Function Attrs: nounwind uwtable
declare dso_local i24 @TCOD_color_lerp(i24, i24, float noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @TCOD_color_gen_map(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %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 %struct.TCOD_ColorRGB, align 1
  %14 = alloca i24, align 4
  %15 = alloca i24, align 4
  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
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %16

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

21:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #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 %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #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 %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %33 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %33, ptr %12, align 4, !tbaa !9
  br label %34

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

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

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

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

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

71:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  ret void
}

attributes #0 = { nocallback nofree 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{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"

%struct.TCOD_ColorRGB = type { i8, i8, i8 }

; Function Attrs: nounwind uwtable
declare dso_local i24 @TCOD_color_lerp(i24, i24, float noundef) local_unnamed_addr #0

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

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

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

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

.lr.ph:                                           ; preds = %.lr.ph28
  %11 = getelementptr inbounds %struct.TCOD_ColorRGB, ptr %2, i64 %indvars.iv30
  %12 = getelementptr inbounds %struct.TCOD_ColorRGB, ptr %2, i64 %indvars.iv.next31
  %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 = getelementptr inbounds %struct.TCOD_ColorRGB, ptr %0, i64 %indvars.iv
  %19 = trunc i64 %indvars.iv to i32
  %20 = sub i32 %19, %8
  %21 = sitofp i32 %20 to float
  %22 = fdiv float %21, %14
  %.0.copyload1 = load i24, ptr %11, align 1
  %.0.copyload = load i24, ptr %12, align 1
  %23 = tail call i24 @TCOD_color_lerp(i24 %.0.copyload1, i24 %.0.copyload, float noundef %22)
  store i24 %23, ptr %18, 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

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

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

!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/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