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

@tmthing = external dso_local global ptr, align 8
@tmx = external dso_local global i32, align 4
@tmy = external dso_local global i32, align 4
@gamemap = external global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @PIT_StompThing(ptr noundef %0) #0 {
  %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) #4
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.mobj_s, ptr %6, i32 0, i32 24
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = and i32 %8, 4
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %12, label %11

11:                                               ; preds = %1
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mobj_s, ptr %13, i32 0, i32 14
  %15 = load i32, ptr %14, align 8, !tbaa !15
  %16 = load ptr, ptr @tmthing, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mobj_s, ptr %16, i32 0, i32 14
  %18 = load i32, ptr %17, align 8, !tbaa !15
  %19 = add nsw i32 %15, %18
  store i32 %19, ptr %4, align 4, !tbaa !16
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mobj_s, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 8, !tbaa !17
  %23 = load i32, ptr @tmx, align 4, !tbaa !16
  %24 = sub nsw i32 %22, %23
  %25 = call i32 @llvm.abs.i32(i32 %24, i1 true)
  %26 = load i32, ptr %4, align 4, !tbaa !16
  %27 = icmp sge i32 %25, %26
  br i1 %27, label %37, label %28

28:                                               ; preds = %12
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mobj_s, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 4, !tbaa !18
  %32 = load i32, ptr @tmy, align 4, !tbaa !16
  %33 = sub nsw i32 %31, %32
  %34 = call i32 @llvm.abs.i32(i32 %33, i1 true)
  %35 = load i32, ptr %4, align 4, !tbaa !16
  %36 = icmp sge i32 %34, %35
  br i1 %36, label %37, label %38

37:                                               ; preds = %28, %12
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

38:                                               ; preds = %28
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = load ptr, ptr @tmthing, align 8, !tbaa !5
  %41 = icmp eq ptr %39, %40
  br i1 %41, label %42, label %43

42:                                               ; preds = %38
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

43:                                               ; preds = %38
  %44 = load ptr, ptr @tmthing, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.mobj_s, ptr %44, i32 0, i32 31
  %46 = load ptr, ptr %45, align 8, !tbaa !19
  %47 = icmp ne ptr %46, null
  br i1 %47, label %52, label %48

48:                                               ; preds = %43
  %49 = load i32, ptr @gamemap, align 4, !tbaa !16
  %50 = icmp ne i32 %49, 30
  br i1 %50, label %51, label %52

51:                                               ; preds = %48
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

52:                                               ; preds = %48, %43
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = load ptr, ptr @tmthing, align 8, !tbaa !5
  %55 = load ptr, ptr @tmthing, align 8, !tbaa !5
  call void @P_DamageMobj(ptr noundef %53, ptr noundef %54, ptr noundef %55, i32 noundef 10000)
  store i32 1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %56

56:                                               ; preds = %52, %51, %42, %37, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %57 = load i32, ptr %2, align 4
  ret i32 %57
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

declare void @P_DamageMobj(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #3

; 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 }

!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 160}
!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 104}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !12, i64 24}
!18 = !{!10, !12, i64 28}
!19 = !{!10, !6, i64 192}


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

@tmthing = external dso_local local_unnamed_addr global ptr, align 8
@tmx = external dso_local local_unnamed_addr global i32, align 4
@tmy = external dso_local local_unnamed_addr global i32, align 4
@gamemap = external local_unnamed_addr global i32, align 4

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

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 104
  %7 = load i32, ptr %6, align 8, !tbaa !14
  %8 = load ptr, ptr @tmthing, align 8, !tbaa !15
  %9 = getelementptr inbounds i8, ptr %8, i64 104
  %10 = load i32, ptr %9, align 8, !tbaa !14
  %11 = add nsw i32 %10, %7
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  %13 = load i32, ptr %12, align 8, !tbaa !16
  %14 = load i32, ptr @tmx, align 4, !tbaa !17
  %15 = sub nsw i32 %13, %14
  %16 = tail call i32 @llvm.abs.i32(i32 %15, i1 true)
  %.not12 = icmp slt i32 %16, %11
  br i1 %.not12, label %17, label %31

17:                                               ; preds = %5
  %18 = getelementptr inbounds i8, ptr %0, i64 28
  %19 = load i32, ptr %18, align 4, !tbaa !18
  %20 = load i32, ptr @tmy, align 4, !tbaa !17
  %21 = sub nsw i32 %19, %20
  %22 = tail call i32 @llvm.abs.i32(i32 %21, i1 true)
  %.not13 = icmp uge i32 %22, %11
  %23 = icmp eq ptr %8, %0
  %or.cond14 = or i1 %23, %.not13
  br i1 %or.cond14, label %31, label %24

24:                                               ; preds = %17
  %25 = getelementptr inbounds i8, ptr %8, i64 192
  %26 = load ptr, ptr %25, align 8, !tbaa !19
  %27 = icmp eq ptr %26, null
  %28 = load i32, ptr @gamemap, align 4
  %29 = icmp ne i32 %28, 30
  %or.cond = select i1 %27, i1 %29, i1 false
  br i1 %or.cond, label %31, label %30

30:                                               ; preds = %24
  tail call void @P_DamageMobj(ptr noundef nonnull %0, ptr noundef nonnull %8, ptr noundef nonnull %8, i32 noundef 10000) #3
  br label %31

31:                                               ; preds = %24, %5, %17, %1, %30
  %.0 = phi i32 [ 1, %30 ], [ 1, %1 ], [ 1, %17 ], [ 1, %5 ], [ 0, %24 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

declare void @P_DamageMobj(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 }

!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 160}
!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 104}
!15 = !{!8, !8, i64 0}
!16 = !{!6, !11, i64 24}
!17 = !{!11, !11, i64 0}
!18 = !{!6, !11, i64 28}
!19 = !{!6, !8, i64 192}


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