; LLVM IR from ctlst-tech/catpilot :: bsp/mcu/periph/spi/stm32 (spi)

; 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.spi_t = type { %struct.__SPI_HandleTypeDef, ptr, ptr, ptr, %struct.dma_t, %struct.dma_t, i32, i32, %struct.spi_private_t }
%struct.__SPI_HandleTypeDef = type { ptr, %struct.SPI_InitTypeDef, ptr, i16, i16, ptr, i16, i16, i32, ptr, ptr, ptr, ptr, i32, i32, i32 }
%struct.SPI_InitTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.dma_t = type { %struct.__DMA_HandleTypeDef, i32, %struct.dma_private_t }
%struct.__DMA_HandleTypeDef = type { ptr, %struct.DMA_InitTypeDef, i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, ptr, ptr, i32, ptr, ptr, i32 }
%struct.DMA_InitTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.dma_private_t = type { i32 }
%struct.spi_private_t = type { i32, ptr, ptr, ptr, i32 }

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

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

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) #1

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) #1

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

15:                                               ; preds = %4
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %21, label %18

18:                                               ; preds = %15
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %18, %15, %4
  store i32 22, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %75

22:                                               ; preds = %18
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.spi_t, ptr %23, i32 0, i32 8
  %25 = getelementptr inbounds %struct.spi_private_t, ptr %24, i32 0, i32 2
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.spi_t, ptr %27, i32 0, i32 6
  %29 = load i32, ptr %28, align 8, !tbaa !22
  %30 = mul i32 %29, 1000
  %31 = udiv i32 %30, 1000
  %32 = call i64 @xQueueSemaphoreTake(ptr noundef %26, i32 noundef %31)
  %33 = icmp eq i64 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %22
  store i32 116, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %75

35:                                               ; preds = %22
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.spi_t, ptr %36, i32 0, i32 8
  %38 = getelementptr inbounds %struct.spi_private_t, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !23
  %40 = call i64 @xQueueSemaphoreTake(ptr noundef %39, i32 noundef 0)
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.spi_t, ptr %41, i32 0, i32 8
  %43 = getelementptr inbounds %struct.spi_private_t, ptr %42, i32 0, i32 4
  store i32 3, ptr %43, align 8, !tbaa !24
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.spi_t, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = load i16, ptr %9, align 2, !tbaa !9
  %49 = call i32 @HAL_SPI_TransmitReceive_DMA(ptr noundef %45, ptr noundef %46, ptr noundef %47, i16 noundef zeroext %48)
  store i32 %49, ptr %10, align 4, !tbaa !11
  %50 = load i32, ptr %10, align 4, !tbaa !11
  %51 = icmp eq i32 %50, 0
  br i1 %51, label %52, label %65

52:                                               ; preds = %35
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.spi_t, ptr %53, i32 0, i32 8
  %55 = getelementptr inbounds %struct.spi_private_t, ptr %54, i32 0, i32 1
  %56 = load ptr, ptr %55, align 8, !tbaa !23
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.spi_t, ptr %57, i32 0, i32 6
  %59 = load i32, ptr %58, align 8, !tbaa !22
  %60 = mul i32 %59, 1000
  %61 = udiv i32 %60, 1000
  %62 = call i64 @xQueueSemaphoreTake(ptr noundef %56, i32 noundef %61)
  %63 = icmp ne i64 %62, 0
  br i1 %63, label %65, label %64

64:                                               ; preds = %52
  store i32 116, ptr %10, align 4, !tbaa !11
  br label %65

65:                                               ; preds = %64, %52, %35
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.spi_t, ptr %66, i32 0, i32 8
  %68 = getelementptr inbounds %struct.spi_private_t, ptr %67, i32 0, i32 4
  store i32 0, ptr %68, align 8, !tbaa !24
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.spi_t, ptr %69, i32 0, i32 8
  %71 = getelementptr inbounds %struct.spi_private_t, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !13
  %73 = call i64 @xQueueGenericSend(ptr noundef %72, ptr noundef null, i32 noundef 0, i64 noundef 0)
  %74 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %74, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %75

75:                                               ; preds = %65, %34, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %76 = load i32, ptr %5, align 4
  ret i32 %76
}

declare i32 @HAL_SPI_TransmitReceive_DMA(ptr noundef, ptr noundef, ptr noundef, i16 noundef zeroext) #1

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 608}
!14 = !{!"", !15, i64 0, !6, i64 176, !6, i64 184, !6, i64 192, !17, i64 200, !17, i64 392, !12, i64 584, !12, i64 588, !21, i64 592}
!15 = !{!"__SPI_HandleTypeDef", !6, i64 0, !16, i64 8, !6, i64 96, !10, i64 104, !10, i64 106, !6, i64 112, !10, i64 120, !10, i64 122, !12, i64 124, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !12, i64 160, !12, i64 164, !12, i64 168}
!16 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84}
!17 = !{!"", !18, i64 0, !12, i64 184, !20, i64 188}
!18 = !{!"__DMA_HandleTypeDef", !6, i64 0, !19, i64 8, !12, i64 56, !12, i64 60, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !12, i64 120, !12, i64 124, !12, i64 128, !6, i64 136, !6, i64 144, !12, i64 152, !6, i64 160, !6, i64 168, !12, i64 176}
!19 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44}
!20 = !{!"", !12, i64 0}
!21 = !{!"", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !12, i64 32}
!22 = !{!14, !12, i64 584}
!23 = !{!14, !6, i64 600}
!24 = !{!14, !12, i64 624}


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

declare i64 @xQueueSemaphoreTake(ptr noundef, i32 noundef) local_unnamed_addr #0

declare i64 @xQueueGenericSend(ptr noundef, ptr noundef, i32 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @spi_transmit_receive(ptr noundef %0, ptr noundef %1, ptr noundef %2, i16 noundef zeroext %3) local_unnamed_addr #1 {
  %5 = icmp eq i16 %3, 0
  %6 = icmp eq ptr %1, null
  %or.cond = or i1 %6, %5
  %7 = icmp eq ptr %2, null
  %or.cond3 = or i1 %7, %or.cond
  br i1 %or.cond3, label %33, label %8

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 608
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %0, i64 584
  %12 = load i32, ptr %11, align 8, !tbaa !19
  %13 = mul i32 %12, 1000
  %14 = udiv i32 %13, 1000
  %15 = tail call i64 @xQueueSemaphoreTake(ptr noundef %10, i32 noundef %14) #2
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %33, label %17

17:                                               ; preds = %8
  %18 = getelementptr inbounds i8, ptr %0, i64 600
  %19 = load ptr, ptr %18, align 8, !tbaa !20
  %20 = tail call i64 @xQueueSemaphoreTake(ptr noundef %19, i32 noundef 0) #2
  %21 = getelementptr inbounds i8, ptr %0, i64 624
  store i32 3, ptr %21, align 8, !tbaa !21
  %22 = tail call i32 @HAL_SPI_TransmitReceive_DMA(ptr noundef nonnull %0, ptr noundef nonnull %1, ptr noundef nonnull %2, i16 noundef zeroext %3) #2
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %30

24:                                               ; preds = %17
  %25 = load ptr, ptr %18, align 8, !tbaa !20
  %26 = load i32, ptr %11, align 8, !tbaa !19
  %27 = mul i32 %26, 1000
  %28 = udiv i32 %27, 1000
  %29 = tail call i64 @xQueueSemaphoreTake(ptr noundef %25, i32 noundef %28) #2
  %.not = icmp eq i64 %29, 0
  %spec.select = select i1 %.not, i32 116, i32 0
  br label %30

30:                                               ; preds = %24, %17
  %.0 = phi i32 [ %22, %17 ], [ %spec.select, %24 ]
  store i32 0, ptr %21, align 8, !tbaa !21
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = tail call i64 @xQueueGenericSend(ptr noundef %31, ptr noundef null, i32 noundef 0, i64 noundef 0) #2
  br label %33

33:                                               ; preds = %8, %4, %30
  %.020 = phi i32 [ %.0, %30 ], [ 22, %4 ], [ 116, %8 ]
  ret i32 %.020
}

declare i32 @HAL_SPI_TransmitReceive_DMA(ptr noundef, ptr noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

attributes #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 = { 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 #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, !8, i64 608}
!6 = !{!"", !7, i64 0, !8, i64 176, !8, i64 184, !8, i64 192, !14, i64 200, !14, i64 392, !12, i64 584, !12, i64 588, !18, i64 592}
!7 = !{!"__SPI_HandleTypeDef", !8, i64 0, !11, i64 8, !8, i64 96, !13, i64 104, !13, i64 106, !8, i64 112, !13, i64 120, !13, i64 122, !12, i64 124, !8, i64 128, !8, i64 136, !8, i64 144, !8, i64 152, !12, i64 160, !12, i64 164, !12, i64 168}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !12, i64 84}
!12 = !{!"int", !9, i64 0}
!13 = !{!"short", !9, i64 0}
!14 = !{!"", !15, i64 0, !12, i64 184, !17, i64 188}
!15 = !{!"__DMA_HandleTypeDef", !8, i64 0, !16, i64 8, !12, i64 56, !12, i64 60, !8, i64 64, !8, i64 72, !8, i64 80, !8, i64 88, !8, i64 96, !8, i64 104, !8, i64 112, !12, i64 120, !12, i64 124, !12, i64 128, !8, i64 136, !8, i64 144, !12, i64 152, !8, i64 160, !8, i64 168, !12, i64 176}
!16 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36, !12, i64 40, !12, i64 44}
!17 = !{!"", !12, i64 0}
!18 = !{!"", !12, i64 0, !8, i64 8, !8, i64 16, !8, i64 24, !12, i64 32}
!19 = !{!6, !12, i64 584}
!20 = !{!6, !8, i64 600}
!21 = !{!6, !12, i64 624}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass