; 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.sector_t = type { i32, i32, i16, i16, i16, i16, i16, i32, ptr, [4 x i32], %struct.degenmobj_t, i32, ptr, ptr, i32, ptr }
%struct.degenmobj_t = type { %struct.thinker_s, i32, i32, i32 }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }

@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [44 x i8], align 1

; 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: nounwind uwtable
declare dso_local ptr @getNextSector(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @P_FindNextHighestFloor(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca [20 x i32], align 16
  %13 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %14 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %14, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 80, ptr %12) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %56, %2
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.sector_t, ptr %17, i32 0, i32 14
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = icmp slt i32 %16, %19
  br i1 %20, label %21, label %59

21:                                               ; preds = %15
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.sector_t, ptr %22, i32 0, i32 15
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = load i32, ptr %6, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds ptr, ptr %24, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  store ptr %28, ptr %9, align 8, !tbaa !5
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = call ptr @getNextSector(ptr noundef %29, ptr noundef %30)
  store ptr %31, ptr %10, align 8, !tbaa !5
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = icmp ne ptr %32, null
  br i1 %33, label %35, label %34

34:                                               ; preds = %21
  br label %56

35:                                               ; preds = %21
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.sector_t, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !17
  %39 = load i32, ptr %11, align 4, !tbaa !9
  %40 = icmp sgt i32 %38, %39
  br i1 %40, label %41, label %49

41:                                               ; preds = %35
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.sector_t, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !17
  %45 = load i32, ptr %7, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %45 to i64
  %48 = getelementptr inbounds [20 x i32], ptr %12, i64 0, i64 %47
  store i32 %44, ptr %48, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %41, %35
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = icmp sge i32 %50, 20
  br i1 %51, label %52, label %55

52:                                               ; preds = %49
  %53 = load ptr, ptr @stderr, align 8, !tbaa !5
  %54 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %53, ptr noundef @.str.1) #3
  br label %59

55:                                               ; preds = %49
  br label %56

56:                                               ; preds = %55, %34
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %6, align 4, !tbaa !9
  br label %15, !llvm.loop !18

59:                                               ; preds = %52, %15
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %64, label %62

62:                                               ; preds = %59
  %63 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %63, ptr %3, align 4
  store i32 1, ptr %13, align 4
  br label %89

64:                                               ; preds = %59
  %65 = getelementptr inbounds [20 x i32], ptr %12, i64 0, i64 0
  %66 = load i32, ptr %65, align 16, !tbaa !9
  store i32 %66, ptr %8, align 4, !tbaa !9
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %67

67:                                               ; preds = %84, %64
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = icmp slt i32 %68, %69
  br i1 %70, label %71, label %87

71:                                               ; preds = %67
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds [20 x i32], ptr %12, i64 0, i64 %73
  %75 = load i32, ptr %74, align 4, !tbaa !9
  %76 = load i32, ptr %8, align 4, !tbaa !9
  %77 = icmp slt i32 %75, %76
  br i1 %77, label %78, label %83

78:                                               ; preds = %71
  %79 = load i32, ptr %6, align 4, !tbaa !9
  %80 = sext i32 %79 to i64
  %81 = getelementptr inbounds [20 x i32], ptr %12, i64 0, i64 %80
  %82 = load i32, ptr %81, align 4, !tbaa !9
  store i32 %82, ptr %8, align 4, !tbaa !9
  br label %83

83:                                               ; preds = %78, %71
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %6, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %6, align 4, !tbaa !9
  br label %67, !llvm.loop !20

87:                                               ; preds = %67
  %88 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %88, ptr %3, align 4
  store i32 1, ptr %13, align 4
  br label %89

89:                                               ; preds = %87, %62
  call void @llvm.lifetime.end.p0(i64 80, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %90 = load i32, ptr %3, align 4
  ret i32 %90
}

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 112}
!12 = !{!"", !10, i64 0, !10, i64 4, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !10, i64 20, !6, i64 24, !7, i64 32, !14, i64 48, !10, i64 88, !6, i64 96, !6, i64 104, !10, i64 112, !6, i64 120}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !15, i64 0, !10, i64 24, !10, i64 28, !10, i64 32}
!15 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!16 = !{!12, !6, i64 120}
!17 = !{!12, !10, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [44 x i8], align 1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @P_FindNextHighestFloor(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = alloca [20 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %3) #4
  %4 = getelementptr inbounds i8, ptr %0, i64 112
  %5 = load i32, ptr %4, align 8, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 120
  br label %8

8:                                                ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %25 ]
  %.02436 = phi i32 [ 0, %.lr.ph ], [ %.2, %25 ]
  %9 = load ptr, ptr %7, align 8, !tbaa !14
  %10 = getelementptr inbounds ptr, ptr %9, i64 %indvars.iv
  %11 = load ptr, ptr %10, align 8, !tbaa !15
  %12 = tail call ptr @getNextSector(ptr noundef %11, ptr noundef nonnull %0)
  %.not = icmp eq ptr %12, null
  br i1 %.not, label %25, label %13

13:                                               ; preds = %8
  %14 = load i32, ptr %12, align 8, !tbaa !16
  %15 = icmp sgt i32 %14, %1
  br i1 %15, label %16, label %20

16:                                               ; preds = %13
  %17 = add nsw i32 %.02436, 1
  %18 = sext i32 %.02436 to i64
  %19 = getelementptr inbounds [20 x i32], ptr %3, i64 0, i64 %18
  store i32 %14, ptr %19, align 4, !tbaa !17
  br label %20

20:                                               ; preds = %16, %13
  %.125 = phi i32 [ %17, %16 ], [ %.02436, %13 ]
  %21 = icmp sgt i32 %.125, 19
  br i1 %21, label %.thread49, label %25

.thread49:                                        ; preds = %20
  %22 = load ptr, ptr @stderr, align 8, !tbaa !15
  %23 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str.1) #5
  %24 = load i32, ptr %3, align 16, !tbaa !17
  br label %.lr.ph41.preheader

25:                                               ; preds = %20, %8
  %.2 = phi i32 [ %.125, %20 ], [ %.02436, %8 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = load i32, ptr %4, align 8, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = icmp slt i64 %indvars.iv.next, %27
  br i1 %28, label %8, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %25
  %.not31 = icmp eq i32 %.2, 0
  br i1 %.not31, label %.loopexit, label %29

29:                                               ; preds = %._crit_edge
  %30 = load i32, ptr %3, align 16, !tbaa !17
  %31 = icmp sgt i32 %.2, 1
  br i1 %31, label %.lr.ph41.preheader, label %.loopexit

.lr.ph41.preheader:                               ; preds = %.thread49, %29
  %32 = phi i32 [ %24, %.thread49 ], [ %30, %29 ]
  %.33451 = phi i32 [ %.125, %.thread49 ], [ %.2, %29 ]
  %wide.trip.count = zext nneg i32 %.33451 to i64
  %33 = add nsw i64 %wide.trip.count, -1
  %min.iters.check = icmp ult i32 %.33451, 9
  br i1 %min.iters.check, label %.lr.ph41.preheader54, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph41.preheader
  %n.vec = and i64 %33, -8
  %ind.end = or disjoint i64 %n.vec, 1
  %minmax.ident.splatinsert = insertelement <4 x i32> poison, i32 %32, i64 0
  %minmax.ident.splat = shufflevector <4 x i32> %minmax.ident.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ %minmax.ident.splat, %vector.ph ], [ %36, %vector.body ]
  %vec.phi52 = phi <4 x i32> [ %minmax.ident.splat, %vector.ph ], [ %37, %vector.body ]
  %offset.idx = or disjoint i64 %index, 1
  %34 = getelementptr inbounds [20 x i32], ptr %3, i64 0, i64 %offset.idx
  %35 = getelementptr inbounds i8, ptr %34, i64 16
  %wide.load = load <4 x i32>, ptr %34, align 4, !tbaa !17
  %wide.load53 = load <4 x i32>, ptr %35, align 4, !tbaa !17
  %36 = tail call <4 x i32> @llvm.smin.v4i32(<4 x i32> %wide.load, <4 x i32> %vec.phi)
  %37 = tail call <4 x i32> @llvm.smin.v4i32(<4 x i32> %wide.load53, <4 x i32> %vec.phi52)
  %index.next = add nuw i64 %index, 8
  %38 = icmp eq i64 %index.next, %n.vec
  br i1 %38, label %middle.block, label %vector.body, !llvm.loop !20

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smin.v4i32(<4 x i32> %36, <4 x i32> %37)
  %39 = tail call i32 @llvm.vector.reduce.smin.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %33, %n.vec
  br i1 %cmp.n, label %.loopexit, label %.lr.ph41.preheader54

.lr.ph41.preheader54:                             ; preds = %middle.block, %.lr.ph41.preheader
  %indvars.iv44.ph = phi i64 [ 1, %.lr.ph41.preheader ], [ %ind.end, %middle.block ]
  %.02638.ph = phi i32 [ %32, %.lr.ph41.preheader ], [ %39, %middle.block ]
  br label %.lr.ph41

.lr.ph41:                                         ; preds = %.lr.ph41.preheader54, %.lr.ph41
  %indvars.iv44 = phi i64 [ %indvars.iv.next45, %.lr.ph41 ], [ %indvars.iv44.ph, %.lr.ph41.preheader54 ]
  %.02638 = phi i32 [ %spec.select, %.lr.ph41 ], [ %.02638.ph, %.lr.ph41.preheader54 ]
  %40 = getelementptr inbounds [20 x i32], ptr %3, i64 0, i64 %indvars.iv44
  %41 = load i32, ptr %40, align 4, !tbaa !17
  %spec.select = tail call i32 @llvm.smin.i32(i32 %41, i32 %.02638)
  %indvars.iv.next45 = add nuw nsw i64 %indvars.iv44, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next45, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph41, !llvm.loop !23

.loopexit:                                        ; preds = %.lr.ph41, %middle.block, %2, %29, %._crit_edge
  %.0 = phi i32 [ %1, %._crit_edge ], [ %30, %29 ], [ %1, %2 ], [ %39, %middle.block ], [ %spec.select, %.lr.ph41 ]
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %3) #4
  ret i32 %.0
}

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.smin.v4i32(<4 x i32>, <4 x i32>) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smin.v4i32(<4 x i32>) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }
attributes #5 = { cold 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, !7, i64 112}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 10, !10, i64 12, !10, i64 14, !10, i64 16, !7, i64 20, !11, i64 24, !8, i64 32, !12, i64 48, !7, i64 88, !11, i64 96, !11, i64 104, !7, i64 112, !11, i64 120}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"", !13, i64 0, !7, i64 24, !7, i64 28, !7, i64 32}
!13 = !{!"thinker_s", !11, i64 0, !11, i64 8, !8, i64 16}
!14 = !{!6, !11, i64 120}
!15 = !{!11, !11, i64 0}
!16 = !{!6, !7, i64 0}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19, !21, !22}
!21 = !{!"llvm.loop.isvectorized", i32 1}
!22 = !{!"llvm.loop.unroll.runtime.disable"}
!23 = distinct !{!23, !19, !22, !21}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,SROAPass,SimplifyCFGPass,TailCallElimPass