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

; 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_Pathfinder = type { i8, [4 x i64], i8, i8, i8, %struct.TCOD_ArrayData, %struct.TCOD_BasicGraph2D, %struct.TCOD_ArrayData, %struct.TCOD_Heap }
%struct.TCOD_BasicGraph2D = type { %struct.TCOD_ArrayData, i32, i32 }
%struct.TCOD_ArrayData = type { i8, i32, [5 x i64], [5 x i64], ptr }
%struct.TCOD_Heap = type { ptr, i32, i32, i64, i64, i64, i32 }

; 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
define dso_local void @TCOD_pf_set_distance_pointer(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %13, label %12

12:                                               ; preds = %4
  br label %63

13:                                               ; preds = %4
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %14, i32 0, i32 0
  %16 = load i8, ptr %15, align 8, !tbaa !11
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %17, i32 0, i32 5
  %19 = getelementptr inbounds %struct.TCOD_ArrayData, ptr %18, i32 0, i32 0
  store i8 %16, ptr %19, align 8, !tbaa !18
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %21, i32 0, i32 5
  %23 = getelementptr inbounds %struct.TCOD_ArrayData, ptr %22, i32 0, i32 1
  store i32 %20, ptr %23, align 4, !tbaa !19
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %25, i32 0, i32 5
  %27 = getelementptr inbounds %struct.TCOD_ArrayData, ptr %26, i32 0, i32 4
  store ptr %24, ptr %27, align 8, !tbaa !20
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %60, %13
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %30, i32 0, i32 0
  %32 = load i8, ptr %31, align 8, !tbaa !11
  %33 = sext i8 %32 to i32
  %34 = icmp slt i32 %29, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %63

36:                                               ; preds = %28
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds i64, ptr %37, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !21
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %42, i32 0, i32 5
  %44 = getelementptr inbounds %struct.TCOD_ArrayData, ptr %43, i32 0, i32 3
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [5 x i64], ptr %44, i64 0, i64 %46
  store i64 %41, ptr %47, align 8, !tbaa !21
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %48, i32 0, i32 1
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [4 x i64], ptr %49, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !21
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.TCOD_Pathfinder, ptr %54, i32 0, i32 5
  %56 = getelementptr inbounds %struct.TCOD_ArrayData, ptr %55, i32 0, i32 2
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [5 x i64], ptr %56, i64 0, i64 %58
  store i64 %53, ptr %59, align 8, !tbaa !21
  br label %60

60:                                               ; preds = %36
  %61 = load i32, ptr %9, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %9, align 4, !tbaa !9
  br label %28, !llvm.loop !22

63:                                               ; preds = %35, %12
  ret void
}

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 = { 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 = !{!12, !7, i64 0}
!12 = !{!"TCOD_Pathfinder", !7, i64 0, !7, i64 8, !13, i64 40, !13, i64 41, !13, i64 42, !14, i64 48, !15, i64 144, !14, i64 248, !16, i64 344}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!"TCOD_ArrayData", !7, i64 0, !10, i64 4, !7, i64 8, !7, i64 48, !6, i64 88}
!15 = !{!"TCOD_BasicGraph2D", !14, i64 0, !10, i64 96, !10, i64 100}
!16 = !{!"TCOD_Heap", !6, i64 0, !10, i64 8, !10, i64 12, !17, i64 16, !17, i64 24, !17, i64 32, !10, i64 40}
!17 = !{!"long", !7, i64 0}
!18 = !{!12, !7, i64 48}
!19 = !{!12, !10, i64 52}
!20 = !{!12, !6, i64 136}
!21 = !{!17, !17, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @TCOD_pf_set_distance_pointer(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %5

5:                                                ; preds = %4
  %6 = load i8, ptr %0, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 48
  store i8 %6, ptr %7, align 8, !tbaa !16
  %8 = getelementptr inbounds i8, ptr %0, i64 52
  store i32 %2, ptr %8, align 4, !tbaa !17
  %9 = getelementptr inbounds i8, ptr %0, i64 136
  store ptr %1, ptr %9, align 8, !tbaa !18
  %10 = icmp sgt i8 %6, 0
  br i1 %10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5
  %wide.trip.count = zext nneg i8 %6 to i64
  %11 = getelementptr inbounds i8, ptr %0, i64 96
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = getelementptr inbounds i8, ptr %0, i64 56
  %xtraiter = and i64 %wide.trip.count, 1
  %14 = icmp eq i8 %6, 1
  br i1 %14, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 126
  br label %15

15:                                               ; preds = %15, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %15 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %15 ]
  %16 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv
  %17 = load i64, ptr %16, align 8, !tbaa !19
  %18 = getelementptr inbounds [5 x i64], ptr %11, i64 0, i64 %indvars.iv
  store i64 %17, ptr %18, align 8, !tbaa !19
  %19 = getelementptr inbounds [4 x i64], ptr %12, i64 0, i64 %indvars.iv
  %20 = load i64, ptr %19, align 8, !tbaa !19
  %21 = getelementptr inbounds [5 x i64], ptr %13, i64 0, i64 %indvars.iv
  store i64 %20, ptr %21, align 8, !tbaa !19
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %22 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv.next
  %23 = load i64, ptr %22, align 8, !tbaa !19
  %24 = getelementptr inbounds [5 x i64], ptr %11, i64 0, i64 %indvars.iv.next
  store i64 %23, ptr %24, align 8, !tbaa !19
  %25 = getelementptr inbounds [4 x i64], ptr %12, i64 0, i64 %indvars.iv.next
  %26 = load i64, ptr %25, align 8, !tbaa !19
  %27 = getelementptr inbounds [5 x i64], ptr %13, i64 0, i64 %indvars.iv.next
  store i64 %26, ptr %27, align 8, !tbaa !19
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %15, !llvm.loop !20

.loopexit.loopexit.unr-lcssa:                     ; preds = %15, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %15 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.loopexit.loopexit.epilog-lcssa

.loopexit.loopexit.epilog-lcssa:                  ; preds = %.loopexit.loopexit.unr-lcssa
  %28 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv.unr
  %29 = load i64, ptr %28, align 8, !tbaa !19
  %30 = getelementptr inbounds [5 x i64], ptr %11, i64 0, i64 %indvars.iv.unr
  store i64 %29, ptr %30, align 8, !tbaa !19
  %31 = getelementptr inbounds [4 x i64], ptr %12, i64 0, i64 %indvars.iv.unr
  %32 = load i64, ptr %31, align 8, !tbaa !19
  %33 = getelementptr inbounds [5 x i64], ptr %13, i64 0, i64 %indvars.iv.unr
  store i64 %32, ptr %33, align 8, !tbaa !19
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit.epilog-lcssa, %.loopexit.loopexit.unr-lcssa, %5, %4
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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, !7, i64 0}
!6 = !{!"TCOD_Pathfinder", !7, i64 0, !7, i64 8, !9, i64 40, !9, i64 41, !9, i64 42, !10, i64 48, !13, i64 144, !10, i64 248, !14, i64 344}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"_Bool", !7, i64 0}
!10 = !{!"TCOD_ArrayData", !7, i64 0, !11, i64 4, !7, i64 8, !7, i64 48, !12, i64 88}
!11 = !{!"int", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"TCOD_BasicGraph2D", !10, i64 0, !11, i64 96, !11, i64 100}
!14 = !{!"TCOD_Heap", !12, i64 0, !11, i64 8, !11, i64 12, !15, i64 16, !15, i64 24, !15, i64 32, !11, i64 40}
!15 = !{!"long", !7, i64 0}
!16 = !{!6, !7, i64 48}
!17 = !{!6, !11, i64 52}
!18 = !{!6, !12, i64 136}
!19 = !{!15, !15, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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