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

; 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.anim_t = type { i32, i32, i32, i32, i32 }
%struct.animdef_t = type { i32, [9 x i8], [9 x i8], i32 }

@anims = external dso_local global [32 x %struct.anim_t], align 16
@lastanim = external dso_local global ptr, align 8
@animdefs = external hidden constant [23 x %struct.animdef_t], align 16
@.str = external hidden unnamed_addr constant [40 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @P_InitPicAnims() #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store ptr @anims, ptr @lastanim, align 8, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %2

2:                                                ; preds = %111, %0
  %3 = load i32, ptr %1, align 4, !tbaa !9
  %4 = sext i32 %3 to i64
  %5 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %4
  %6 = getelementptr inbounds %struct.animdef_t, ptr %5, i32 0, i32 3
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %9, label %114

9:                                                ; preds = %2
  %10 = load i32, ptr %1, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %11
  %13 = getelementptr inbounds %struct.animdef_t, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %42

16:                                               ; preds = %9
  %17 = load i32, ptr %1, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %18
  %20 = getelementptr inbounds %struct.animdef_t, ptr %19, i32 0, i32 2
  %21 = getelementptr inbounds [9 x i8], ptr %20, i64 0, i64 0
  %22 = call i32 @R_CheckTextureNumForName(ptr noundef %21)
  %23 = icmp eq i32 %22, -1
  br i1 %23, label %24, label %25

24:                                               ; preds = %16
  br label %111

25:                                               ; preds = %16
  %26 = load i32, ptr %1, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.animdef_t, ptr %28, i32 0, i32 1
  %30 = getelementptr inbounds [9 x i8], ptr %29, i64 0, i64 0
  %31 = call i32 @R_TextureNumForName(ptr noundef %30)
  %32 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.anim_t, ptr %32, i32 0, i32 1
  store i32 %31, ptr %33, align 4, !tbaa !14
  %34 = load i32, ptr %1, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.animdef_t, ptr %36, i32 0, i32 2
  %38 = getelementptr inbounds [9 x i8], ptr %37, i64 0, i64 0
  %39 = call i32 @R_TextureNumForName(ptr noundef %38)
  %40 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.anim_t, ptr %40, i32 0, i32 2
  store i32 %39, ptr %41, align 4, !tbaa !16
  br label %68

42:                                               ; preds = %9
  %43 = load i32, ptr %1, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %44
  %46 = getelementptr inbounds %struct.animdef_t, ptr %45, i32 0, i32 2
  %47 = getelementptr inbounds [9 x i8], ptr %46, i64 0, i64 0
  %48 = call i32 @W_CheckNumForName(ptr noundef %47)
  %49 = icmp eq i32 %48, -1
  br i1 %49, label %50, label %51

50:                                               ; preds = %42
  br label %111

51:                                               ; preds = %42
  %52 = load i32, ptr %1, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.animdef_t, ptr %54, i32 0, i32 1
  %56 = getelementptr inbounds [9 x i8], ptr %55, i64 0, i64 0
  %57 = call i32 @R_FlatNumForName(ptr noundef %56)
  %58 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.anim_t, ptr %58, i32 0, i32 1
  store i32 %57, ptr %59, align 4, !tbaa !14
  %60 = load i32, ptr %1, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %61
  %63 = getelementptr inbounds %struct.animdef_t, ptr %62, i32 0, i32 2
  %64 = getelementptr inbounds [9 x i8], ptr %63, i64 0, i64 0
  %65 = call i32 @R_FlatNumForName(ptr noundef %64)
  %66 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.anim_t, ptr %66, i32 0, i32 2
  store i32 %65, ptr %67, align 4, !tbaa !16
  br label %68

68:                                               ; preds = %51, %25
  %69 = load i32, ptr %1, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %70
  %72 = getelementptr inbounds %struct.animdef_t, ptr %71, i32 0, i32 0
  %73 = load i32, ptr %72, align 4, !tbaa !13
  %74 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.anim_t, ptr %74, i32 0, i32 0
  store i32 %73, ptr %75, align 4, !tbaa !17
  %76 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.anim_t, ptr %76, i32 0, i32 1
  %78 = load i32, ptr %77, align 4, !tbaa !14
  %79 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.anim_t, ptr %79, i32 0, i32 2
  %81 = load i32, ptr %80, align 4, !tbaa !16
  %82 = sub nsw i32 %78, %81
  %83 = add nsw i32 %82, 1
  %84 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.anim_t, ptr %84, i32 0, i32 3
  store i32 %83, ptr %85, align 4, !tbaa !18
  %86 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.anim_t, ptr %86, i32 0, i32 3
  %88 = load i32, ptr %87, align 4, !tbaa !18
  %89 = icmp slt i32 %88, 2
  br i1 %89, label %90, label %101

90:                                               ; preds = %68
  %91 = load i32, ptr %1, align 4, !tbaa !9
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %92
  %94 = getelementptr inbounds %struct.animdef_t, ptr %93, i32 0, i32 2
  %95 = getelementptr inbounds [9 x i8], ptr %94, i64 0, i64 0
  %96 = load i32, ptr %1, align 4, !tbaa !9
  %97 = sext i32 %96 to i64
  %98 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %97
  %99 = getelementptr inbounds %struct.animdef_t, ptr %98, i32 0, i32 1
  %100 = getelementptr inbounds [9 x i8], ptr %99, i64 0, i64 0
  call void (ptr, ...) @I_Error(ptr noundef @.str, ptr noundef %95, ptr noundef %100) #5
  unreachable

101:                                              ; preds = %68
  %102 = load i32, ptr %1, align 4, !tbaa !9
  %103 = sext i32 %102 to i64
  %104 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %103
  %105 = getelementptr inbounds %struct.animdef_t, ptr %104, i32 0, i32 3
  %106 = load i32, ptr %105, align 4, !tbaa !11
  %107 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.anim_t, ptr %107, i32 0, i32 4
  store i32 %106, ptr %108, align 4, !tbaa !19
  %109 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.anim_t, ptr %109, i32 1
  store ptr %110, ptr @lastanim, align 8, !tbaa !5
  br label %111

111:                                              ; preds = %101, %50, %24
  %112 = load i32, ptr %1, align 4, !tbaa !9
  %113 = add nsw i32 %112, 1
  store i32 %113, ptr %1, align 4, !tbaa !9
  br label %2, !llvm.loop !20

114:                                              ; preds = %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

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

declare i32 @R_CheckTextureNumForName(ptr noundef) #2

declare i32 @R_TextureNumForName(ptr noundef) #2

declare i32 @W_CheckNumForName(ptr noundef) #2

declare i32 @R_FlatNumForName(ptr noundef) #2

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) #3

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = { noreturn "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 }
attributes #5 = { noreturn }

!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, !10, i64 24}
!12 = !{!"", !10, i64 0, !7, i64 4, !7, i64 13, !10, i64 24}
!13 = !{!12, !10, i64 0}
!14 = !{!15, !10, i64 4}
!15 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16}
!16 = !{!15, !10, i64 8}
!17 = !{!15, !10, i64 0}
!18 = !{!15, !10, i64 12}
!19 = !{!15, !10, i64 16}
!20 = distinct !{!20, !21}
!21 = !{!"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.anim_t = type { i32, i32, i32, i32, i32 }
%struct.animdef_t = type { i32, [9 x i8], [9 x i8], i32 }

@anims = external dso_local global [32 x %struct.anim_t], align 16
@lastanim = external dso_local local_unnamed_addr global ptr, align 8
@animdefs = external hidden constant [23 x %struct.animdef_t], align 16
@.str = external hidden unnamed_addr constant [40 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @P_InitPicAnims() local_unnamed_addr #0 {
  store ptr @anims, ptr @lastanim, align 8, !tbaa !5
  %1 = load i32, ptr getelementptr inbounds (i8, ptr @animdefs, i64 24), align 8, !tbaa !9
  %2 = icmp sgt i32 %1, 0
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %40
  %indvars.iv = phi i64 [ %indvars.iv.next, %40 ], [ 0, %0 ]
  %3 = phi i32 [ %43, %40 ], [ %1, %0 ]
  %4 = phi ptr [ %41, %40 ], [ @animdefs, %0 ]
  %5 = load i32, ptr %4, align 4, !tbaa !12
  %.not = icmp eq i32 %5, 0
  %6 = getelementptr inbounds i8, ptr %4, i64 13
  br i1 %.not, label %16, label %7

7:                                                ; preds = %.lr.ph
  %8 = tail call i32 @R_CheckTextureNumForName(ptr noundef nonnull %6) #3
  %9 = icmp eq i32 %8, -1
  br i1 %9, label %40, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %4, i64 4
  %12 = tail call i32 @R_TextureNumForName(ptr noundef nonnull %11) #3
  %13 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 4
  store i32 %12, ptr %14, align 4, !tbaa !13
  %15 = tail call i32 @R_TextureNumForName(ptr noundef nonnull %6) #3
  br label %25

16:                                               ; preds = %.lr.ph
  %17 = tail call i32 @W_CheckNumForName(ptr noundef nonnull %6) #3
  %18 = icmp eq i32 %17, -1
  br i1 %18, label %40, label %19

19:                                               ; preds = %16
  %20 = getelementptr inbounds i8, ptr %4, i64 4
  %21 = tail call i32 @R_FlatNumForName(ptr noundef nonnull %20) #3
  %22 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 4
  store i32 %21, ptr %23, align 4, !tbaa !13
  %24 = tail call i32 @R_FlatNumForName(ptr noundef nonnull %6) #3
  br label %25

25:                                               ; preds = %19, %10
  %.sink = phi i32 [ %24, %19 ], [ %15, %10 ]
  %26 = load ptr, ptr @lastanim, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  store i32 %.sink, ptr %27, align 4, !tbaa !15
  store i32 %5, ptr %26, align 4, !tbaa !16
  %28 = getelementptr inbounds i8, ptr %26, i64 4
  %29 = load i32, ptr %28, align 4, !tbaa !13
  %30 = sub nsw i32 %29, %.sink
  %31 = add nsw i32 %30, 1
  %32 = getelementptr inbounds i8, ptr %26, i64 12
  store i32 %31, ptr %32, align 4, !tbaa !17
  %33 = icmp slt i32 %30, 1
  br i1 %33, label %34, label %37

34:                                               ; preds = %25
  %35 = getelementptr inbounds i8, ptr %4, i64 13
  %36 = getelementptr inbounds i8, ptr %4, i64 4
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str, ptr noundef nonnull %35, ptr noundef nonnull %36) #4
  unreachable

37:                                               ; preds = %25
  %38 = getelementptr inbounds i8, ptr %26, i64 16
  store i32 %3, ptr %38, align 4, !tbaa !18
  %39 = getelementptr inbounds i8, ptr %26, i64 20
  store ptr %39, ptr @lastanim, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %16, %7, %37
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %41 = getelementptr inbounds [23 x %struct.animdef_t], ptr @animdefs, i64 0, i64 %indvars.iv.next
  %42 = getelementptr inbounds i8, ptr %41, i64 24
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = icmp sgt i32 %43, 0
  br i1 %44, label %.lr.ph, label %._crit_edge, !llvm.loop !19

._crit_edge:                                      ; preds = %40, %0
  ret void
}

declare i32 @R_CheckTextureNumForName(ptr noundef) local_unnamed_addr #1

declare i32 @R_TextureNumForName(ptr noundef) local_unnamed_addr #1

declare i32 @W_CheckNumForName(ptr noundef) local_unnamed_addr #1

declare i32 @R_FlatNumForName(ptr noundef) local_unnamed_addr #1

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #2

attributes #0 = { 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 #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 = { noreturn "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 }
attributes #4 = { noreturn 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, !11, i64 24}
!10 = !{!"", !11, i64 0, !7, i64 4, !7, i64 13, !11, i64 24}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!14, !11, i64 4}
!14 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16}
!15 = !{!14, !11, i64 8}
!16 = !{!14, !11, i64 0}
!17 = !{!14, !11, i64 12}
!18 = !{!14, !11, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass