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

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

@deathmatch_p = external global ptr, align 8
@deathmatchstarts = external global [10 x %struct.mapthing_t], align 16
@.str.7 = external hidden unnamed_addr constant [37 x i8], align 1
@playerstarts = external global [4 x %struct.mapthing_t], align 16

; 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @G_CheckSpot(i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @G_DeathMatchSpawnPlayer(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %7 = load ptr, ptr @deathmatch_p, align 8, !tbaa !9
  %8 = ptrtoint ptr %7 to i64
  %9 = sub i64 %8, ptrtoint (ptr @deathmatchstarts to i64)
  %10 = sdiv exact i64 %9, 10
  %11 = trunc i64 %10 to i32
  store i32 %11, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 4
  br i1 %13, label %14, label %16

14:                                               ; preds = %1
  %15 = load i32, ptr %5, align 4, !tbaa !5
  call void (ptr, ...) @I_Error(ptr noundef @.str.7, i32 noundef %15) #5
  unreachable

16:                                               ; preds = %1
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %17

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

20:                                               ; preds = %17
  %21 = call i32 @P_Random()
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = srem i32 %21, %22
  store i32 %23, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %26
  %28 = call i32 @G_CheckSpot(i32 noundef %24, ptr noundef %27)
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %41

30:                                               ; preds = %20
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  %33 = trunc i32 %32 to i16
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %35
  %37 = getelementptr inbounds %struct.mapthing_t, ptr %36, i32 0, i32 3
  store i16 %33, ptr %37, align 2, !tbaa !11
  %38 = load i32, ptr %3, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %39
  call void @P_SpawnPlayer(ptr noundef %40)
  store i32 1, ptr %6, align 4
  br label %49

41:                                               ; preds = %20
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %4, align 4, !tbaa !5
  br label %17, !llvm.loop !14

45:                                               ; preds = %17
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %47
  call void @P_SpawnPlayer(ptr noundef %48)
  store i32 0, ptr %6, align 4
  br label %49

49:                                               ; preds = %45, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  %50 = load i32, ptr %6, align 4
  switch i32 %50, label %52 [
    i32 0, label %51
    i32 1, label %51
  ]

51:                                               ; preds = %49, %49
  ret void

52:                                               ; preds = %49
  unreachable
}

declare i32 @P_Random() #3

declare void @P_SpawnPlayer(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { "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 = !{!"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 = distinct !{!14, !15}
!15 = !{!"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.mapthing_t = type { i16, i16, i16, i16, i16 }

@deathmatch_p = external local_unnamed_addr global ptr, align 8
@deathmatchstarts = external global [10 x %struct.mapthing_t], align 16
@.str.7 = external hidden unnamed_addr constant [37 x i8], align 1
@playerstarts = external global [4 x %struct.mapthing_t], align 16

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @G_CheckSpot(i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @G_DeathMatchSpawnPlayer(i32 noundef %0) local_unnamed_addr #1 {
  %2 = load ptr, ptr @deathmatch_p, align 8, !tbaa !5
  %3 = ptrtoint ptr %2 to i64
  %4 = sub i64 %3, ptrtoint (ptr @deathmatchstarts to i64)
  %5 = sdiv exact i64 %4, 10
  %6 = trunc i64 %5 to i32
  %7 = icmp slt i32 %6, 4
  br i1 %7, label %13, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %8 = tail call i32 @P_Random() #3
  %9 = srem i32 %8, %6
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %10
  %12 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %11)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %.preheader.1, label %112

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

.preheader.1:                                     ; preds = %.preheader.preheader
  %14 = tail call i32 @P_Random() #3
  %15 = srem i32 %14, %6
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %16
  %18 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %17)
  %.not.1 = icmp eq i32 %18, 0
  br i1 %.not.1, label %.preheader.2, label %112

.preheader.2:                                     ; preds = %.preheader.1
  %19 = tail call i32 @P_Random() #3
  %20 = srem i32 %19, %6
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %21
  %23 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %22)
  %.not.2 = icmp eq i32 %23, 0
  br i1 %.not.2, label %.preheader.3, label %112

.preheader.3:                                     ; preds = %.preheader.2
  %24 = tail call i32 @P_Random() #3
  %25 = srem i32 %24, %6
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %26
  %28 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %27)
  %.not.3 = icmp eq i32 %28, 0
  br i1 %.not.3, label %.preheader.4, label %112

.preheader.4:                                     ; preds = %.preheader.3
  %29 = tail call i32 @P_Random() #3
  %30 = srem i32 %29, %6
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %31
  %33 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %32)
  %.not.4 = icmp eq i32 %33, 0
  br i1 %.not.4, label %.preheader.5, label %112

.preheader.5:                                     ; preds = %.preheader.4
  %34 = tail call i32 @P_Random() #3
  %35 = srem i32 %34, %6
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %36
  %38 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %37)
  %.not.5 = icmp eq i32 %38, 0
  br i1 %.not.5, label %.preheader.6, label %112

.preheader.6:                                     ; preds = %.preheader.5
  %39 = tail call i32 @P_Random() #3
  %40 = srem i32 %39, %6
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %41
  %43 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %42)
  %.not.6 = icmp eq i32 %43, 0
  br i1 %.not.6, label %.preheader.7, label %112

.preheader.7:                                     ; preds = %.preheader.6
  %44 = tail call i32 @P_Random() #3
  %45 = srem i32 %44, %6
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %46
  %48 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %47)
  %.not.7 = icmp eq i32 %48, 0
  br i1 %.not.7, label %.preheader.8, label %112

.preheader.8:                                     ; preds = %.preheader.7
  %49 = tail call i32 @P_Random() #3
  %50 = srem i32 %49, %6
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %51
  %53 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %52)
  %.not.8 = icmp eq i32 %53, 0
  br i1 %.not.8, label %.preheader.9, label %112

.preheader.9:                                     ; preds = %.preheader.8
  %54 = tail call i32 @P_Random() #3
  %55 = srem i32 %54, %6
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %56
  %58 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %57)
  %.not.9 = icmp eq i32 %58, 0
  br i1 %.not.9, label %.preheader.10, label %112

.preheader.10:                                    ; preds = %.preheader.9
  %59 = tail call i32 @P_Random() #3
  %60 = srem i32 %59, %6
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %61
  %63 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %62)
  %.not.10 = icmp eq i32 %63, 0
  br i1 %.not.10, label %.preheader.11, label %112

.preheader.11:                                    ; preds = %.preheader.10
  %64 = tail call i32 @P_Random() #3
  %65 = srem i32 %64, %6
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %66
  %68 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %67)
  %.not.11 = icmp eq i32 %68, 0
  br i1 %.not.11, label %.preheader.12, label %112

.preheader.12:                                    ; preds = %.preheader.11
  %69 = tail call i32 @P_Random() #3
  %70 = srem i32 %69, %6
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %71
  %73 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %72)
  %.not.12 = icmp eq i32 %73, 0
  br i1 %.not.12, label %.preheader.13, label %112

.preheader.13:                                    ; preds = %.preheader.12
  %74 = tail call i32 @P_Random() #3
  %75 = srem i32 %74, %6
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %76
  %78 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %77)
  %.not.13 = icmp eq i32 %78, 0
  br i1 %.not.13, label %.preheader.14, label %112

.preheader.14:                                    ; preds = %.preheader.13
  %79 = tail call i32 @P_Random() #3
  %80 = srem i32 %79, %6
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %81
  %83 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %82)
  %.not.14 = icmp eq i32 %83, 0
  br i1 %.not.14, label %.preheader.15, label %112

.preheader.15:                                    ; preds = %.preheader.14
  %84 = tail call i32 @P_Random() #3
  %85 = srem i32 %84, %6
  %86 = sext i32 %85 to i64
  %87 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %86
  %88 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %87)
  %.not.15 = icmp eq i32 %88, 0
  br i1 %.not.15, label %.preheader.16, label %112

.preheader.16:                                    ; preds = %.preheader.15
  %89 = tail call i32 @P_Random() #3
  %90 = srem i32 %89, %6
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %91
  %93 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %92)
  %.not.16 = icmp eq i32 %93, 0
  br i1 %.not.16, label %.preheader.17, label %112

.preheader.17:                                    ; preds = %.preheader.16
  %94 = tail call i32 @P_Random() #3
  %95 = srem i32 %94, %6
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %96
  %98 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %97)
  %.not.17 = icmp eq i32 %98, 0
  br i1 %.not.17, label %.preheader.18, label %112

.preheader.18:                                    ; preds = %.preheader.17
  %99 = tail call i32 @P_Random() #3
  %100 = srem i32 %99, %6
  %101 = sext i32 %100 to i64
  %102 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %101
  %103 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %102)
  %.not.18 = icmp eq i32 %103, 0
  br i1 %.not.18, label %.preheader.19, label %112

.preheader.19:                                    ; preds = %.preheader.18
  %104 = tail call i32 @P_Random() #3
  %105 = srem i32 %104, %6
  %106 = sext i32 %105 to i64
  %107 = getelementptr inbounds [10 x %struct.mapthing_t], ptr @deathmatchstarts, i64 0, i64 %106
  %108 = tail call i32 @G_CheckSpot(i32 noundef %0, ptr noundef nonnull %107)
  %.not.19 = icmp eq i32 %108, 0
  br i1 %.not.19, label %109, label %112

109:                                              ; preds = %.preheader.19
  %110 = sext i32 %0 to i64
  %111 = getelementptr inbounds [4 x %struct.mapthing_t], ptr @playerstarts, i64 0, i64 %110
  br label %116

112:                                              ; preds = %.preheader.19, %.preheader.18, %.preheader.17, %.preheader.16, %.preheader.15, %.preheader.14, %.preheader.13, %.preheader.12, %.preheader.11, %.preheader.10, %.preheader.9, %.preheader.8, %.preheader.7, %.preheader.6, %.preheader.5, %.preheader.4, %.preheader.3, %.preheader.2, %.preheader.1, %.preheader.preheader
  %.lcssa = phi ptr [ %11, %.preheader.preheader ], [ %17, %.preheader.1 ], [ %22, %.preheader.2 ], [ %27, %.preheader.3 ], [ %32, %.preheader.4 ], [ %37, %.preheader.5 ], [ %42, %.preheader.6 ], [ %47, %.preheader.7 ], [ %52, %.preheader.8 ], [ %57, %.preheader.9 ], [ %62, %.preheader.10 ], [ %67, %.preheader.11 ], [ %72, %.preheader.12 ], [ %77, %.preheader.13 ], [ %82, %.preheader.14 ], [ %87, %.preheader.15 ], [ %92, %.preheader.16 ], [ %97, %.preheader.17 ], [ %102, %.preheader.18 ], [ %107, %.preheader.19 ]
  %113 = trunc i32 %0 to i16
  %114 = add i16 %113, 1
  %115 = getelementptr inbounds i8, ptr %.lcssa, i64 6
  store i16 %114, ptr %115, align 2, !tbaa !9
  br label %116

116:                                              ; preds = %109, %112
  %.sink = phi ptr [ %111, %109 ], [ %.lcssa, %112 ]
  tail call void @P_SpawnPlayer(ptr noundef nonnull %.sink) #3
  ret void
}

declare i32 @P_Random() local_unnamed_addr #2

declare void @P_SpawnPlayer(ptr noundef) local_unnamed_addr #2

attributes #0 = { 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 #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 = { "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 6}
!10 = !{!"", !11, i64 0, !11, i64 2, !11, i64 4, !11, i64 6, !11, i64 8}
!11 = !{!"short", !7, i64 0}


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