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

; 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.hu_textline_t = type { i32, i32, ptr, i32, [81 x i8], i32, i32 }
%struct.patch_t = type { i16, i16, i16, i16, [8 x i32] }

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

14:                                               ; preds = %99, %2
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.hu_textline_t, ptr %16, i32 0, i32 5
  %18 = load i32, ptr %17, align 8, !tbaa !13
  %19 = icmp slt i32 %15, %18
  br i1 %19, label %20, label %102

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %21 = call ptr @__ctype_toupper_loc() #5
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.hu_textline_t, ptr %23, i32 0, i32 4
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [81 x i8], ptr %24, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !14
  %29 = sext i8 %28 to i32
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i32, ptr %22, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !9
  store i32 %32, ptr %9, align 4, !tbaa !9
  %33 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %33, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  %34 = load i32, ptr %10, align 4, !tbaa !9
  %35 = trunc i32 %34 to i8
  store i8 %35, ptr %8, align 1, !tbaa !14
  %36 = load i8, ptr %8, align 1, !tbaa !14
  %37 = zext i8 %36 to i32
  %38 = icmp ne i32 %37, 32
  br i1 %38, label %39, label %91

39:                                               ; preds = %20
  %40 = load i8, ptr %8, align 1, !tbaa !14
  %41 = zext i8 %40 to i32
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.hu_textline_t, ptr %42, i32 0, i32 3
  %44 = load i32, ptr %43, align 8, !tbaa !15
  %45 = icmp sge i32 %41, %44
  br i1 %45, label %46, label %91

46:                                               ; preds = %39
  %47 = load i8, ptr %8, align 1, !tbaa !14
  %48 = zext i8 %47 to i32
  %49 = icmp sle i32 %48, 95
  br i1 %49, label %50, label %91

50:                                               ; preds = %46
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.hu_textline_t, ptr %51, i32 0, i32 2
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = load i8, ptr %8, align 1, !tbaa !14
  %55 = zext i8 %54 to i32
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.hu_textline_t, ptr %56, i32 0, i32 3
  %58 = load i32, ptr %57, align 8, !tbaa !15
  %59 = sub nsw i32 %55, %58
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds ptr, ptr %53, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.patch_t, ptr %62, i32 0, i32 0
  %64 = load i16, ptr %63, align 4, !tbaa !17
  %65 = sext i16 %64 to i32
  store i32 %65, ptr %6, align 4, !tbaa !9
  %66 = load i32, ptr %7, align 4, !tbaa !9
  %67 = load i32, ptr %6, align 4, !tbaa !9
  %68 = add nsw i32 %66, %67
  %69 = icmp sgt i32 %68, 320
  br i1 %69, label %70, label %71

70:                                               ; preds = %50
  br label %102

71:                                               ; preds = %50
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.hu_textline_t, ptr %73, i32 0, i32 1
  %75 = load i32, ptr %74, align 4, !tbaa !20
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.hu_textline_t, ptr %76, i32 0, i32 2
  %78 = load ptr, ptr %77, align 8, !tbaa !16
  %79 = load i8, ptr %8, align 1, !tbaa !14
  %80 = zext i8 %79 to i32
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.hu_textline_t, ptr %81, i32 0, i32 3
  %83 = load i32, ptr %82, align 8, !tbaa !15
  %84 = sub nsw i32 %80, %83
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds ptr, ptr %78, i64 %85
  %87 = load ptr, ptr %86, align 8, !tbaa !5
  call void @V_DrawPatchDirect(i32 noundef %72, i32 noundef %75, i32 noundef 0, ptr noundef %87)
  %88 = load i32, ptr %6, align 4, !tbaa !9
  %89 = load i32, ptr %7, align 4, !tbaa !9
  %90 = add nsw i32 %89, %88
  store i32 %90, ptr %7, align 4, !tbaa !9
  br label %98

91:                                               ; preds = %46, %39, %20
  %92 = load i32, ptr %7, align 4, !tbaa !9
  %93 = add nsw i32 %92, 4
  store i32 %93, ptr %7, align 4, !tbaa !9
  %94 = load i32, ptr %7, align 4, !tbaa !9
  %95 = icmp sge i32 %94, 320
  br i1 %95, label %96, label %97

96:                                               ; preds = %91
  br label %102

97:                                               ; preds = %91
  br label %98

98:                                               ; preds = %97, %71
  br label %99

99:                                               ; preds = %98
  %100 = load i32, ptr %5, align 4, !tbaa !9
  %101 = add nsw i32 %100, 1
  store i32 %101, ptr %5, align 4, !tbaa !9
  br label %14, !llvm.loop !21

102:                                              ; preds = %96, %70, %14
  %103 = load i32, ptr %4, align 4, !tbaa !9
  %104 = icmp ne i32 %103, 0
  br i1 %104, label %105, label %137

105:                                              ; preds = %102
  %106 = load i32, ptr %7, align 4, !tbaa !9
  %107 = load ptr, ptr %3, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.hu_textline_t, ptr %107, i32 0, i32 2
  %109 = load ptr, ptr %108, align 8, !tbaa !16
  %110 = load ptr, ptr %3, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.hu_textline_t, ptr %110, i32 0, i32 3
  %112 = load i32, ptr %111, align 8, !tbaa !15
  %113 = sub nsw i32 95, %112
  %114 = sext i32 %113 to i64
  %115 = getelementptr inbounds ptr, ptr %109, i64 %114
  %116 = load ptr, ptr %115, align 8, !tbaa !5
  %117 = getelementptr inbounds %struct.patch_t, ptr %116, i32 0, i32 0
  %118 = load i16, ptr %117, align 4, !tbaa !17
  %119 = sext i16 %118 to i32
  %120 = add nsw i32 %106, %119
  %121 = icmp sle i32 %120, 320
  br i1 %121, label %122, label %137

122:                                              ; preds = %105
  %123 = load i32, ptr %7, align 4, !tbaa !9
  %124 = load ptr, ptr %3, align 8, !tbaa !5
  %125 = getelementptr inbounds %struct.hu_textline_t, ptr %124, i32 0, i32 1
  %126 = load i32, ptr %125, align 4, !tbaa !20
  %127 = load ptr, ptr %3, align 8, !tbaa !5
  %128 = getelementptr inbounds %struct.hu_textline_t, ptr %127, i32 0, i32 2
  %129 = load ptr, ptr %128, align 8, !tbaa !16
  %130 = load ptr, ptr %3, align 8, !tbaa !5
  %131 = getelementptr inbounds %struct.hu_textline_t, ptr %130, i32 0, i32 3
  %132 = load i32, ptr %131, align 8, !tbaa !15
  %133 = sub nsw i32 95, %132
  %134 = sext i32 %133 to i64
  %135 = getelementptr inbounds ptr, ptr %129, i64 %134
  %136 = load ptr, ptr %135, align 8, !tbaa !5
  call void @V_DrawPatchDirect(i32 noundef %123, i32 noundef %126, i32 noundef 0, ptr noundef %136)
  br label %137

137:                                              ; preds = %122, %105, %102
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_toupper_loc() #2

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

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

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 = { nounwind willreturn memory(none) "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 = { "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 = { nounwind willreturn memory(none) }

!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 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8, !10, i64 16, !7, i64 20, !10, i64 104, !10, i64 108}
!13 = !{!12, !10, i64 104}
!14 = !{!7, !7, i64 0}
!15 = !{!12, !10, i64 16}
!16 = !{!12, !6, i64 8}
!17 = !{!18, !19, i64 0}
!18 = !{!"", !19, i64 0, !19, i64 2, !19, i64 4, !19, i64 6, !7, i64 8}
!19 = !{!"short", !7, i64 0}
!20 = !{!12, !10, i64 4}
!21 = distinct !{!21, !22}
!22 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local void @HUlib_drawTextLine(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = getelementptr inbounds i8, ptr %0, i64 104
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %8 = tail call ptr @__ctype_toupper_loc() #3
  %9 = getelementptr inbounds i8, ptr %0, i64 20
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = getelementptr inbounds i8, ptr %0, i64 4
  br label %12

12:                                               ; preds = %.lr.ph, %40
  %13 = phi i32 [ %6, %.lr.ph ], [ %41, %40 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %40 ]
  %.03542 = phi i32 [ %3, %.lr.ph ], [ %.1, %40 ]
  %14 = load ptr, ptr %8, align 8, !tbaa !12
  %15 = getelementptr inbounds [81 x i8], ptr %9, i64 0, i64 %indvars.iv
  %16 = load i8, ptr %15, align 1, !tbaa !13
  %17 = sext i8 %16 to i64
  %18 = getelementptr inbounds i32, ptr %14, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !14
  %20 = and i32 %19, 255
  %.not = icmp eq i32 %20, 32
  br i1 %.not, label %37, label %21

21:                                               ; preds = %12
  %22 = load i32, ptr %10, align 8, !tbaa !15
  %23 = icmp sge i32 %20, %22
  %24 = icmp ult i32 %20, 96
  %or.cond = and i1 %24, %23
  br i1 %or.cond, label %25, label %37

25:                                               ; preds = %21
  %26 = load ptr, ptr %4, align 8, !tbaa !16
  %27 = sub nsw i32 %20, %22
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds ptr, ptr %26, i64 %28
  %30 = load ptr, ptr %29, align 8, !tbaa !12
  %31 = load i16, ptr %30, align 4, !tbaa !17
  %32 = sext i16 %31 to i32
  %33 = add nsw i32 %.03542, %32
  %34 = icmp sgt i32 %33, 320
  br i1 %34, label %._crit_edge, label %35

35:                                               ; preds = %25
  %36 = load i32, ptr %11, align 4, !tbaa !20
  tail call void @V_DrawPatchDirect(i32 noundef %.03542, i32 noundef %36, i32 noundef 0, ptr noundef nonnull %30) #4
  %.pre = load i32, ptr %5, align 8, !tbaa !11
  br label %40

37:                                               ; preds = %21, %12
  %38 = add nsw i32 %.03542, 4
  %39 = icmp sgt i32 %.03542, 315
  br i1 %39, label %._crit_edge, label %40

40:                                               ; preds = %35, %37
  %41 = phi i32 [ %.pre, %35 ], [ %13, %37 ]
  %.1 = phi i32 [ %33, %35 ], [ %38, %37 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %42 = sext i32 %41 to i64
  %43 = icmp slt i64 %indvars.iv.next, %42
  br i1 %43, label %12, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %40, %25, %37, %2
  %.2 = phi i32 [ %3, %2 ], [ %38, %37 ], [ %.03542, %25 ], [ %.1, %40 ]
  %.not41 = icmp eq i32 %1, 0
  br i1 %.not41, label %59, label %44

44:                                               ; preds = %._crit_edge
  %45 = load ptr, ptr %4, align 8, !tbaa !16
  %46 = getelementptr inbounds i8, ptr %0, i64 16
  %47 = load i32, ptr %46, align 8, !tbaa !15
  %48 = sub nsw i32 95, %47
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds ptr, ptr %45, i64 %49
  %51 = load ptr, ptr %50, align 8, !tbaa !12
  %52 = load i16, ptr %51, align 4, !tbaa !17
  %53 = sext i16 %52 to i32
  %54 = add nsw i32 %.2, %53
  %55 = icmp slt i32 %54, 321
  br i1 %55, label %56, label %59

56:                                               ; preds = %44
  %57 = getelementptr inbounds i8, ptr %0, i64 4
  %58 = load i32, ptr %57, align 4, !tbaa !20
  tail call void @V_DrawPatchDirect(i32 noundef %.2, i32 noundef %58, i32 noundef 0, ptr noundef nonnull %51) #4
  br label %59

59:                                               ; preds = %56, %44, %._crit_edge
  ret void
}

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_toupper_loc() local_unnamed_addr #1

declare void @V_DrawPatchDirect(i32 noundef, i32 noundef, i32 noundef, ptr 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 nofree nosync nounwind willreturn memory(none) "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 willreturn memory(none) }
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 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !8, i64 20, !7, i64 104, !7, i64 108}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 104}
!12 = !{!10, !10, i64 0}
!13 = !{!8, !8, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !7, i64 16}
!16 = !{!6, !10, i64 8}
!17 = !{!18, !19, i64 0}
!18 = !{!"", !19, i64 0, !19, i64 2, !19, i64 4, !19, i64 6, !8, i64 8}
!19 = !{!"short", !8, i64 0}
!20 = !{!6, !7, i64 4}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass