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

@translationtables = 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

; Function Attrs: nounwind uwtable
define dso_local void @R_InitTranslationTables() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %2 = call ptr @Z_Malloc(i32 noundef 1023, i32 noundef 1, ptr noundef null)
  store ptr %2, ptr @translationtables, align 8, !tbaa !5
  %3 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %4 = ptrtoint ptr %3 to i64
  %5 = add i64 %4, 255
  %6 = and i64 %5, -256
  %7 = inttoptr i64 %6 to ptr
  store ptr %7, ptr @translationtables, align 8, !tbaa !5
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %62, %0
  %9 = load i32, ptr %1, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 256
  br i1 %10, label %11, label %65

11:                                               ; preds = %8
  %12 = load i32, ptr %1, align 4, !tbaa !9
  %13 = icmp sge i32 %12, 112
  br i1 %13, label %14, label %44

14:                                               ; preds = %11
  %15 = load i32, ptr %1, align 4, !tbaa !9
  %16 = icmp sle i32 %15, 127
  br i1 %16, label %17, label %44

17:                                               ; preds = %14
  %18 = load i32, ptr %1, align 4, !tbaa !9
  %19 = and i32 %18, 15
  %20 = add nsw i32 96, %19
  %21 = trunc i32 %20 to i8
  %22 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %23 = load i32, ptr %1, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %22, i64 %24
  store i8 %21, ptr %25, align 1, !tbaa !11
  %26 = load i32, ptr %1, align 4, !tbaa !9
  %27 = and i32 %26, 15
  %28 = add nsw i32 64, %27
  %29 = trunc i32 %28 to i8
  %30 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %31 = load i32, ptr %1, align 4, !tbaa !9
  %32 = add nsw i32 %31, 256
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %30, i64 %33
  store i8 %29, ptr %34, align 1, !tbaa !11
  %35 = load i32, ptr %1, align 4, !tbaa !9
  %36 = and i32 %35, 15
  %37 = add nsw i32 32, %36
  %38 = trunc i32 %37 to i8
  %39 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %40 = load i32, ptr %1, align 4, !tbaa !9
  %41 = add nsw i32 %40, 512
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %39, i64 %42
  store i8 %38, ptr %43, align 1, !tbaa !11
  br label %61

44:                                               ; preds = %14, %11
  %45 = load i32, ptr %1, align 4, !tbaa !9
  %46 = trunc i32 %45 to i8
  %47 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %48 = load i32, ptr %1, align 4, !tbaa !9
  %49 = add nsw i32 %48, 512
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %47, i64 %50
  store i8 %46, ptr %51, align 1, !tbaa !11
  %52 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %53 = load i32, ptr %1, align 4, !tbaa !9
  %54 = add nsw i32 %53, 256
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %52, i64 %55
  store i8 %46, ptr %56, align 1, !tbaa !11
  %57 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %58 = load i32, ptr %1, align 4, !tbaa !9
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds i8, ptr %57, i64 %59
  store i8 %46, ptr %60, align 1, !tbaa !11
  br label %61

61:                                               ; preds = %44, %17
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %1, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %1, align 4, !tbaa !9
  br label %8, !llvm.loop !12

65:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr noundef) #2

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 = { "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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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

@translationtables = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @R_InitTranslationTables() local_unnamed_addr #0 {
  %1 = tail call ptr @Z_Malloc(i32 noundef 1023, i32 noundef 1, ptr noundef null) #2
  %2 = ptrtoint ptr %1 to i64
  %3 = add i64 %2, 255
  %4 = and i64 %3, -256
  %5 = inttoptr i64 %4 to ptr
  store ptr %5, ptr @translationtables, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %0, %31
  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %31 ]
  %7 = and i64 %indvars.iv, 240
  %or.cond = icmp eq i64 %7, 112
  %8 = trunc i64 %indvars.iv to i8
  br i1 %or.cond, label %9, label %22

9:                                                ; preds = %6
  %10 = and i8 %8, 15
  %11 = or disjoint i8 %10, 96
  %12 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 %indvars.iv
  store i8 %11, ptr %13, align 1, !tbaa !9
  %14 = or disjoint i8 %10, 64
  %15 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %16 = or disjoint i64 %indvars.iv, 256
  %17 = getelementptr inbounds i8, ptr %15, i64 %16
  store i8 %14, ptr %17, align 1, !tbaa !9
  %18 = or disjoint i8 %10, 32
  %19 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %20 = or disjoint i64 %indvars.iv, 512
  %21 = getelementptr inbounds i8, ptr %19, i64 %20
  store i8 %18, ptr %21, align 1, !tbaa !9
  br label %31

22:                                               ; preds = %6
  %23 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %24 = or disjoint i64 %indvars.iv, 512
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  store i8 %8, ptr %25, align 1, !tbaa !9
  %26 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %27 = or disjoint i64 %indvars.iv, 256
  %28 = getelementptr inbounds i8, ptr %26, i64 %27
  store i8 %8, ptr %28, align 1, !tbaa !9
  %29 = load ptr, ptr @translationtables, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 %indvars.iv
  store i8 %8, ptr %30, align 1, !tbaa !9
  br label %31

31:                                               ; preds = %9, %22
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, 256
  br i1 %exitcond.not, label %32, label %6, !llvm.loop !10

32:                                               ; preds = %31
  ret void
}

declare ptr @Z_Malloc(i32 noundef, i32 noundef, ptr 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass