; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (p_setup)

; 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.mapthing_t = type { i16, i16, i16, i16, i16 }

@gamemode = external global i32, align 4

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

declare i32 @W_LumpLength(i32 noundef) #1

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) #1

declare void @Z_Free(ptr noundef) #1

; 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 @P_LoadThings(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = call ptr @W_CacheLumpNum(i32 noundef %8, i32 noundef 1)
  store ptr %9, ptr %3, align 8, !tbaa !9
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = call i32 @W_LumpLength(i32 noundef %10)
  %12 = sext i32 %11 to i64
  %13 = udiv i64 %12, 10
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr %6, align 4, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !9
  store ptr %15, ptr %5, align 8, !tbaa !9
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %61, %1
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %20, label %66

20:                                               ; preds = %16
  store i32 1, ptr %7, align 4, !tbaa !5
  %21 = load i32, ptr @gamemode, align 4, !tbaa !5
  %22 = icmp ne i32 %21, 2
  br i1 %22, label %23, label %30

23:                                               ; preds = %20
  %24 = load ptr, ptr %5, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.mapthing_t, ptr %24, i32 0, i32 3
  %26 = load i16, ptr %25, align 2, !tbaa !11
  %27 = sext i16 %26 to i32
  switch i32 %27, label %29 [
    i32 68, label %28
    i32 64, label %28
    i32 88, label %28
    i32 89, label %28
    i32 69, label %28
    i32 67, label %28
    i32 71, label %28
    i32 65, label %28
    i32 66, label %28
    i32 84, label %28
  ]

28:                                               ; preds = %23, %23, %23, %23, %23, %23, %23, %23, %23, %23
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %28, %23
  br label %30

30:                                               ; preds = %29, %20
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %34

33:                                               ; preds = %30
  br label %66

34:                                               ; preds = %30
  %35 = load ptr, ptr %5, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.mapthing_t, ptr %35, i32 0, i32 0
  %37 = load i16, ptr %36, align 2, !tbaa !14
  %38 = load ptr, ptr %5, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.mapthing_t, ptr %38, i32 0, i32 0
  store i16 %37, ptr %39, align 2, !tbaa !14
  %40 = load ptr, ptr %5, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.mapthing_t, ptr %40, i32 0, i32 1
  %42 = load i16, ptr %41, align 2, !tbaa !15
  %43 = load ptr, ptr %5, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.mapthing_t, ptr %43, i32 0, i32 1
  store i16 %42, ptr %44, align 2, !tbaa !15
  %45 = load ptr, ptr %5, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.mapthing_t, ptr %45, i32 0, i32 2
  %47 = load i16, ptr %46, align 2, !tbaa !16
  %48 = load ptr, ptr %5, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.mapthing_t, ptr %48, i32 0, i32 2
  store i16 %47, ptr %49, align 2, !tbaa !16
  %50 = load ptr, ptr %5, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.mapthing_t, ptr %50, i32 0, i32 3
  %52 = load i16, ptr %51, align 2, !tbaa !11
  %53 = load ptr, ptr %5, align 8, !tbaa !9
  %54 = getelementptr inbounds %struct.mapthing_t, ptr %53, i32 0, i32 3
  store i16 %52, ptr %54, align 2, !tbaa !11
  %55 = load ptr, ptr %5, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.mapthing_t, ptr %55, i32 0, i32 4
  %57 = load i16, ptr %56, align 2, !tbaa !17
  %58 = load ptr, ptr %5, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.mapthing_t, ptr %58, i32 0, i32 4
  store i16 %57, ptr %59, align 2, !tbaa !17
  %60 = load ptr, ptr %5, align 8, !tbaa !9
  call void @P_SpawnMapThing(ptr noundef %60)
  br label %61

61:                                               ; preds = %34
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %4, align 4, !tbaa !5
  %64 = load ptr, ptr %5, align 8, !tbaa !9
  %65 = getelementptr inbounds %struct.mapthing_t, ptr %64, i32 1
  store ptr %65, ptr %5, align 8, !tbaa !9
  br label %16, !llvm.loop !18

66:                                               ; preds = %33, %16
  %67 = load ptr, ptr %3, align 8, !tbaa !9
  call void @Z_Free(ptr noundef %67)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

declare void @P_SpawnMapThing(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !13, i64 6}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!12, !13, i64 2}
!16 = !{!12, !13, i64 4}
!17 = !{!12, !13, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@gamemode = external local_unnamed_addr global i32, align 4

declare i32 @W_LumpLength(i32 noundef) local_unnamed_addr #0

declare ptr @W_CacheLumpNum(i32 noundef, i32 noundef) local_unnamed_addr #0

declare void @Z_Free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadThings(i32 noundef %0) local_unnamed_addr #1 {
  %2 = tail call ptr @W_CacheLumpNum(i32 noundef %0, i32 noundef 1) #2
  %3 = tail call i32 @W_LumpLength(i32 noundef %0) #2
  %4 = sext i32 %3 to i64
  %5 = udiv i64 %4, 10
  %6 = trunc i64 %5 to i32
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1, %.critedge
  %.02023 = phi i32 [ %12, %.critedge ], [ 0, %1 ]
  %.02122 = phi ptr [ %13, %.critedge ], [ %2, %1 ]
  %8 = load i32, ptr @gamemode, align 4, !tbaa !5
  %.not = icmp eq i32 %8, 2
  br i1 %.not, label %.critedge, label %9

9:                                                ; preds = %.lr.ph
  %10 = getelementptr inbounds i8, ptr %.02122, i64 6
  %11 = load i16, ptr %10, align 2, !tbaa !9
  switch i16 %11, label %.critedge [
    i16 68, label %._crit_edge
    i16 64, label %._crit_edge
    i16 88, label %._crit_edge
    i16 89, label %._crit_edge
    i16 69, label %._crit_edge
    i16 67, label %._crit_edge
    i16 71, label %._crit_edge
    i16 65, label %._crit_edge
    i16 66, label %._crit_edge
    i16 84, label %._crit_edge
  ]

.critedge:                                        ; preds = %.lr.ph, %9
  tail call void @P_SpawnMapThing(ptr noundef %.02122) #2
  %12 = add nuw nsw i32 %.02023, 1
  %13 = getelementptr inbounds i8, ptr %.02122, i64 10
  %exitcond.not = icmp eq i32 %12, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.critedge, %9, %9, %9, %9, %9, %9, %9, %9, %9, %9, %1
  tail call void @Z_Free(ptr noundef %2) #2
  ret void
}

declare void @P_SpawnMapThing(ptr noundef) local_unnamed_addr #0

attributes #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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 6}
!10 = !{!"", !11, i64 0, !11, i64 2, !11, i64 4, !11, i64 6, !11, i64 8}
!11 = !{!"short", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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