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

; 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.mobj_s = type { %struct.thinker_s, i32, i32, i32, ptr, ptr, i32, i32, i32, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, i32, ptr, i32, i32, i32, i32, ptr, i32, i32, ptr, i32, %struct.mapthing_t, ptr }
%struct.thinker_s = type { ptr, ptr, %union.actionf_t }
%union.actionf_t = type { ptr }
%struct.mapthing_t = type { i16, i16, i16, i16, i16 }

@tmfloorz = external dso_local global i32, align 4
@tmceilingz = external dso_local global i32, align 4

; 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 i32 @P_CheckPosition(ptr noundef, i32 noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @P_ThingHeightClip(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.mobj_s, ptr %6, i32 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.mobj_s, ptr %9, i32 0, i32 12
  %11 = load i32, ptr %10, align 8, !tbaa !15
  %12 = icmp eq i32 %8, %11
  %13 = zext i1 %12 to i32
  store i32 %13, ptr %4, align 4, !tbaa !16
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.mobj_s, ptr %15, i32 0, i32 1
  %17 = load i32, ptr %16, align 8, !tbaa !17
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mobj_s, ptr %18, i32 0, i32 2
  %20 = load i32, ptr %19, align 4, !tbaa !18
  %21 = call i32 @P_CheckPosition(ptr noundef %14, i32 noundef %17, i32 noundef %20)
  %22 = load i32, ptr @tmfloorz, align 4, !tbaa !16
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mobj_s, ptr %23, i32 0, i32 12
  store i32 %22, ptr %24, align 8, !tbaa !15
  %25 = load i32, ptr @tmceilingz, align 4, !tbaa !16
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mobj_s, ptr %26, i32 0, i32 13
  store i32 %25, ptr %27, align 4, !tbaa !19
  %28 = load i32, ptr %4, align 4, !tbaa !16
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %36

30:                                               ; preds = %1
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mobj_s, ptr %31, i32 0, i32 12
  %33 = load i32, ptr %32, align 8, !tbaa !15
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mobj_s, ptr %34, i32 0, i32 3
  store i32 %33, ptr %35, align 8, !tbaa !9
  br label %59

36:                                               ; preds = %1
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mobj_s, ptr %37, i32 0, i32 3
  %39 = load i32, ptr %38, align 8, !tbaa !9
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.mobj_s, ptr %40, i32 0, i32 15
  %42 = load i32, ptr %41, align 4, !tbaa !20
  %43 = add nsw i32 %39, %42
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mobj_s, ptr %44, i32 0, i32 13
  %46 = load i32, ptr %45, align 4, !tbaa !19
  %47 = icmp sgt i32 %43, %46
  br i1 %47, label %48, label %58

48:                                               ; preds = %36
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.mobj_s, ptr %49, i32 0, i32 13
  %51 = load i32, ptr %50, align 4, !tbaa !19
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.mobj_s, ptr %52, i32 0, i32 15
  %54 = load i32, ptr %53, align 4, !tbaa !20
  %55 = sub nsw i32 %51, %54
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mobj_s, ptr %56, i32 0, i32 3
  store i32 %55, ptr %57, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %48, %36
  br label %59

59:                                               ; preds = %58, %30
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mobj_s, ptr %60, i32 0, i32 13
  %62 = load i32, ptr %61, align 4, !tbaa !19
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.mobj_s, ptr %63, i32 0, i32 12
  %65 = load i32, ptr %64, align 8, !tbaa !15
  %66 = sub nsw i32 %62, %65
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mobj_s, ptr %67, i32 0, i32 15
  %69 = load i32, ptr %68, align 4, !tbaa !20
  %70 = icmp slt i32 %66, %69
  br i1 %70, label %71, label %72

71:                                               ; preds = %59
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %73

72:                                               ; preds = %59
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %73

73:                                               ; preds = %72, %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %74 = load i32, ptr %2, align 4
  ret i32 %74
}

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 }

!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, !12, i64 32}
!10 = !{!"mobj_s", !11, i64 0, !12, i64 24, !12, i64 28, !12, i64 32, !6, i64 40, !6, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !12, i64 96, !12, i64 100, !12, i64 104, !12, i64 108, !12, i64 112, !12, i64 116, !12, i64 120, !12, i64 124, !12, i64 128, !6, i64 136, !12, i64 144, !6, i64 152, !12, i64 160, !12, i64 164, !12, i64 168, !12, i64 172, !6, i64 176, !12, i64 184, !12, i64 188, !6, i64 192, !12, i64 200, !13, i64 204, !6, i64 216}
!11 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !14, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !14, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!10, !12, i64 96}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !12, i64 24}
!18 = !{!10, !12, i64 28}
!19 = !{!10, !12, i64 100}
!20 = !{!10, !12, i64 108}


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

@tmfloorz = external dso_local local_unnamed_addr global i32, align 4
@tmceilingz = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @P_ThingHeightClip(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 32
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 96
  %5 = load i32, ptr %4, align 8, !tbaa !14
  %6 = icmp eq i32 %3, %5
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load i32, ptr %7, align 8, !tbaa !15
  %9 = getelementptr inbounds i8, ptr %0, i64 28
  %10 = load i32, ptr %9, align 4, !tbaa !16
  %11 = tail call i32 @P_CheckPosition(ptr noundef %0, i32 noundef %8, i32 noundef %10)
  %12 = load i32, ptr @tmfloorz, align 4, !tbaa !17
  store i32 %12, ptr %4, align 8, !tbaa !14
  %13 = load i32, ptr @tmceilingz, align 4, !tbaa !17
  %14 = getelementptr inbounds i8, ptr %0, i64 100
  store i32 %13, ptr %14, align 4, !tbaa !18
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 108
  br i1 %6, label %15, label %16

15:                                               ; preds = %1
  store i32 %12, ptr %2, align 8, !tbaa !5
  %.pre = load i32, ptr %.phi.trans.insert, align 4, !tbaa !19
  br label %23

16:                                               ; preds = %1
  %17 = load i32, ptr %2, align 8, !tbaa !5
  %18 = load i32, ptr %.phi.trans.insert, align 4, !tbaa !19
  %19 = add nsw i32 %18, %17
  %20 = icmp sgt i32 %19, %13
  br i1 %20, label %21, label %23

21:                                               ; preds = %16
  %22 = sub nsw i32 %13, %18
  store i32 %22, ptr %2, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %16, %21, %15
  %24 = phi i32 [ %18, %16 ], [ %18, %21 ], [ %.pre, %15 ]
  %25 = sub nsw i32 %13, %12
  %26 = icmp sge i32 %25, %24
  %. = zext i1 %26 to i32
  ret i32 %.
}

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

!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, !11, i64 32}
!6 = !{!"mobj_s", !7, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !8, i64 40, !8, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !8, i64 72, !8, i64 80, !8, i64 88, !11, i64 96, !11, i64 100, !11, i64 104, !11, i64 108, !11, i64 112, !11, i64 116, !11, i64 120, !11, i64 124, !11, i64 128, !8, i64 136, !11, i64 144, !8, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !8, i64 176, !11, i64 184, !11, i64 188, !8, i64 192, !11, i64 200, !12, i64 204, !8, i64 216}
!7 = !{!"thinker_s", !8, i64 0, !8, i64 8, !9, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!13 = !{!"short", !9, i64 0}
!14 = !{!6, !11, i64 96}
!15 = !{!6, !11, i64 24}
!16 = !{!6, !11, i64 28}
!17 = !{!11, !11, i64 0}
!18 = !{!6, !11, i64 100}
!19 = !{!6, !11, i64 108}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass