; 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.intercept_t = type { i32, i32, %union.anon }
%union.anon = type { ptr }
%struct.line_s = type { ptr, ptr, i32, i32, i16, i16, i16, [2 x i16], [4 x i32], i32, ptr, ptr, i32, ptr }
%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 }

@opentop = external global i32, align 4
@openbottom = external global i32, align 4
@slidemo = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@openrange = external global i32, align 4
@bestslidefrac = external dso_local global i32, align 4
@secondslidefrac = external dso_local global i32, align 4
@bestslideline = external dso_local global ptr, align 8
@secondslideline = external dso_local global ptr, align 8

; 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

declare void @P_LineOpening(ptr noundef) #1

declare i32 @P_PointOnLineSide(i32 noundef, i32 noundef, ptr noundef) #1

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

10:                                               ; preds = %1
  call void (ptr, ...) @I_Error(ptr noundef @.str) #5
  unreachable

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.intercept_t, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !12
  store ptr %14, ptr %4, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.line_s, ptr %15, i32 0, i32 4
  %17 = load i16, ptr %16, align 8, !tbaa !13
  %18 = sext i16 %17 to i32
  %19 = and i32 %18, 4
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %33, label %21

21:                                               ; preds = %11
  %22 = load ptr, ptr @slidemo, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mobj_s, ptr %22, i32 0, i32 1
  %24 = load i32, ptr %23, align 8, !tbaa !16
  %25 = load ptr, ptr @slidemo, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mobj_s, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !20
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = call i32 @P_PointOnLineSide(i32 noundef %24, i32 noundef %27, ptr noundef %28)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %21
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

32:                                               ; preds = %21
  br label %61

33:                                               ; preds = %11
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  call void @P_LineOpening(ptr noundef %34)
  %35 = load i32, ptr @openrange, align 4, !tbaa !21
  %36 = load ptr, ptr @slidemo, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.mobj_s, ptr %36, i32 0, i32 15
  %38 = load i32, ptr %37, align 4, !tbaa !22
  %39 = icmp slt i32 %35, %38
  br i1 %39, label %40, label %41

40:                                               ; preds = %33
  br label %61

41:                                               ; preds = %33
  %42 = load i32, ptr @opentop, align 4, !tbaa !21
  %43 = load ptr, ptr @slidemo, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mobj_s, ptr %43, i32 0, i32 3
  %45 = load i32, ptr %44, align 8, !tbaa !23
  %46 = sub nsw i32 %42, %45
  %47 = load ptr, ptr @slidemo, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mobj_s, ptr %47, i32 0, i32 15
  %49 = load i32, ptr %48, align 4, !tbaa !22
  %50 = icmp slt i32 %46, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %41
  br label %61

52:                                               ; preds = %41
  %53 = load i32, ptr @openbottom, align 4, !tbaa !21
  %54 = load ptr, ptr @slidemo, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mobj_s, ptr %54, i32 0, i32 3
  %56 = load i32, ptr %55, align 8, !tbaa !23
  %57 = sub nsw i32 %53, %56
  %58 = icmp sgt i32 %57, 1572864
  br i1 %58, label %59, label %60

59:                                               ; preds = %52
  br label %61

60:                                               ; preds = %52
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

61:                                               ; preds = %59, %51, %40, %32
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.intercept_t, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 8, !tbaa !24
  %65 = load i32, ptr @bestslidefrac, align 4, !tbaa !21
  %66 = icmp slt i32 %64, %65
  br i1 %66, label %67, label %74

67:                                               ; preds = %61
  %68 = load i32, ptr @bestslidefrac, align 4, !tbaa !21
  store i32 %68, ptr @secondslidefrac, align 4, !tbaa !21
  %69 = load ptr, ptr @bestslideline, align 8, !tbaa !5
  store ptr %69, ptr @secondslideline, align 8, !tbaa !5
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.intercept_t, ptr %70, i32 0, i32 0
  %72 = load i32, ptr %71, align 8, !tbaa !24
  store i32 %72, ptr @bestslidefrac, align 4, !tbaa !21
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %73, ptr @bestslideline, align 8, !tbaa !5
  br label %74

74:                                               ; preds = %67, %61
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %75

75:                                               ; preds = %74, %60, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %76 = load i32, ptr %2, align 4
  ret i32 %76
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { 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, !11, i64 4}
!10 = !{!"", !11, i64 0, !11, i64 4, !7, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !15, i64 24}
!14 = !{!"line_s", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !15, i64 24, !15, i64 26, !15, i64 28, !7, i64 30, !7, i64 36, !11, i64 52, !6, i64 56, !6, i64 64, !11, i64 72, !6, i64 80}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !11, i64 24}
!17 = !{!"mobj_s", !18, i64 0, !11, i64 24, !11, i64 28, !11, i64 32, !6, i64 40, !6, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !6, i64 72, !6, i64 80, !6, 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, !6, i64 136, !11, i64 144, !6, i64 152, !11, i64 160, !11, i64 164, !11, i64 168, !11, i64 172, !6, i64 176, !11, i64 184, !11, i64 188, !6, i64 192, !11, i64 200, !19, i64 204, !6, i64 216}
!18 = !{!"thinker_s", !6, i64 0, !6, i64 8, !7, i64 16}
!19 = !{!"", !15, i64 0, !15, i64 2, !15, i64 4, !15, i64 6, !15, i64 8}
!20 = !{!17, !11, i64 28}
!21 = !{!11, !11, i64 0}
!22 = !{!17, !11, i64 108}
!23 = !{!17, !11, i64 32}
!24 = !{!10, !11, i64 0}


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

@opentop = external local_unnamed_addr global i32, align 4
@openbottom = external local_unnamed_addr global i32, align 4
@slidemo = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@openrange = external local_unnamed_addr global i32, align 4
@bestslidefrac = external dso_local local_unnamed_addr global i32, align 4
@secondslidefrac = external dso_local local_unnamed_addr global i32, align 4
@bestslideline = external dso_local local_unnamed_addr global ptr, align 8
@secondslideline = external dso_local local_unnamed_addr global ptr, align 8

declare void @P_LineOpening(ptr noundef) local_unnamed_addr #0

declare i32 @P_PointOnLineSide(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @PTR_SlideTraverse(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %5

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

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !10
  %8 = getelementptr inbounds i8, ptr %7, i64 24
  %9 = load i16, ptr %8, align 8, !tbaa !11
  %10 = and i16 %9, 4
  %.not10 = icmp eq i16 %10, 0
  br i1 %.not10, label %11, label %18

11:                                               ; preds = %5
  %12 = load ptr, ptr @slidemo, align 8, !tbaa !15
  %13 = getelementptr inbounds i8, ptr %12, i64 24
  %14 = load i32, ptr %13, align 8, !tbaa !16
  %15 = getelementptr inbounds i8, ptr %12, i64 28
  %16 = load i32, ptr %15, align 4, !tbaa !20
  %17 = tail call i32 @P_PointOnLineSide(i32 noundef %14, i32 noundef %16, ptr noundef nonnull %7) #4
  %.not11 = icmp eq i32 %17, 0
  br i1 %.not11, label %34, label %41

18:                                               ; preds = %5
  tail call void @P_LineOpening(ptr noundef nonnull %7) #4
  %19 = load i32, ptr @openrange, align 4, !tbaa !21
  %20 = load ptr, ptr @slidemo, align 8, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %20, i64 108
  %22 = load i32, ptr %21, align 4, !tbaa !22
  %23 = icmp slt i32 %19, %22
  br i1 %23, label %34, label %24

24:                                               ; preds = %18
  %25 = load i32, ptr @opentop, align 4, !tbaa !21
  %26 = getelementptr inbounds i8, ptr %20, i64 32
  %27 = load i32, ptr %26, align 8, !tbaa !23
  %28 = sub nsw i32 %25, %27
  %29 = icmp slt i32 %28, %22
  br i1 %29, label %34, label %30

30:                                               ; preds = %24
  %31 = load i32, ptr @openbottom, align 4, !tbaa !21
  %32 = sub nsw i32 %31, %27
  %33 = icmp sgt i32 %32, 1572864
  br i1 %33, label %34, label %41

34:                                               ; preds = %30, %24, %18, %11
  %35 = load i32, ptr %0, align 8, !tbaa !24
  %36 = load i32, ptr @bestslidefrac, align 4, !tbaa !21
  %37 = icmp slt i32 %35, %36
  br i1 %37, label %38, label %41

38:                                               ; preds = %34
  store i32 %36, ptr @secondslidefrac, align 4, !tbaa !21
  %39 = load ptr, ptr @bestslideline, align 8, !tbaa !15
  store ptr %39, ptr @secondslideline, align 8, !tbaa !15
  %40 = load i32, ptr %0, align 8, !tbaa !24
  store i32 %40, ptr @bestslidefrac, align 4, !tbaa !21
  store ptr %7, ptr @bestslideline, align 8, !tbaa !15
  br label %41

41:                                               ; preds = %34, %38, %30, %11
  %.0 = phi i32 [ 1, %11 ], [ 1, %30 ], [ 0, %38 ], [ 0, %34 ]
  ret i32 %.0
}

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

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass