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

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

@ylookup = external dso_local global [200 x ptr], align 16
@columnofs = external dso_local global [320 x i32], align 16
@ds_x2 = external dso_local global i32, align 4
@ds_x1 = external dso_local global i32, align 4
@ds_y = external dso_local global i32, align 4
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@ds_xfrac = external dso_local global i32, align 4
@ds_yfrac = external dso_local global i32, align 4
@ds_source = external dso_local global ptr, align 8
@ds_colormap = external dso_local global ptr, align 8
@ds_xstep = external dso_local global i32, align 4
@ds_ystep = 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: noreturn
declare void @I_Error(ptr noundef, ...) #1

; 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
define dso_local void @R_DrawSpan() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %6 = load i32, ptr @ds_x2, align 4, !tbaa !5
  %7 = load i32, ptr @ds_x1, align 4, !tbaa !5
  %8 = icmp slt i32 %6, %7
  br i1 %8, label %18, label %9

9:                                                ; preds = %0
  %10 = load i32, ptr @ds_x1, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %18, label %12

12:                                               ; preds = %9
  %13 = load i32, ptr @ds_x2, align 4, !tbaa !5
  %14 = icmp sge i32 %13, 320
  br i1 %14, label %18, label %15

15:                                               ; preds = %12
  %16 = load i32, ptr @ds_y, align 4, !tbaa !5
  %17 = icmp ugt i32 %16, 200
  br i1 %17, label %18, label %22

18:                                               ; preds = %15, %12, %9, %0
  %19 = load i32, ptr @ds_x1, align 4, !tbaa !5
  %20 = load i32, ptr @ds_x2, align 4, !tbaa !5
  %21 = load i32, ptr @ds_y, align 4, !tbaa !5
  call void (ptr, ...) @I_Error(ptr noundef @.str.2, i32 noundef %19, i32 noundef %20, i32 noundef %21) #4
  unreachable

22:                                               ; preds = %15
  %23 = load i32, ptr @ds_x2, align 4, !tbaa !5
  %24 = load i32, ptr @ds_x1, align 4, !tbaa !5
  %25 = sub nsw i32 %23, %24
  store i32 %25, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %22
  store i32 1, ptr %5, align 4
  br label %50

29:                                               ; preds = %22
  %30 = load i32, ptr @ds_xfrac, align 4, !tbaa !5
  store i32 %30, ptr %1, align 4, !tbaa !5
  %31 = load i32, ptr @ds_yfrac, align 4, !tbaa !5
  store i32 %31, ptr %2, align 4, !tbaa !5
  %32 = load i32, ptr @ds_y, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [200 x ptr], ptr @ylookup, i64 0, i64 %33
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = load i32, ptr @ds_x1, align 4, !tbaa !5
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [320 x i32], ptr @columnofs, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %35, i64 %40
  store ptr %41, ptr %3, align 8, !tbaa !9
  %42 = load ptr, ptr %3, align 8, !tbaa !9
  %43 = load ptr, ptr @ds_source, align 8, !tbaa !9
  %44 = load ptr, ptr @ds_colormap, align 8, !tbaa !9
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = load i32, ptr @ds_xstep, align 4, !tbaa !5
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = load i32, ptr @ds_ystep, align 4, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !5
  call void @R_DrawSpanKernel(ptr noundef %42, ptr noundef %43, ptr noundef %44, i32 noundef %45, i32 noundef %46, i32 noundef %47, i32 noundef %48, i32 noundef %49)
  store i32 0, ptr %5, align 4
  br label %50

50:                                               ; preds = %29, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  %51 = load i32, ptr %5, align 4
  switch i32 %51, label %53 [
    i32 0, label %52
    i32 1, label %52
  ]

52:                                               ; preds = %50, %50
  ret void

53:                                               ; preds = %50
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden void @R_DrawSpanKernel(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = { nounwind }
attributes #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, 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"

@ylookup = external dso_local local_unnamed_addr global [200 x ptr], align 16
@columnofs = external dso_local local_unnamed_addr global [320 x i32], align 16
@ds_x2 = external dso_local local_unnamed_addr global i32, align 4
@ds_x1 = external dso_local local_unnamed_addr global i32, align 4
@ds_y = external dso_local local_unnamed_addr global i32, align 4
@.str.2 = external hidden unnamed_addr constant [27 x i8], align 1
@ds_xfrac = external dso_local local_unnamed_addr global i32, align 4
@ds_yfrac = external dso_local local_unnamed_addr global i32, align 4
@ds_source = external dso_local local_unnamed_addr global ptr, align 8
@ds_colormap = external dso_local local_unnamed_addr global ptr, align 8
@ds_xstep = external dso_local local_unnamed_addr global i32, align 4
@ds_ystep = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local void @R_DrawSpan() local_unnamed_addr #1 {
  %1 = load i32, ptr @ds_x2, align 4
  %2 = load i32, ptr @ds_x1, align 4
  %3 = icmp slt i32 %1, %2
  %4 = icmp slt i32 %2, 0
  %or.cond = or i1 %3, %4
  %5 = icmp sgt i32 %1, 319
  %or.cond3 = or i1 %5, %or.cond
  %6 = load i32, ptr @ds_y, align 4
  %7 = icmp ugt i32 %6, 200
  %or.cond5 = select i1 %or.cond3, i1 true, i1 %7
  br i1 %or.cond5, label %8, label %9

8:                                                ; preds = %0
  tail call void (ptr, ...) @I_Error(ptr noundef nonnull @.str.2, i32 noundef %2, i32 noundef %1, i32 noundef %6) #2
  unreachable

9:                                                ; preds = %0
  %10 = sub nsw i32 %1, %2
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %27, label %12

12:                                               ; preds = %9
  %13 = load i32, ptr @ds_xfrac, align 4, !tbaa !5
  %14 = load i32, ptr @ds_yfrac, align 4, !tbaa !5
  %15 = zext nneg i32 %6 to i64
  %16 = getelementptr inbounds [200 x ptr], ptr @ylookup, i64 0, i64 %15
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %18 = zext nneg i32 %2 to i64
  %19 = getelementptr inbounds [320 x i32], ptr @columnofs, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %17, i64 %21
  %23 = load ptr, ptr @ds_source, align 8, !tbaa !9
  %24 = load ptr, ptr @ds_colormap, align 8, !tbaa !9
  %25 = load i32, ptr @ds_xstep, align 4, !tbaa !5
  %26 = load i32, ptr @ds_ystep, align 4, !tbaa !5
  tail call void @R_DrawSpanKernel(ptr noundef %22, ptr noundef %23, ptr noundef %24, i32 noundef %13, i32 noundef %25, i32 noundef %14, i32 noundef %26, i32 noundef %10)
  br label %27

27:                                               ; preds = %9, %12
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @R_DrawSpanKernel(ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { 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 #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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}


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