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

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

@.str = external hidden unnamed_addr constant [53 x i8], align 1
@maxframe = external dso_local global i32, align 4
@sprtemp = external dso_local global [29 x %struct.spriteframe_t], align 16
@.str.1 = external hidden unnamed_addr constant [56 x i8], align 1
@spritename = external dso_local global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [65 x i8], align 1
@firstspritelump = external global i32, align 4
@.str.3 = external hidden unnamed_addr constant [62 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @R_InstallSpriteLump(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = icmp uge i32 %11, 29
  br i1 %12, label %16, label %13

13:                                               ; preds = %4
  %14 = load i32, ptr %7, align 4, !tbaa !5
  %15 = icmp ugt i32 %14, 8
  br i1 %15, label %16, label %18

16:                                               ; preds = %13, %4
  %17 = load i32, ptr %5, align 4, !tbaa !5
  call void (ptr, ...) @I_Error(ptr noundef @.str, i32 noundef %17) #4
  unreachable

18:                                               ; preds = %13
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = load i32, ptr @maxframe, align 4, !tbaa !5
  %21 = icmp sgt i32 %19, %20
  br i1 %21, label %22, label %24

22:                                               ; preds = %18
  %23 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %23, ptr @maxframe, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %22, %18
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %27, label %82

27:                                               ; preds = %24
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %29
  %31 = getelementptr inbounds %struct.spriteframe_t, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %38

34:                                               ; preds = %27
  %35 = load ptr, ptr @spritename, align 8, !tbaa !11
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = add i32 65, %36
  call void (ptr, ...) @I_Error(ptr noundef @.str.1, ptr noundef %35, i32 noundef %37) #4
  unreachable

38:                                               ; preds = %27
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.spriteframe_t, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = icmp eq i32 %43, 1
  br i1 %44, label %45, label %49

45:                                               ; preds = %38
  %46 = load ptr, ptr @spritename, align 8, !tbaa !11
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = add i32 65, %47
  call void (ptr, ...) @I_Error(ptr noundef @.str.2, ptr noundef %46, i32 noundef %48) #4
  unreachable

49:                                               ; preds = %38
  %50 = load i32, ptr %6, align 4, !tbaa !5
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %51
  %53 = getelementptr inbounds %struct.spriteframe_t, ptr %52, i32 0, i32 0
  store i32 0, ptr %53, align 4, !tbaa !9
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %78, %49
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = icmp slt i32 %55, 8
  br i1 %56, label %57, label %81

57:                                               ; preds = %54
  %58 = load i32, ptr %5, align 4, !tbaa !5
  %59 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %60 = sub nsw i32 %58, %59
  %61 = trunc i32 %60 to i16
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = zext i32 %62 to i64
  %64 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %63
  %65 = getelementptr inbounds %struct.spriteframe_t, ptr %64, i32 0, i32 1
  %66 = load i32, ptr %9, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [8 x i16], ptr %65, i64 0, i64 %67
  store i16 %61, ptr %68, align 2, !tbaa !13
  %69 = load i32, ptr %8, align 4, !tbaa !5
  %70 = trunc i32 %69 to i8
  %71 = load i32, ptr %6, align 4, !tbaa !5
  %72 = zext i32 %71 to i64
  %73 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %72
  %74 = getelementptr inbounds %struct.spriteframe_t, ptr %73, i32 0, i32 2
  %75 = load i32, ptr %9, align 4, !tbaa !5
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [8 x i8], ptr %74, i64 0, i64 %76
  store i8 %70, ptr %77, align 1, !tbaa !15
  br label %78

78:                                               ; preds = %57
  %79 = load i32, ptr %9, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %9, align 4, !tbaa !5
  br label %54, !llvm.loop !16

81:                                               ; preds = %54
  store i32 1, ptr %10, align 4
  br label %137

82:                                               ; preds = %24
  %83 = load i32, ptr %6, align 4, !tbaa !5
  %84 = zext i32 %83 to i64
  %85 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %84
  %86 = getelementptr inbounds %struct.spriteframe_t, ptr %85, i32 0, i32 0
  %87 = load i32, ptr %86, align 4, !tbaa !9
  %88 = icmp eq i32 %87, 0
  br i1 %88, label %89, label %93

89:                                               ; preds = %82
  %90 = load ptr, ptr @spritename, align 8, !tbaa !11
  %91 = load i32, ptr %6, align 4, !tbaa !5
  %92 = add i32 65, %91
  call void (ptr, ...) @I_Error(ptr noundef @.str.2, ptr noundef %90, i32 noundef %92) #4
  unreachable

93:                                               ; preds = %82
  %94 = load i32, ptr %6, align 4, !tbaa !5
  %95 = zext i32 %94 to i64
  %96 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %95
  %97 = getelementptr inbounds %struct.spriteframe_t, ptr %96, i32 0, i32 0
  store i32 1, ptr %97, align 4, !tbaa !9
  %98 = load i32, ptr %7, align 4, !tbaa !5
  %99 = add i32 %98, -1
  store i32 %99, ptr %7, align 4, !tbaa !5
  %100 = load i32, ptr %6, align 4, !tbaa !5
  %101 = zext i32 %100 to i64
  %102 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %101
  %103 = getelementptr inbounds %struct.spriteframe_t, ptr %102, i32 0, i32 1
  %104 = load i32, ptr %7, align 4, !tbaa !5
  %105 = zext i32 %104 to i64
  %106 = getelementptr inbounds [8 x i16], ptr %103, i64 0, i64 %105
  %107 = load i16, ptr %106, align 2, !tbaa !13
  %108 = sext i16 %107 to i32
  %109 = icmp ne i32 %108, -1
  br i1 %109, label %110, label %116

110:                                              ; preds = %93
  %111 = load ptr, ptr @spritename, align 8, !tbaa !11
  %112 = load i32, ptr %6, align 4, !tbaa !5
  %113 = add i32 65, %112
  %114 = load i32, ptr %7, align 4, !tbaa !5
  %115 = add i32 49, %114
  call void (ptr, ...) @I_Error(ptr noundef @.str.3, ptr noundef %111, i32 noundef %113, i32 noundef %115) #4
  unreachable

116:                                              ; preds = %93
  %117 = load i32, ptr %5, align 4, !tbaa !5
  %118 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %119 = sub nsw i32 %117, %118
  %120 = trunc i32 %119 to i16
  %121 = load i32, ptr %6, align 4, !tbaa !5
  %122 = zext i32 %121 to i64
  %123 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %122
  %124 = getelementptr inbounds %struct.spriteframe_t, ptr %123, i32 0, i32 1
  %125 = load i32, ptr %7, align 4, !tbaa !5
  %126 = zext i32 %125 to i64
  %127 = getelementptr inbounds [8 x i16], ptr %124, i64 0, i64 %126
  store i16 %120, ptr %127, align 2, !tbaa !13
  %128 = load i32, ptr %8, align 4, !tbaa !5
  %129 = trunc i32 %128 to i8
  %130 = load i32, ptr %6, align 4, !tbaa !5
  %131 = zext i32 %130 to i64
  %132 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %131
  %133 = getelementptr inbounds %struct.spriteframe_t, ptr %132, i32 0, i32 2
  %134 = load i32, ptr %7, align 4, !tbaa !5
  %135 = zext i32 %134 to i64
  %136 = getelementptr inbounds [8 x i8], ptr %133, i64 0, i64 %135
  store i8 %129, ptr %136, align 1, !tbaa !15
  store i32 0, ptr %10, align 4
  br label %137

137:                                              ; preds = %116, %81
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %138 = load i32, ptr %10, align 4
  switch i32 %138, label %140 [
    i32 0, label %139
    i32 1, label %139
  ]

139:                                              ; preds = %137, %137
  ret void

140:                                              ; preds = %137
  unreachable
}

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

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

; 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 = { 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 }

!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, !6, i64 0}
!10 = !{!"", !6, i64 0, !7, i64 4, !7, i64 20}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!7, !7, i64 0}
!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"

%struct.spriteframe_t = type { i32, [8 x i16], [8 x i8] }

@.str = external hidden unnamed_addr constant [53 x i8], align 1
@maxframe = external dso_local local_unnamed_addr global i32, align 4
@sprtemp = external dso_local local_unnamed_addr global [29 x %struct.spriteframe_t], align 16
@.str.1 = external hidden unnamed_addr constant [56 x i8], align 1
@spritename = external dso_local local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [65 x i8], align 1
@firstspritelump = external local_unnamed_addr global i32, align 4
@.str.3 = external hidden unnamed_addr constant [62 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @R_InstallSpriteLump(i32 noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = icmp ugt i32 %1, 28
  %6 = icmp ugt i32 %2, 8
  %or.cond = or i1 %5, %6
  br i1 %or.cond, label %7, label %8

7:                                                ; preds = %4
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str, i32 noundef %0) #2
  unreachable

8:                                                ; preds = %4
  %9 = load i32, ptr @maxframe, align 4, !tbaa !5
  %10 = icmp slt i32 %9, %1
  br i1 %10, label %11, label %12

11:                                               ; preds = %8
  store i32 %1, ptr @maxframe, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %11, %8
  %13 = icmp eq i32 %2, 0
  %14 = zext nneg i32 %1 to i64
  %15 = getelementptr inbounds [29 x %struct.spriteframe_t], ptr @sprtemp, i64 0, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !9
  br i1 %13, label %17, label %44

17:                                               ; preds = %12
  switch i32 %16, label %.loopexit.loopexit [
    i32 0, label %18
    i32 1, label %21
  ]

18:                                               ; preds = %17
  %19 = load ptr, ptr @spritename, align 8, !tbaa !11
  %20 = add nuw nsw i32 %1, 65
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.1, ptr noundef %19, i32 noundef %20) #2
  unreachable

21:                                               ; preds = %17
  %22 = load ptr, ptr @spritename, align 8, !tbaa !11
  %23 = add nuw nsw i32 %1, 65
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.2, ptr noundef %22, i32 noundef %23) #2
  unreachable

.loopexit.loopexit:                               ; preds = %17
  store i32 0, ptr %15, align 4, !tbaa !9
  %24 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %25 = sub nsw i32 %0, %24
  %26 = trunc i32 %25 to i16
  %27 = getelementptr inbounds i8, ptr %15, i64 4
  %28 = trunc i32 %3 to i8
  %29 = getelementptr inbounds i8, ptr %15, i64 20
  store i16 %26, ptr %27, align 4, !tbaa !13
  store i8 %28, ptr %29, align 4, !tbaa !15
  %30 = getelementptr inbounds i8, ptr %15, i64 6
  store i16 %26, ptr %30, align 2, !tbaa !13
  %31 = getelementptr inbounds i8, ptr %15, i64 21
  store i8 %28, ptr %31, align 1, !tbaa !15
  %32 = getelementptr inbounds i8, ptr %15, i64 8
  store i16 %26, ptr %32, align 4, !tbaa !13
  %33 = getelementptr inbounds i8, ptr %15, i64 22
  store i8 %28, ptr %33, align 2, !tbaa !15
  %34 = getelementptr inbounds i8, ptr %15, i64 10
  store i16 %26, ptr %34, align 2, !tbaa !13
  %35 = getelementptr inbounds i8, ptr %15, i64 23
  store i8 %28, ptr %35, align 1, !tbaa !15
  %36 = getelementptr inbounds i8, ptr %15, i64 12
  store i16 %26, ptr %36, align 4, !tbaa !13
  %37 = getelementptr inbounds i8, ptr %15, i64 24
  store i8 %28, ptr %37, align 4, !tbaa !15
  %38 = getelementptr inbounds i8, ptr %15, i64 14
  store i16 %26, ptr %38, align 2, !tbaa !13
  %39 = getelementptr inbounds i8, ptr %15, i64 25
  store i8 %28, ptr %39, align 1, !tbaa !15
  %40 = getelementptr inbounds i8, ptr %15, i64 16
  store i16 %26, ptr %40, align 4, !tbaa !13
  %41 = getelementptr inbounds i8, ptr %15, i64 26
  store i8 %28, ptr %41, align 2, !tbaa !15
  %42 = getelementptr inbounds i8, ptr %15, i64 18
  store i16 %26, ptr %42, align 2, !tbaa !13
  %43 = getelementptr inbounds i8, ptr %15, i64 27
  store i8 %28, ptr %43, align 1, !tbaa !15
  br label %.loopexit

44:                                               ; preds = %12
  %45 = icmp eq i32 %16, 0
  br i1 %45, label %46, label %49

46:                                               ; preds = %44
  %47 = load ptr, ptr @spritename, align 8, !tbaa !11
  %48 = add nuw nsw i32 %1, 65
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.2, ptr noundef %47, i32 noundef %48) #2
  unreachable

49:                                               ; preds = %44
  store i32 1, ptr %15, align 4, !tbaa !9
  %50 = add nsw i32 %2, -1
  %51 = getelementptr inbounds i8, ptr %15, i64 4
  %52 = zext nneg i32 %50 to i64
  %53 = getelementptr inbounds [8 x i16], ptr %51, i64 0, i64 %52
  %54 = load i16, ptr %53, align 2, !tbaa !13
  %.not = icmp eq i16 %54, -1
  br i1 %.not, label %59, label %55

55:                                               ; preds = %49
  %56 = load ptr, ptr @spritename, align 8, !tbaa !11
  %57 = add nuw nsw i32 %1, 65
  %58 = or disjoint i32 %2, 48
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.3, ptr noundef %56, i32 noundef %57, i32 noundef %58) #2
  unreachable

59:                                               ; preds = %49
  %60 = load i32, ptr @firstspritelump, align 4, !tbaa !5
  %61 = sub nsw i32 %0, %60
  %62 = trunc i32 %61 to i16
  store i16 %62, ptr %53, align 2, !tbaa !13
  %63 = trunc i32 %3 to i8
  %64 = getelementptr inbounds i8, ptr %15, i64 20
  %65 = getelementptr inbounds [8 x i8], ptr %64, i64 0, i64 %52
  store i8 %63, ptr %65, align 1, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %59
  ret void
}

; Function Attrs: noreturn
declare void @I_Error(ptr noundef, ...) local_unnamed_addr #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 = { 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 #2 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"", !6, i64 0, !7, i64 4, !7, i64 20}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!7, !7, i64 0}


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