; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/FIRMWARE/LIBFEMTOC (print)

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

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

declare i32 @putchar(i32 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 @print_dec(i32 noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca [255 x i8], align 16
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 255, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = getelementptr inbounds [255 x i8], ptr %3, i64 0, i64 0
  store ptr %6, ptr %4, align 8, !tbaa !9
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %9, label %13

9:                                                ; preds = %1
  %10 = call i32 @putchar(i32 noundef 45)
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sub nsw i32 0, %11
  call void @print_dec(i32 noundef %12)
  store i32 1, ptr %5, align 4
  br label %44

13:                                               ; preds = %1
  br label %14

14:                                               ; preds = %23, %13
  %15 = load i32, ptr %2, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %21, label %17

17:                                               ; preds = %14
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  %19 = getelementptr inbounds [255 x i8], ptr %3, i64 0, i64 0
  %20 = icmp eq ptr %18, %19
  br label %21

21:                                               ; preds = %17, %14
  %22 = phi i1 [ true, %14 ], [ %20, %17 ]
  br i1 %22, label %23, label %31

23:                                               ; preds = %21
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = srem i32 %24, 10
  %26 = trunc i32 %25 to i8
  %27 = load ptr, ptr %4, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %27, i32 1
  store ptr %28, ptr %4, align 8, !tbaa !9
  store i8 %26, ptr %27, align 1, !tbaa !11
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sdiv i32 %29, 10
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %14, !llvm.loop !12

31:                                               ; preds = %21
  br label %32

32:                                               ; preds = %36, %31
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = getelementptr inbounds [255 x i8], ptr %3, i64 0, i64 0
  %35 = icmp ne ptr %33, %34
  br i1 %35, label %36, label %43

36:                                               ; preds = %32
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds i8, ptr %37, i32 -1
  store ptr %38, ptr %4, align 8, !tbaa !9
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = sext i8 %39 to i32
  %41 = add nsw i32 48, %40
  %42 = call i32 @putchar(i32 noundef %41)
  br label %32, !llvm.loop !14

43:                                               ; preds = %32
  store i32 0, ptr %5, align 4
  br label %44

44:                                               ; preds = %43, %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 255, ptr %3) #3
  %45 = load i32, ptr %5, align 4
  switch i32 %45, label %47 [
    i32 0, label %46
    i32 1, label %46
  ]

46:                                               ; preds = %44, %44
  ret void

47:                                               ; preds = %44
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 }

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

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

; Function Attrs: nofree nounwind
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nofree nounwind uwtable
define dso_local void @print_dec(i32 noundef %0) local_unnamed_addr #2 {
  %2 = alloca [255 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 255, ptr nonnull %2) #3
  %3 = icmp slt i32 %0, 0
  br i1 %3, label %4, label %.preheader11

4:                                                ; preds = %1
  %5 = tail call i32 @putchar(i32 noundef 45)
  %6 = sub nsw i32 0, %0
  tail call void @print_dec(i32 noundef %6)
  br label %.loopexit

.preheader11:                                     ; preds = %1, %.preheader11
  %.013 = phi ptr [ %9, %.preheader11 ], [ %2, %1 ]
  %.0812 = phi i32 [ %10, %.preheader11 ], [ %0, %1 ]
  %7 = urem i32 %.0812, 10
  %8 = trunc nuw nsw i32 %7 to i8
  %9 = getelementptr inbounds i8, ptr %.013, i64 1
  store i8 %8, ptr %.013, align 1, !tbaa !5
  %10 = udiv i32 %.0812, 10
  %11 = icmp ugt i32 %.0812, 9
  br i1 %11, label %.preheader11, label %.preheader, !llvm.loop !8

.preheader:                                       ; preds = %.preheader11, %.preheader
  %.114 = phi ptr [ %12, %.preheader ], [ %9, %.preheader11 ]
  %12 = getelementptr inbounds i8, ptr %.114, i64 -1
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %14 = sext i8 %13 to i32
  %15 = add nsw i32 %14, 48
  %16 = call i32 @putchar(i32 noundef %15)
  %.not = icmp eq ptr %12, %2
  br i1 %.not, label %.loopexit, label %.preheader, !llvm.loop !10

.loopexit:                                        ; preds = %.preheader, %4
  call void @llvm.lifetime.end.p0(i64 255, ptr nonnull %2) #3
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nofree 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 }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass