; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/TUTORIALS/FROM_BLINKER_TO_RISCV/FIRMWARE (riscv_logo)

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

@logo = external dso_local global [16 x [16 x i8]], align 16
@cmap = external dso_local global [8 x ptr], align 16
@sintab = external dso_local global [64 x i32], align 16
@.str.6 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [7 x i8], align 1

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

16:                                               ; preds = %109, %0
  %17 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = and i32 %18, 63
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [64 x i32], ptr @sintab, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, 400
  %24 = shl i32 %23, 1
  store i32 %24, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = and i32 %26, 63
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [64 x i32], ptr @sintab, i64 0, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = mul nsw i32 %25, %30
  store i32 %31, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = load i32, ptr %1, align 4, !tbaa !5
  %34 = add nsw i32 %33, 16
  %35 = and i32 %34, 63
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [64 x i32], ptr @sintab, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = mul nsw i32 %32, %38
  store i32 %39, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = sub nsw i32 0, %40
  store i32 %41, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %42 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %42, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %43, %44
  %46 = mul nsw i32 -20, %45
  store i32 %46, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = add nsw i32 %47, %48
  %50 = mul nsw i32 -20, %49
  store i32 %50, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %51

51:                                               ; preds = %106, %16
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = icmp slt i32 %52, 40
  br i1 %53, label %55, label %54

54:                                               ; preds = %51
  store i32 4, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %109

55:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %56 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %56, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %57 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %57, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %58

58:                                               ; preds = %95, %55
  %59 = load i32, ptr %14, align 4, !tbaa !5
  %60 = icmp slt i32 %59, 40
  br i1 %60, label %62, label %61

61:                                               ; preds = %58
  store i32 7, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %98

62:                                               ; preds = %58
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #3
  %63 = load i32, ptr %13, align 4, !tbaa !5
  %64 = ashr i32 %63, 18
  %65 = and i32 %64, 15
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [16 x [16 x i8]], ptr @logo, i64 0, i64 %66
  %68 = load i32, ptr %12, align 4, !tbaa !5
  %69 = ashr i32 %68, 18
  %70 = and i32 %69, 15
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [16 x i8], ptr %67, i64 0, i64 %71
  %73 = load i8, ptr %72, align 1, !tbaa !9
  store i8 %73, ptr %15, align 1, !tbaa !9
  %74 = load i8, ptr %15, align 1, !tbaa !9
  %75 = zext i8 %74 to i32
  %76 = load i32, ptr %2, align 4, !tbaa !5
  %77 = icmp eq i32 %75, %76
  br i1 %77, label %78, label %79

78:                                               ; preds = %62
  br label %84

79:                                               ; preds = %62
  %80 = load i8, ptr %15, align 1, !tbaa !9
  %81 = zext i8 %80 to i64
  %82 = getelementptr inbounds [8 x ptr], ptr @cmap, i64 0, i64 %81
  %83 = load ptr, ptr %82, align 8, !tbaa !10
  br label %84

84:                                               ; preds = %79, %78
  %85 = phi ptr [ @.str.7, %78 ], [ %83, %79 ]
  %86 = call i32 (ptr, ...) @printf(ptr noundef %85)
  %87 = load i8, ptr %15, align 1, !tbaa !9
  %88 = zext i8 %87 to i32
  store i32 %88, ptr %2, align 4, !tbaa !5
  %89 = load i32, ptr %4, align 4, !tbaa !5
  %90 = load i32, ptr %12, align 4, !tbaa !5
  %91 = add nsw i32 %90, %89
  store i32 %91, ptr %12, align 4, !tbaa !5
  %92 = load i32, ptr %5, align 4, !tbaa !5
  %93 = load i32, ptr %13, align 4, !tbaa !5
  %94 = add nsw i32 %93, %92
  store i32 %94, ptr %13, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #3
  br label %95

95:                                               ; preds = %84
  %96 = load i32, ptr %14, align 4, !tbaa !5
  %97 = add nsw i32 %96, 1
  store i32 %97, ptr %14, align 4, !tbaa !5
  br label %58, !llvm.loop !12

98:                                               ; preds = %61
  %99 = call i32 (ptr, ...) @printf(ptr noundef @.str.8)
  store i32 -1, ptr %2, align 4, !tbaa !5
  %100 = load i32, ptr %6, align 4, !tbaa !5
  %101 = load i32, ptr %8, align 4, !tbaa !5
  %102 = add nsw i32 %101, %100
  store i32 %102, ptr %8, align 4, !tbaa !5
  %103 = load i32, ptr %7, align 4, !tbaa !5
  %104 = load i32, ptr %9, align 4, !tbaa !5
  %105 = add nsw i32 %104, %103
  store i32 %105, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %106

106:                                              ; preds = %98
  %107 = load i32, ptr %10, align 4, !tbaa !5
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %10, align 4, !tbaa !5
  br label %51, !llvm.loop !14

109:                                              ; preds = %54
  %110 = load i32, ptr %1, align 4, !tbaa !5
  %111 = add nsw i32 %110, 1
  store i32 %111, ptr %1, align 4, !tbaa !5
  %112 = call i32 @usleep(i32 noundef 20000)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %16
}

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

declare i32 @printf(ptr noundef, ...) #2

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

declare i32 @usleep(i32 noundef) #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !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"

@logo = external dso_local local_unnamed_addr global [16 x [16 x i8]], align 16
@cmap = external dso_local local_unnamed_addr global [8 x ptr], align 16
@sintab = external dso_local local_unnamed_addr global [64 x i32], align 16
@.str.6 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
define dso_local void @main() local_unnamed_addr #0 {
  br label %1

1:                                                ; preds = %20, %0
  %.0 = phi i32 [ 0, %0 ], [ %21, %20 ]
  %2 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  %3 = and i32 %.0, 63
  %4 = zext nneg i32 %3 to i64
  %5 = getelementptr inbounds [64 x i32], ptr @sintab, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = shl i32 %6, 1
  %8 = add i32 %7, 800
  %9 = mul nsw i32 %8, %6
  %10 = add nuw nsw i32 %.0, 16
  %11 = and i32 %10, 63
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [64 x i32], ptr @sintab, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = mul nsw i32 %8, %14
  %16 = sub i32 %9, %15
  %17 = mul nsw i32 %16, -20
  %18 = add nsw i32 %15, %9
  %19 = mul nsw i32 %18, -20
  br label %.preheader

.preheader:                                       ; preds = %1, %23
  %.03545 = phi i32 [ 0, %1 ], [ %27, %23 ]
  %.03644 = phi i32 [ %19, %1 ], [ %26, %23 ]
  %.03743 = phi i32 [ %17, %1 ], [ %25, %23 ]
  br label %28

20:                                               ; preds = %23
  %21 = add nuw nsw i32 %.0, 1
  %22 = tail call i32 @usleep(i32 noundef 20000) #3
  br label %1

23:                                               ; preds = %43
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8)
  %25 = sub i32 %.03743, %15
  %26 = add nsw i32 %.03644, %9
  %27 = add nuw nsw i32 %.03545, 1
  %exitcond46.not = icmp eq i32 %27, 40
  br i1 %exitcond46.not, label %20, label %.preheader, !llvm.loop !9

28:                                               ; preds = %.preheader, %43
  %.03242 = phi i32 [ 0, %.preheader ], [ %48, %43 ]
  %.03341 = phi i32 [ %.03644, %.preheader ], [ %47, %43 ]
  %.03440 = phi i32 [ %.03743, %.preheader ], [ %46, %43 ]
  %.239 = phi i32 [ -1, %.preheader ], [ %37, %43 ]
  %29 = lshr i32 %.03341, 18
  %30 = and i32 %29, 15
  %31 = zext nneg i32 %30 to i64
  %32 = lshr i32 %.03440, 18
  %33 = and i32 %32, 15
  %34 = zext nneg i32 %33 to i64
  %35 = getelementptr inbounds [16 x [16 x i8]], ptr @logo, i64 0, i64 %31, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = zext i8 %36 to i32
  %38 = icmp eq i32 %.239, %37
  br i1 %38, label %43, label %39

39:                                               ; preds = %28
  %40 = zext i8 %36 to i64
  %41 = getelementptr inbounds [8 x ptr], ptr @cmap, i64 0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !12
  br label %43

43:                                               ; preds = %28, %39
  %44 = phi ptr [ %42, %39 ], [ @.str.7, %28 ]
  %45 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %44)
  %46 = add nsw i32 %.03440, %9
  %47 = add nsw i32 %.03341, %15
  %48 = add nuw nsw i32 %.03242, 1
  %exitcond.not = icmp eq i32 %48, 40
  br i1 %exitcond.not, label %23, label %28, !llvm.loop !14
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

declare i32 @usleep(i32 noundef) local_unnamed_addr #2

attributes #0 = { noreturn 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 = { nofree nounwind "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = distinct !{!14, !10}


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