; LLVM IR from cvra/robot-software :: lib/softspi (softspi)

; 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.softspi_t = type { i32 }

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

16:                                               ; preds = %74, %4
  %17 = load i64, ptr %13, align 8, !tbaa !9
  %18 = load i64, ptr %8, align 8, !tbaa !9
  %19 = mul i64 8, %18
  %20 = icmp ult i64 %17, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  br label %77

22:                                               ; preds = %16
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load i64, ptr %13, align 8, !tbaa !9
  %25 = udiv i64 %24, 8
  %26 = getelementptr inbounds i8, ptr %23, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = zext i8 %27 to i32
  store i32 %28, ptr %9, align 4, !tbaa !12
  %29 = load i64, ptr %13, align 8, !tbaa !9
  %30 = urem i64 %29, 8
  %31 = sub i64 7, %30
  %32 = trunc i64 %31 to i32
  store i32 %32, ptr %11, align 4, !tbaa !12
  %33 = load i32, ptr %9, align 4, !tbaa !12
  %34 = load i32, ptr %11, align 4, !tbaa !12
  %35 = shl i32 1, %34
  %36 = and i32 %33, %35
  %37 = icmp ne i32 %36, 0
  %38 = zext i1 %37 to i32
  store i32 %38, ptr %10, align 4, !tbaa !12
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = load i32, ptr %10, align 4, !tbaa !12
  call void @softspi_mosi_set(ptr noundef %39, i32 noundef %40)
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.softspi_t, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 4, !tbaa !14
  store volatile i32 %43, ptr %12, align 4, !tbaa !12
  br label %44

44:                                               ; preds = %48, %22
  %45 = load volatile i32, ptr %12, align 4, !tbaa !12
  %46 = add nsw i32 %45, -1
  store volatile i32 %46, ptr %12, align 4, !tbaa !12
  %47 = icmp ne i32 %45, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %44
  br label %44, !llvm.loop !16

49:                                               ; preds = %44
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  call void @softspi_sck_set(ptr noundef %50, i32 noundef 1)
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.softspi_t, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 4, !tbaa !14
  store volatile i32 %53, ptr %12, align 4, !tbaa !12
  br label %54

54:                                               ; preds = %58, %49
  %55 = load volatile i32, ptr %12, align 4, !tbaa !12
  %56 = add nsw i32 %55, -1
  store volatile i32 %56, ptr %12, align 4, !tbaa !12
  %57 = icmp ne i32 %55, 0
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  br label %54, !llvm.loop !18

59:                                               ; preds = %54
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = call i32 @softspi_miso_get(ptr noundef %60)
  store i32 %61, ptr %10, align 4, !tbaa !12
  %62 = load i32, ptr %10, align 4, !tbaa !12
  %63 = load i32, ptr %11, align 4, !tbaa !12
  %64 = shl i32 %62, %63
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = load i64, ptr %13, align 8, !tbaa !9
  %67 = udiv i64 %66, 8
  %68 = getelementptr inbounds i8, ptr %65, i64 %67
  %69 = load i8, ptr %68, align 1, !tbaa !11
  %70 = zext i8 %69 to i32
  %71 = or i32 %70, %64
  %72 = trunc i32 %71 to i8
  store i8 %72, ptr %68, align 1, !tbaa !11
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  call void @softspi_sck_set(ptr noundef %73, i32 noundef 0)
  br label %74

74:                                               ; preds = %59
  %75 = load i64, ptr %13, align 8, !tbaa !9
  %76 = add i64 %75, 1
  store i64 %76, ptr %13, align 8, !tbaa !9
  br label %16, !llvm.loop !19

77:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

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

declare i32 @softspi_miso_get(ptr noundef) #3

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

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 }

!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 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 0}
!15 = !{!"", !13, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = distinct !{!19, !17}


; 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 @softspi_send(ptr noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5)
  tail call void @llvm.memset.p0.i64(ptr align 1 %2, i8 0, i64 %3, i1 false)
  %6 = shl i64 %3, 3
  %.not24 = icmp eq i64 %6, 0
  br i1 %.not24, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %23, %4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5)
  ret void

.lr.ph:                                           ; preds = %4, %23
  %.023 = phi i64 [ %30, %23 ], [ 0, %4 ]
  %7 = lshr i64 %.023, 3
  %8 = getelementptr inbounds i8, ptr %1, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = trunc i64 %.023 to i32
  %12 = and i32 %11, 7
  %13 = xor i32 %12, 7
  %14 = lshr i32 %10, %13
  %15 = and i32 %14, 1
  tail call void @softspi_mosi_set(ptr noundef %0, i32 noundef %15) #4
  %16 = load i32, ptr %0, align 4, !tbaa !8
  store volatile i32 %16, ptr %5, align 4, !tbaa !11
  br label %17

17:                                               ; preds = %17, %.lr.ph
  %.0..0..0..0. = load volatile i32, ptr %5, align 4, !tbaa !11
  %18 = add nsw i32 %.0..0..0..0., -1
  store volatile i32 %18, ptr %5, align 4, !tbaa !11
  %.not = icmp eq i32 %.0..0..0..0., 0
  br i1 %.not, label %19, label %17, !llvm.loop !12

19:                                               ; preds = %17
  tail call void @softspi_sck_set(ptr noundef nonnull %0, i32 noundef 1) #4
  %20 = load i32, ptr %0, align 4, !tbaa !8
  store volatile i32 %20, ptr %5, align 4, !tbaa !11
  br label %21

21:                                               ; preds = %21, %19
  %.0..0..0..0.5 = load volatile i32, ptr %5, align 4, !tbaa !11
  %22 = add nsw i32 %.0..0..0..0.5, -1
  store volatile i32 %22, ptr %5, align 4, !tbaa !11
  %.not22 = icmp eq i32 %.0..0..0..0.5, 0
  br i1 %.not22, label %23, label %21, !llvm.loop !14

23:                                               ; preds = %21
  %24 = tail call i32 @softspi_miso_get(ptr noundef nonnull %0) #4
  %25 = shl i32 %24, %13
  %26 = getelementptr inbounds i8, ptr %2, i64 %7
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = trunc i32 %25 to i8
  %29 = or i8 %27, %28
  store i8 %29, ptr %26, align 1, !tbaa !5
  tail call void @softspi_sck_set(ptr noundef nonnull %0, i32 noundef 0) #4
  %30 = add nuw i64 %.023, 1
  %exitcond.not = icmp eq i64 %30, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15
}

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare void @softspi_mosi_set(ptr noundef, i32 noundef) local_unnamed_addr #3

declare void @softspi_sck_set(ptr noundef, i32 noundef) local_unnamed_addr #3

declare i32 @softspi_miso_get(ptr noundef) local_unnamed_addr #3

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

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

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


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