; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (am_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.mline_t = type { %struct.mpoint_t, %struct.mpoint_t }
%struct.mpoint_t = type { i32, i32 }
%struct.islope_t = type { i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local void @AM_getIslope(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.mline_t, ptr %7, i32 0, i32 0
  %9 = getelementptr inbounds %struct.mpoint_t, ptr %8, i32 0, i32 1
  %10 = load i32, ptr %9, align 4, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mline_t, ptr %11, i32 0, i32 1
  %13 = getelementptr inbounds %struct.mpoint_t, ptr %12, i32 0, i32 1
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = sub nsw i32 %10, %14
  store i32 %15, ptr %6, align 4, !tbaa !14
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mline_t, ptr %16, i32 0, i32 1
  %18 = getelementptr inbounds %struct.mpoint_t, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !15
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mline_t, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.mpoint_t, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %22, align 4, !tbaa !16
  %24 = sub nsw i32 %19, %23
  store i32 %24, ptr %5, align 4, !tbaa !14
  %25 = load i32, ptr %6, align 4, !tbaa !14
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %34, label %27

27:                                               ; preds = %2
  %28 = load i32, ptr %5, align 4, !tbaa !14
  %29 = icmp slt i32 %28, 0
  %30 = zext i1 %29 to i64
  %31 = select i1 %29, i32 -2147483647, i32 2147483647
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.islope_t, ptr %32, i32 0, i32 1
  store i32 %31, ptr %33, align 4, !tbaa !17
  br label %40

34:                                               ; preds = %2
  %35 = load i32, ptr %5, align 4, !tbaa !14
  %36 = load i32, ptr %6, align 4, !tbaa !14
  %37 = call i32 @FixedDiv(i32 noundef %35, i32 noundef %36)
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.islope_t, ptr %38, i32 0, i32 1
  store i32 %37, ptr %39, align 4, !tbaa !17
  br label %40

40:                                               ; preds = %34, %27
  %41 = load i32, ptr %5, align 4, !tbaa !14
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %50, label %43

43:                                               ; preds = %40
  %44 = load i32, ptr %6, align 4, !tbaa !14
  %45 = icmp slt i32 %44, 0
  %46 = zext i1 %45 to i64
  %47 = select i1 %45, i32 -2147483647, i32 2147483647
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.islope_t, ptr %48, i32 0, i32 0
  store i32 %47, ptr %49, align 4, !tbaa !18
  br label %56

50:                                               ; preds = %40
  %51 = load i32, ptr %6, align 4, !tbaa !14
  %52 = load i32, ptr %5, align 4, !tbaa !14
  %53 = call i32 @FixedDiv(i32 noundef %51, i32 noundef %52)
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.islope_t, ptr %54, i32 0, i32 0
  store i32 %53, ptr %55, align 4, !tbaa !18
  br label %56

56:                                               ; preds = %50, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

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

declare i32 @FixedDiv(i32 noundef, i32 noundef) #2

; 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 = { "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, !12, i64 4}
!10 = !{!"", !11, i64 0, !11, i64 8}
!11 = !{!"", !12, i64 0, !12, i64 4}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 12}
!14 = !{!12, !12, i64 0}
!15 = !{!10, !12, i64 8}
!16 = !{!10, !12, i64 0}
!17 = !{!11, !12, i64 4}
!18 = !{!11, !12, 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"

; Function Attrs: nounwind uwtable
define dso_local void @AM_getIslope(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = sub nsw i32 %4, %7
  %9 = load i32, ptr %5, align 4, !tbaa !12
  %10 = load i32, ptr %0, align 4, !tbaa !13
  %11 = sub nsw i32 %9, %10
  %.not = icmp eq i32 %4, %7
  br i1 %.not, label %12, label %15

12:                                               ; preds = %2
  %13 = icmp slt i32 %11, 0
  %14 = select i1 %13, i32 -2147483647, i32 2147483647
  br label %17

15:                                               ; preds = %2
  %16 = tail call i32 @FixedDiv(i32 noundef %11, i32 noundef %8) #2
  br label %17

17:                                               ; preds = %15, %12
  %.sink = phi i32 [ %14, %12 ], [ %16, %15 ]
  %18 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 %.sink, ptr %18, align 4
  %.not16 = icmp eq i32 %9, %10
  br i1 %.not16, label %19, label %22

19:                                               ; preds = %17
  %20 = icmp slt i32 %8, 0
  %21 = select i1 %20, i32 -2147483647, i32 2147483647
  br label %24

22:                                               ; preds = %17
  %23 = tail call i32 @FixedDiv(i32 noundef %8, i32 noundef %11) #2
  br label %24

24:                                               ; preds = %22, %19
  %storemerge = phi i32 [ %21, %19 ], [ %23, %22 ]
  store i32 %storemerge, ptr %1, align 4, !tbaa !14
  ret void
}

declare i32 @FixedDiv(i32 noundef, i32 noundef) local_unnamed_addr #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 = { "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, !8, i64 4}
!6 = !{!"", !7, i64 0, !7, i64 8}
!7 = !{!"", !8, i64 0, !8, i64 4}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!6, !8, i64 12}
!12 = !{!6, !8, i64 8}
!13 = !{!6, !8, i64 0}
!14 = !{!7, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass