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

; 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.patch_t = type { i16, i16, i16, i16, [8 x i32] }

@.str.6 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [2 x i8], align 1
@firstspritelump = external dso_local global i32, align 4
@lastspritelump = external dso_local global i32, align 4
@numspritelumps = external dso_local global i32, align 4
@spritewidth = external dso_local global ptr, align 8
@spriteoffset = external dso_local global ptr, align 8
@spritetopoffset = 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

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

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

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

declare i32 @printf(ptr noundef, ...) #1

declare i32 @W_GetNumForName(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @R_InitSpriteLumps() #2 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %3 = call i32 @W_GetNumForName(ptr noundef @.str.6)
  %4 = add nsw i32 %3, 1
  store i32 %4, ptr @firstspritelump, align 4, !tbaa !5
  %5 = call i32 @W_GetNumForName(ptr noundef @.str.7)
  %6 = sub nsw i32 %5, 1
  store i32 %6, ptr @lastspritelump, align 4, !tbaa !5
  %7 = load i32, ptr @lastspritelump, align 4, !tbaa !5
  %8 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %9 = sub nsw i32 %7, %8
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @numspritelumps, align 4, !tbaa !5
  %11 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = mul i64 %12, 4
  %14 = trunc i64 %13 to i32
  %15 = call ptr @Z_Malloc(i32 noundef %14, i32 noundef 1, ptr noundef null)
  store ptr %15, ptr @spritewidth, align 8, !tbaa !9
  %16 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = mul i64 %17, 4
  %19 = trunc i64 %18 to i32
  %20 = call ptr @Z_Malloc(i32 noundef %19, i32 noundef 1, ptr noundef null)
  store ptr %20, ptr @spriteoffset, align 8, !tbaa !9
  %21 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = mul i64 %22, 4
  %24 = trunc i64 %23 to i32
  %25 = call ptr @Z_Malloc(i32 noundef %24, i32 noundef 1, ptr noundef null)
  store ptr %25, ptr @spritetopoffset, align 8, !tbaa !9
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %68, %0
  %27 = load i32, ptr %1, align 4, !tbaa !5
  %28 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %30, label %71

30:                                               ; preds = %26
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = and i32 %31, 63
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %36, label %34

34:                                               ; preds = %30
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.13)
  br label %36

36:                                               ; preds = %34, %30
  %37 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %38 = load i32, ptr %1, align 4, !tbaa !5
  %39 = add nsw i32 %37, %38
  %40 = call ptr @W_CacheLumpNum(i32 noundef %39, i32 noundef 101)
  store ptr %40, ptr %2, align 8, !tbaa !9
  %41 = load ptr, ptr %2, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.patch_t, ptr %41, i32 0, i32 0
  %43 = load i16, ptr %42, align 4, !tbaa !11
  %44 = sext i16 %43 to i32
  %45 = shl i32 %44, 16
  %46 = load ptr, ptr @spritewidth, align 8, !tbaa !9
  %47 = load i32, ptr %1, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  store i32 %45, ptr %49, align 4, !tbaa !5
  %50 = load ptr, ptr %2, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.patch_t, ptr %50, i32 0, i32 2
  %52 = load i16, ptr %51, align 4, !tbaa !14
  %53 = sext i16 %52 to i32
  %54 = shl i32 %53, 16
  %55 = load ptr, ptr @spriteoffset, align 8, !tbaa !9
  %56 = load i32, ptr %1, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i32, ptr %55, i64 %57
  store i32 %54, ptr %58, align 4, !tbaa !5
  %59 = load ptr, ptr %2, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.patch_t, ptr %59, i32 0, i32 3
  %61 = load i16, ptr %60, align 2, !tbaa !15
  %62 = sext i16 %61 to i32
  %63 = shl i32 %62, 16
  %64 = load ptr, ptr @spritetopoffset, align 8, !tbaa !9
  %65 = load i32, ptr %1, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds i32, ptr %64, i64 %66
  store i32 %63, ptr %67, align 4, !tbaa !5
  br label %68

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

71:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  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 = { 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 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !7, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!12, !13, i64 6}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@.str.6 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [2 x i8], align 1
@firstspritelump = external dso_local local_unnamed_addr global i32, align 4
@lastspritelump = external dso_local local_unnamed_addr global i32, align 4
@numspritelumps = external dso_local local_unnamed_addr global i32, align 4
@spritewidth = external dso_local local_unnamed_addr global ptr, align 8
@spriteoffset = external dso_local local_unnamed_addr global ptr, align 8
@spritetopoffset = external dso_local local_unnamed_addr global ptr, align 8

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: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

declare i32 @W_GetNumForName(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @R_InitSpriteLumps() local_unnamed_addr #2 {
  %1 = tail call i32 @W_GetNumForName(ptr noundef nonnull @.str.6) #3
  %2 = add nsw i32 %1, 1
  store i32 %2, ptr @firstspritelump, align 4, !tbaa !5
  %3 = tail call i32 @W_GetNumForName(ptr noundef nonnull @.str.7) #3
  %4 = add nsw i32 %3, -1
  store i32 %4, ptr @lastspritelump, align 4, !tbaa !5
  %5 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %6 = sub nsw i32 %4, %5
  %7 = add nsw i32 %6, 1
  store i32 %7, ptr @numspritelumps, align 4, !tbaa !5
  %8 = shl i32 %7, 2
  %9 = tail call ptr @Z_Malloc(i32 noundef %8, i32 noundef 1, ptr noundef null) #3
  store ptr %9, ptr @spritewidth, align 8, !tbaa !9
  %10 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %11 = shl i32 %10, 2
  %12 = tail call ptr @Z_Malloc(i32 noundef %11, i32 noundef 1, ptr noundef null) #3
  store ptr %12, ptr @spriteoffset, align 8, !tbaa !9
  %13 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %14 = shl i32 %13, 2
  %15 = tail call ptr @Z_Malloc(i32 noundef %14, i32 noundef 1, ptr noundef null) #3
  store ptr %15, ptr @spritetopoffset, align 8, !tbaa !9
  %16 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %22
  %indvars.iv = phi i64 [ %indvars.iv.next, %22 ], [ 0, %0 ]
  %18 = trunc nuw nsw i64 %indvars.iv to i32
  %19 = and i32 %18, 63
  %.not = icmp eq i32 %19, 0
  br i1 %.not, label %20, label %22

20:                                               ; preds = %.lr.ph
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.13)
  br label %22

22:                                               ; preds = %20, %.lr.ph
  %23 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %24 = add nsw i32 %23, %18
  %25 = tail call ptr @W_CacheLumpNum(i32 noundef %24, i32 noundef 101) #3
  %26 = load i16, ptr %25, align 4, !tbaa !11
  %27 = sext i16 %26 to i32
  %28 = shl nsw i32 %27, 16
  %29 = load ptr, ptr @spritewidth, align 8, !tbaa !9
  %30 = getelementptr inbounds i32, ptr %29, i64 %indvars.iv
  store i32 %28, ptr %30, align 4, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %25, i64 4
  %32 = load i16, ptr %31, align 4, !tbaa !14
  %33 = sext i16 %32 to i32
  %34 = shl nsw i32 %33, 16
  %35 = load ptr, ptr @spriteoffset, align 8, !tbaa !9
  %36 = getelementptr inbounds i32, ptr %35, i64 %indvars.iv
  store i32 %34, ptr %36, align 4, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %25, i64 6
  %38 = load i16, ptr %37, align 2, !tbaa !15
  %39 = sext i16 %38 to i32
  %40 = shl nsw i32 %39, 16
  %41 = load ptr, ptr @spritetopoffset, align 8, !tbaa !9
  %42 = getelementptr inbounds i32, ptr %41, i64 %indvars.iv
  store i32 %40, ptr %42, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %43 = load i32, ptr @numspritelumps, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = icmp slt i64 %indvars.iv.next, %44
  br i1 %45, label %.lr.ph, label %._crit_edge, !llvm.loop !16

._crit_edge:                                      ; preds = %22, %0
  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 = { nofree nounwind "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 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !7, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!12, !13, i64 6}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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