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

@blockmaplump = external dso_local global ptr, align 8
@blockmap = external dso_local global ptr, align 8
@bmaporgx = external dso_local global i32, align 4
@bmaporgy = external dso_local global i32, align 4
@bmapwidth = external dso_local global i32, align 4
@bmapheight = external dso_local global i32, align 4
@blocklinks = external dso_local global ptr, align 8

; 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 @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) #1

declare ptr @W_CacheLumpNum(i32 noundef, i32 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadBlockMap(i32 noundef %0) #3 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = call ptr @W_CacheLumpNum(i32 noundef %5, i32 noundef 50)
  store ptr %6, ptr @blockmaplump, align 8, !tbaa !9
  %7 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %8 = getelementptr inbounds i16, ptr %7, i64 4
  store ptr %8, ptr @blockmap, align 8, !tbaa !9
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = call i32 @W_LumpLength(i32 noundef %9)
  %11 = sdiv i32 %10, 2
  store i32 %11, ptr %4, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %26, %1
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %29

16:                                               ; preds = %12
  %17 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds i16, ptr %17, i64 %19
  %21 = load i16, ptr %20, align 2, !tbaa !11
  %22 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i16, ptr %22, i64 %24
  store i16 %21, ptr %25, align 2, !tbaa !11
  br label %26

26:                                               ; preds = %16
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %3, align 4, !tbaa !5
  br label %12, !llvm.loop !13

29:                                               ; preds = %12
  %30 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %31 = getelementptr inbounds i16, ptr %30, i64 0
  %32 = load i16, ptr %31, align 2, !tbaa !11
  %33 = sext i16 %32 to i32
  %34 = shl i32 %33, 16
  store i32 %34, ptr @bmaporgx, align 4, !tbaa !5
  %35 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %36 = getelementptr inbounds i16, ptr %35, i64 1
  %37 = load i16, ptr %36, align 2, !tbaa !11
  %38 = sext i16 %37 to i32
  %39 = shl i32 %38, 16
  store i32 %39, ptr @bmaporgy, align 4, !tbaa !5
  %40 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %41 = getelementptr inbounds i16, ptr %40, i64 2
  %42 = load i16, ptr %41, align 2, !tbaa !11
  %43 = sext i16 %42 to i32
  store i32 %43, ptr @bmapwidth, align 4, !tbaa !5
  %44 = load ptr, ptr @blockmaplump, align 8, !tbaa !9
  %45 = getelementptr inbounds i16, ptr %44, i64 3
  %46 = load i16, ptr %45, align 2, !tbaa !11
  %47 = sext i16 %46 to i32
  store i32 %47, ptr @bmapheight, align 4, !tbaa !5
  %48 = load i32, ptr @bmapwidth, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = mul i64 8, %49
  %51 = load i32, ptr @bmapheight, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = mul i64 %50, %52
  %54 = trunc i64 %53 to i32
  store i32 %54, ptr %4, align 4, !tbaa !5
  %55 = load i32, ptr %4, align 4, !tbaa !5
  %56 = call ptr @Z_Malloc(i32 noundef %55, i32 noundef 50, ptr noundef null)
  store ptr %56, ptr @blocklinks, align 8, !tbaa !9
  %57 = load ptr, ptr @blocklinks, align 8, !tbaa !9
  %58 = load i32, ptr %4, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  call void @llvm.memset.p0.i64(ptr align 8 %57, i8 0, i64 %59, i1 false)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret void
}

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { 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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@blockmaplump = external dso_local local_unnamed_addr global ptr, align 8
@blockmap = external dso_local local_unnamed_addr global ptr, align 8
@bmaporgx = external dso_local local_unnamed_addr global i32, align 4
@bmaporgy = external dso_local local_unnamed_addr global i32, align 4
@bmapwidth = external dso_local local_unnamed_addr global i32, align 4
@bmapheight = external dso_local local_unnamed_addr global i32, align 4
@blocklinks = external dso_local local_unnamed_addr global ptr, align 8

declare i32 @W_LumpLength(i32 noundef) local_unnamed_addr #0

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @P_LoadBlockMap(i32 noundef %0) local_unnamed_addr #2 {
  %2 = tail call ptr @W_CacheLumpNum(i32 noundef %0, i32 noundef 50) #3
  store ptr %2, ptr @blockmaplump, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %3, ptr @blockmap, align 8, !tbaa !5
  %4 = tail call i32 @W_LumpLength(i32 noundef %0) #3
  %5 = load ptr, ptr @blockmaplump, align 8, !tbaa !5
  %6 = load i16, ptr %5, align 2, !tbaa !9
  %7 = sext i16 %6 to i32
  %8 = shl nsw i32 %7, 16
  store i32 %8, ptr @bmaporgx, align 4, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %5, i64 2
  %10 = load i16, ptr %9, align 2, !tbaa !9
  %11 = sext i16 %10 to i32
  %12 = shl nsw i32 %11, 16
  store i32 %12, ptr @bmaporgy, align 4, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %5, i64 4
  %14 = load i16, ptr %13, align 2, !tbaa !9
  %15 = sext i16 %14 to i32
  store i32 %15, ptr @bmapwidth, align 4, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %5, i64 6
  %17 = load i16, ptr %16, align 2, !tbaa !9
  %18 = sext i16 %17 to i32
  store i32 %18, ptr @bmapheight, align 4, !tbaa !11
  %19 = shl nsw i32 %15, 3
  %20 = mul i32 %19, %18
  %21 = tail call ptr @Z_Malloc(i32 noundef %20, i32 noundef 50, ptr noundef null) #3
  store ptr %21, ptr @blocklinks, align 8, !tbaa !5
  %22 = sext i32 %20 to i64
  tail call void @llvm.memset.p0.i64(ptr align 8 %21, i8 0, i64 %22, i1 false)
  ret void
}

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass