; LLVM IR from BrunoLevy/learn-fpga :: FemtoRV/FIRMWARE/LIBFEMTORV32 (spi_sd)

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

@.str.2 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [34 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @spi_send(i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @spi_receive() #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
declare dso_local zeroext i8 @spi_sendrecv(i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare dso_local void @spi_writeblock(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @sd_send_command(i8 noundef zeroext, i32 noundef) #0

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

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

12:                                               ; preds = %66, %3
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = add i32 %13, -1
  store i32 %14, ptr %7, align 4, !tbaa !5
  %15 = icmp ne i32 %13, 0
  br i1 %15, label %16, label %67

16:                                               ; preds = %12
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = add i32 %17, 1
  store i32 %18, ptr %5, align 4, !tbaa !5
  %19 = call zeroext i8 @sd_send_command(i8 noundef zeroext 24, i32 noundef %17)
  store i8 %19, ptr %8, align 1, !tbaa !11
  %20 = load i8, ptr %8, align 1, !tbaa !11
  %21 = zext i8 %20 to i32
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %27

23:                                               ; preds = %16
  %24 = load i8, ptr %8, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %25)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

27:                                               ; preds = %16
  call void @spi_send(i8 noundef zeroext -2)
  %28 = load ptr, ptr %6, align 8, !tbaa !9
  call void @spi_writeblock(ptr noundef %28, i32 noundef 512)
  %29 = load ptr, ptr %6, align 8, !tbaa !9
  %30 = getelementptr inbounds i8, ptr %29, i64 512
  store ptr %30, ptr %6, align 8, !tbaa !9
  call void @spi_send(i8 noundef zeroext -1)
  call void @spi_send(i8 noundef zeroext -1)
  %31 = call zeroext i8 (i32, ...) @spi_receive(i32 noundef 255)
  store i8 %31, ptr %8, align 1, !tbaa !11
  %32 = load i8, ptr %8, align 1, !tbaa !11
  %33 = zext i8 %32 to i32
  %34 = and i32 %33, 31
  %35 = icmp ne i32 %34, 5
  br i1 %35, label %36, label %40

36:                                               ; preds = %27
  %37 = load i8, ptr %8, align 1, !tbaa !11
  %38 = zext i8 %37 to i32
  %39 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %38)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

40:                                               ; preds = %27
  br label %41

41:                                               ; preds = %50, %40
  %42 = call zeroext i8 @spi_sendrecv(i8 noundef zeroext -1)
  %43 = zext i8 %42 to i32
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %53

45:                                               ; preds = %41
  %46 = load i32, ptr %9, align 4, !tbaa !5
  %47 = icmp sgt i32 %46, 5000
  br i1 %47, label %48, label %50

48:                                               ; preds = %45
  %49 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

50:                                               ; preds = %45
  %51 = load i32, ptr %9, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %9, align 4, !tbaa !5
  br label %41, !llvm.loop !12

53:                                               ; preds = %41
  call void @spi_send(i8 noundef zeroext -1)
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %63, %53
  %55 = call zeroext i8 @spi_sendrecv(i8 noundef zeroext -1)
  %56 = zext i8 %55 to i32
  %57 = icmp eq i32 %56, 0
  br i1 %57, label %58, label %66

58:                                               ; preds = %54
  %59 = load i32, ptr %9, align 4, !tbaa !5
  %60 = icmp sgt i32 %59, 5000
  br i1 %60, label %61, label %63

61:                                               ; preds = %58
  %62 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

63:                                               ; preds = %58
  %64 = load i32, ptr %9, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %9, align 4, !tbaa !5
  br label %54, !llvm.loop !14

66:                                               ; preds = %54
  br label %12, !llvm.loop !15

67:                                               ; preds = %12
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %68

68:                                               ; preds = %67, %61, %48, %36, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  %69 = load i32, ptr %4, align 4
  ret i32 %69
}

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 = !{!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}
!15 = distinct !{!15, !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"

@.str.2 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [34 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @spi_send(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @spi_receive() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @spi_sendrecv(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @spi_writeblock(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @sd_send_command(i8 noundef zeroext, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @sd_writesector(i32 noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not31 = icmp eq i32 %2, 0
  br i1 %.not31, label %.loopexit19, label %.lr.ph36

.loopexit:                                        ; preds = %29, %._crit_edge
  %.2.lcssa = phi i32 [ 0, %._crit_edge ], [ %30, %29 ]
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.loopexit19, label %.lr.ph36, !llvm.loop !5

.lr.ph36:                                         ; preds = %3, %.loopexit
  %.in = phi i32 [ %4, %.loopexit ], [ %2, %3 ]
  %.01234 = phi i32 [ %.2.lcssa, %.loopexit ], [ 0, %3 ]
  %.01433 = phi ptr [ %11, %.loopexit ], [ %1, %3 ]
  %.01532 = phi i32 [ %5, %.loopexit ], [ %0, %3 ]
  %4 = add i32 %.in, -1
  %5 = add i32 %.01532, 1
  %6 = tail call zeroext i8 @sd_send_command(i8 noundef zeroext 24, i32 noundef %.01532)
  %.not17 = icmp eq i8 %6, 0
  br i1 %.not17, label %10, label %7

7:                                                ; preds = %.lr.ph36
  %8 = zext i8 %6 to i32
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %8)
  br label %.loopexit19

10:                                               ; preds = %.lr.ph36
  tail call void @spi_send(i8 noundef zeroext -2)
  tail call void @spi_writeblock(ptr noundef %.01433, i32 noundef 512)
  %11 = getelementptr inbounds i8, ptr %.01433, i64 512
  tail call void @spi_send(i8 noundef zeroext -1)
  tail call void @spi_send(i8 noundef zeroext -1)
  %12 = tail call zeroext i8 (i32, ...) @spi_receive(i32 noundef 255)
  %13 = zext i8 %12 to i32
  %14 = and i32 %13, 31
  %.not18 = icmp eq i32 %14, 5
  br i1 %.not18, label %.preheader, label %17

.preheader:                                       ; preds = %10
  %15 = tail call zeroext i8 @spi_sendrecv(i8 noundef zeroext -1)
  %16 = icmp eq i8 %15, 0
  br i1 %16, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %umax = tail call i32 @llvm.umax.i32(i32 %.01234, i32 5001)
  br label %.lr.ph

17:                                               ; preds = %10
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %13)
  br label %.loopexit19

19:                                               ; preds = %.lr.ph
  %20 = add nuw nsw i32 %.127, 1
  %21 = tail call zeroext i8 @spi_sendrecv(i8 noundef zeroext -1)
  %22 = icmp eq i8 %21, 0
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !7

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %.127 = phi i32 [ %20, %19 ], [ %.01234, %.lr.ph.preheader ]
  %exitcond = icmp eq i32 %.127, %umax
  br i1 %exitcond, label %23, label %19

23:                                               ; preds = %.lr.ph
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %.loopexit19

._crit_edge:                                      ; preds = %19, %.preheader
  tail call void @spi_send(i8 noundef zeroext -1)
  %25 = tail call zeroext i8 @spi_sendrecv(i8 noundef zeroext -1)
  %26 = icmp eq i8 %25, 0
  br i1 %26, label %.lr.ph30, label %.loopexit

.lr.ph30:                                         ; preds = %._crit_edge, %29
  %.228 = phi i32 [ %30, %29 ], [ 0, %._crit_edge ]
  %exitcond46 = icmp eq i32 %.228, 5001
  br i1 %exitcond46, label %27, label %29

27:                                               ; preds = %.lr.ph30
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %.loopexit19

29:                                               ; preds = %.lr.ph30
  %30 = add nuw nsw i32 %.228, 1
  %31 = tail call zeroext i8 @spi_sendrecv(i8 noundef zeroext -1)
  %32 = icmp eq i8 %31, 0
  br i1 %32, label %.lr.ph30, label %.loopexit, !llvm.loop !8

.loopexit19:                                      ; preds = %.loopexit, %3, %27, %23, %17, %7
  %.0 = phi i32 [ 0, %7 ], [ 0, %17 ], [ 0, %23 ], [ 0, %27 ], [ 1, %3 ], [ 1, %.loopexit ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #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 = { 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 = { nocallback nofree nosync nounwind speculatable 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}
!8 = distinct !{!8, !6}


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