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

; 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_Map = type { i32, i32, i32, ptr }
%struct.TCOD_MapCell = 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: nounwind uwtable
define dso_local void @TCOD_map_clear(ptr noundef %0, i1 noundef zeroext %1, i1 noundef zeroext %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  %9 = zext i1 %1 to i8
  store i8 %9, ptr %5, align 1, !tbaa !9
  %10 = zext i1 %2 to i8
  store i8 %10, ptr %6, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %3
  store i32 1, ptr %8, align 4
  br label %53

14:                                               ; preds = %3
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %15

15:                                               ; preds = %49, %14
  %16 = load i32, ptr %7, align 4, !tbaa !11
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.TCOD_Map, ptr %17, i32 0, i32 2
  %19 = load i32, ptr %18, align 8, !tbaa !13
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %21, label %52

21:                                               ; preds = %15
  %22 = load i8, ptr %5, align 1, !tbaa !9, !range !15, !noundef !16
  %23 = trunc i8 %22 to i1
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.TCOD_Map, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %25, align 8, !tbaa !17
  %27 = load i32, ptr %7, align 4, !tbaa !11
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds %struct.TCOD_MapCell, ptr %26, i64 %28
  %30 = getelementptr inbounds %struct.TCOD_MapCell, ptr %29, i32 0, i32 0
  %31 = zext i1 %23 to i8
  store i8 %31, ptr %30, align 1, !tbaa !18
  %32 = load i8, ptr %6, align 1, !tbaa !9, !range !15, !noundef !16
  %33 = trunc i8 %32 to i1
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.TCOD_Map, ptr %34, i32 0, i32 3
  %36 = load ptr, ptr %35, align 8, !tbaa !17
  %37 = load i32, ptr %7, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %struct.TCOD_MapCell, ptr %36, i64 %38
  %40 = getelementptr inbounds %struct.TCOD_MapCell, ptr %39, i32 0, i32 1
  %41 = zext i1 %33 to i8
  store i8 %41, ptr %40, align 1, !tbaa !20
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.TCOD_Map, ptr %42, i32 0, i32 3
  %44 = load ptr, ptr %43, align 8, !tbaa !17
  %45 = load i32, ptr %7, align 4, !tbaa !11
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds %struct.TCOD_MapCell, ptr %44, i64 %46
  %48 = getelementptr inbounds %struct.TCOD_MapCell, ptr %47, i32 0, i32 2
  store i8 0, ptr %48, align 1, !tbaa !21
  br label %49

49:                                               ; preds = %21
  %50 = load i32, ptr %7, align 4, !tbaa !11
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %7, align 4, !tbaa !11
  br label %15, !llvm.loop !22

52:                                               ; preds = %15
  store i32 0, ptr %8, align 4
  br label %53

53:                                               ; preds = %52, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %54 = load i32, ptr %8, align 4
  switch i32 %54, label %56 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  ret void

56:                                               ; preds = %53
  unreachable
}

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 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 8}
!14 = !{!"TCOD_Map", !12, i64 0, !12, i64 4, !12, i64 8, !6, i64 16}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!14, !6, i64 16}
!18 = !{!19, !10, i64 0}
!19 = !{!"TCOD_MapCell", !10, i64 0, !10, i64 1, !10, i64 2}
!20 = !{!19, !10, i64 1}
!21 = !{!19, !10, i64 2}
!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"

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

; Function Attrs: nofree norecurse nosync nounwind memory(write, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @TCOD_map_clear(ptr noundef readonly %0, i1 noundef zeroext %1, i1 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = zext i1 %1 to i8
  %5 = zext i1 %2 to i8
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %wide.trip.count = zext nneg i32 %7 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %11 = icmp eq i32 %7, 1
  br i1 %11, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.new

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

12:                                               ; preds = %12, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %12 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %12 ]
  %13 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv
  store i8 %4, ptr %13, align 1, !tbaa !12
  %14 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv, i32 1
  store i8 %5, ptr %14, align 1, !tbaa !15
  %15 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv, i32 2
  store i8 0, ptr %15, align 1, !tbaa !16
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %16 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv.next
  store i8 %4, ptr %16, align 1, !tbaa !12
  %17 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv.next, i32 1
  store i8 %5, ptr %17, align 1, !tbaa !15
  %18 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv.next, i32 2
  store i8 0, ptr %18, align 1, !tbaa !16
  %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 %12, !llvm.loop !17

.loopexit.loopexit.unr-lcssa:                     ; preds = %12, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %12 ]
  %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
  %19 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv.unr
  store i8 %4, ptr %19, align 1, !tbaa !12
  %20 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv.unr, i32 1
  store i8 %5, ptr %20, align 1, !tbaa !15
  %21 = getelementptr inbounds %struct.TCOD_MapCell, ptr %10, i64 %indvars.iv.unr, i32 2
  store i8 0, ptr %21, align 1, !tbaa !16
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit.epilog-lcssa, %.loopexit.loopexit.unr-lcssa, %.preheader, %3
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(write, argmem: readwrite, inaccessiblemem: none) 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 8}
!6 = !{!"TCOD_Map", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!13, !14, i64 0}
!13 = !{!"TCOD_MapCell", !14, i64 0, !14, i64 1, !14, i64 2}
!14 = !{!"_Bool", !8, i64 0}
!15 = !{!13, !14, i64 1}
!16 = !{!13, !14, i64 2}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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