; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/FIRMWARE/LIBFEMTOGL (max7219_text)

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

@buff = external hidden global [16 x i8], align 16
@font_8x8 = external global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @MAX7219_shift() #0

; 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @MAX7219_putchar(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = icmp eq i32 %6, 10
  br i1 %7, label %8, label %12

8:                                                ; preds = %1
  %9 = call i32 @MAX7219_putchar(i32 noundef 32)
  %10 = call i32 @MAX7219_putchar(i32 noundef 32)
  %11 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %11, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %49

12:                                               ; preds = %1
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 13
  br i1 %14, label %15, label %19

15:                                               ; preds = %12
  %16 = call i32 @MAX7219_putchar(i32 noundef 32)
  %17 = call i32 @MAX7219_putchar(i32 noundef 32)
  %18 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %18, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %49

19:                                               ; preds = %12
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %36, %19
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = icmp slt i32 %21, 8
  br i1 %22, label %23, label %39

23:                                               ; preds = %20
  %24 = load ptr, ptr @font_8x8, align 8, !tbaa !9
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = mul nsw i32 8, %25
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = add nsw i32 %26, %27
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %24, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = add nsw i32 8, %32
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [16 x i8], ptr @buff, i64 0, i64 %34
  store i8 %31, ptr %35, align 1, !tbaa !11
  br label %36

36:                                               ; preds = %23
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %4, align 4, !tbaa !5
  br label %20, !llvm.loop !12

39:                                               ; preds = %20
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %44, %39
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = icmp slt i32 %41, 8
  br i1 %42, label %43, label %47

43:                                               ; preds = %40
  call void @MAX7219_shift()
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %4, align 4, !tbaa !5
  br label %40, !llvm.loop !14

47:                                               ; preds = %40
  %48 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %48, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %49

49:                                               ; preds = %47, %15, %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %50 = load i32, ptr %2, align 4
  ret i32 %50
}

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 }

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


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

@buff = external hidden local_unnamed_addr global [16 x i8], align 16
@font_8x8 = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @MAX7219_shift() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @MAX7219_putchar(i32 noundef returned %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @font_8x8, align 8, !tbaa !5
  switch i32 %0, label %.preheader24 [
    i32 10, label %.preheader28
    i32 13, label %.preheader32
  ]

.preheader32:                                     ; preds = %1
  %3 = getelementptr inbounds i8, ptr %2, i64 256
  %4 = load <8 x i8>, ptr %3, align 1, !tbaa !9
  store <8 x i8> %4, ptr getelementptr inbounds (i8, ptr @buff, i64 8), align 8, !tbaa !9
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  %5 = load ptr, ptr @font_8x8, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 256
  %7 = load <4 x i8>, ptr %6, align 1, !tbaa !9
  store <4 x i8> %7, ptr getelementptr inbounds (i8, ptr @buff, i64 8), align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %5, i64 260
  %9 = load i8, ptr %8, align 1, !tbaa !9
  store i8 %9, ptr getelementptr inbounds (i8, ptr @buff, i64 12), align 4, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %5, i64 261
  %11 = load i8, ptr %10, align 1, !tbaa !9
  store i8 %11, ptr getelementptr inbounds (i8, ptr @buff, i64 13), align 1, !tbaa !9
  %12 = getelementptr inbounds i8, ptr %5, i64 262
  %13 = load i8, ptr %12, align 1, !tbaa !9
  store i8 %13, ptr getelementptr inbounds (i8, ptr @buff, i64 14), align 2, !tbaa !9
  %14 = getelementptr inbounds i8, ptr %5, i64 263
  br label %MAX7219_putchar.exit17

.preheader28:                                     ; preds = %1
  %15 = getelementptr inbounds i8, ptr %2, i64 256
  %16 = load <8 x i8>, ptr %15, align 1, !tbaa !9
  store <8 x i8> %16, ptr getelementptr inbounds (i8, ptr @buff, i64 8), align 8, !tbaa !9
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  %17 = load ptr, ptr @font_8x8, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 256
  %19 = load <4 x i8>, ptr %18, align 1, !tbaa !9
  store <4 x i8> %19, ptr getelementptr inbounds (i8, ptr @buff, i64 8), align 8, !tbaa !9
  %20 = getelementptr inbounds i8, ptr %17, i64 260
  %21 = load i8, ptr %20, align 1, !tbaa !9
  store i8 %21, ptr getelementptr inbounds (i8, ptr @buff, i64 12), align 4, !tbaa !9
  %22 = getelementptr inbounds i8, ptr %17, i64 261
  %23 = load i8, ptr %22, align 1, !tbaa !9
  store i8 %23, ptr getelementptr inbounds (i8, ptr @buff, i64 13), align 1, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %17, i64 262
  %25 = load i8, ptr %24, align 1, !tbaa !9
  store i8 %25, ptr getelementptr inbounds (i8, ptr @buff, i64 14), align 2, !tbaa !9
  %26 = getelementptr inbounds i8, ptr %17, i64 263
  br label %MAX7219_putchar.exit17

.preheader24:                                     ; preds = %1
  %27 = shl nsw i32 %0, 3
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %2, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !9
  store i8 %30, ptr getelementptr inbounds (i8, ptr @buff, i64 8), align 8, !tbaa !9
  %31 = or disjoint i64 %28, 1
  %32 = getelementptr inbounds i8, ptr %2, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !9
  store i8 %33, ptr getelementptr inbounds (i8, ptr @buff, i64 9), align 1, !tbaa !9
  %34 = or disjoint i64 %28, 2
  %35 = getelementptr inbounds i8, ptr %2, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !9
  store i8 %36, ptr getelementptr inbounds (i8, ptr @buff, i64 10), align 2, !tbaa !9
  %37 = or disjoint i64 %28, 3
  %38 = getelementptr inbounds i8, ptr %2, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !9
  store i8 %39, ptr getelementptr inbounds (i8, ptr @buff, i64 11), align 1, !tbaa !9
  %40 = or disjoint i64 %28, 4
  %41 = getelementptr inbounds i8, ptr %2, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !9
  store i8 %42, ptr getelementptr inbounds (i8, ptr @buff, i64 12), align 4, !tbaa !9
  %43 = or disjoint i64 %28, 5
  %44 = getelementptr inbounds i8, ptr %2, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !9
  store i8 %45, ptr getelementptr inbounds (i8, ptr @buff, i64 13), align 1, !tbaa !9
  %46 = or disjoint i64 %28, 6
  %47 = getelementptr inbounds i8, ptr %2, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !9
  store i8 %48, ptr getelementptr inbounds (i8, ptr @buff, i64 14), align 2, !tbaa !9
  %49 = or disjoint i64 %28, 7
  %50 = getelementptr inbounds i8, ptr %2, i64 %49
  br label %MAX7219_putchar.exit17

MAX7219_putchar.exit17:                           ; preds = %.preheader32, %.preheader28, %.preheader24
  %.sink50 = phi ptr [ %14, %.preheader32 ], [ %26, %.preheader28 ], [ %50, %.preheader24 ]
  %51 = load i8, ptr %.sink50, align 1, !tbaa !9
  store i8 %51, ptr getelementptr inbounds (i8, ptr @buff, i64 15), align 1, !tbaa !9
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  tail call void @MAX7219_shift()
  ret i32 %0
}

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

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


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InlinerPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass