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

; 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.usart_t = type { [32 x i8], [32 x i8], %struct.file_operations, %struct.__UART_HandleTypeDef, %struct.dma_t, %struct.dma_t, i32, ptr, ptr, i32, i32, i32, i32, i32, %struct.usart_private_t }
%struct.file_operations = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.__UART_HandleTypeDef = type { ptr, %struct.UART_InitTypeDef, %struct.UART_AdvFeatureInitTypeDef, ptr, i16, i16, ptr, i16, i16, i16, i32, i16, i16, i32, ptr, ptr, ptr, ptr, i32, i32, i32, i32 }
%struct.UART_InitTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.UART_AdvFeatureInitTypeDef = type { 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.usart_private_t = type { i32, ptr, ptr, ptr, ptr, i32, i32, i32, i32, ptr, ptr, ptr, ptr, i32, i8, i8, i8, i8, i8 }
%struct.USART_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }

@UARTPrescTable = external constant [12 x i16], align 16

; 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

; Function Attrs: nounwind uwtable
define dso_local i32 @usart_set_speed(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %6, ptr %5, align 8, !tbaa !5
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.usart_t, ptr %8, i32 0, i32 3
  %10 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %9, i32 0, i32 1
  %11 = getelementptr inbounds %struct.UART_InitTypeDef, ptr %10, i32 0, i32 0
  store i32 %7, ptr %11, align 8, !tbaa !11
  %12 = call i32 @HAL_RCC_GetPCLK1Freq()
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.usart_t, ptr %13, i32 0, i32 3
  %15 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %14, i32 0, i32 1
  %16 = getelementptr inbounds %struct.UART_InitTypeDef, ptr %15, i32 0, i32 8
  %17 = load i32, ptr %16, align 8, !tbaa !24
  %18 = zext i32 %17 to i64
  %19 = getelementptr inbounds [12 x i16], ptr @UARTPrescTable, i64 0, i64 %18
  %20 = load i16, ptr %19, align 2, !tbaa !25
  %21 = zext i16 %20 to i32
  %22 = udiv i32 %12, %21
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.usart_t, ptr %23, i32 0, i32 3
  %25 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %24, i32 0, i32 1
  %26 = getelementptr inbounds %struct.UART_InitTypeDef, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = udiv i32 %27, 2
  %29 = add i32 %22, %28
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.usart_t, ptr %30, i32 0, i32 3
  %32 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %31, i32 0, i32 1
  %33 = getelementptr inbounds %struct.UART_InitTypeDef, ptr %32, i32 0, i32 0
  %34 = load i32, ptr %33, align 8, !tbaa !11
  %35 = udiv i32 %29, %34
  %36 = trunc i32 %35 to i16
  %37 = zext i16 %36 to i32
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.usart_t, ptr %38, i32 0, i32 3
  %40 = getelementptr inbounds %struct.__UART_HandleTypeDef, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !26
  %42 = getelementptr inbounds %struct.USART_TypeDef, ptr %41, i32 0, i32 3
  store volatile i32 %37, ptr %42, align 4, !tbaa !27
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret i32 0
}

declare i32 @HAL_RCC_GetPCLK1Freq() #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 160}
!12 = !{!"", !7, i64 0, !7, i64 32, !13, i64 64, !14, i64 152, !18, i64 336, !18, i64 528, !10, i64 720, !6, i64 728, !6, i64 736, !10, i64 744, !10, i64 748, !10, i64 752, !10, i64 756, !10, i64 760, !22, i64 768}
!13 = !{!"file_operations", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80}
!14 = !{!"__UART_HandleTypeDef", !6, i64 0, !15, i64 8, !16, i64 44, !6, i64 88, !17, i64 96, !17, i64 98, !6, i64 104, !17, i64 112, !17, i64 114, !17, i64 116, !10, i64 120, !17, i64 124, !17, i64 126, !10, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !10, i64 168, !10, i64 172, !10, i64 176, !10, i64 180}
!15 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32}
!16 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36}
!17 = !{!"short", !7, i64 0}
!18 = !{!"", !19, i64 0, !10, i64 184, !21, i64 188}
!19 = !{!"__DMA_HandleTypeDef", !6, i64 0, !20, i64 8, !10, i64 56, !10, i64 60, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !10, i64 120, !10, i64 124, !10, i64 128, !6, i64 136, !6, i64 144, !10, i64 152, !6, i64 160, !6, i64 168, !10, i64 176}
!20 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44}
!21 = !{!"", !10, i64 0}
!22 = !{!"", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !23, i64 92, !23, i64 93, !23, i64 94, !23, i64 95, !23, i64 96}
!23 = !{!"_Bool", !7, i64 0}
!24 = !{!12, !10, i64 192}
!25 = !{!17, !17, i64 0}
!26 = !{!12, !6, i64 152}
!27 = !{!20, !10, i64 12}


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

@UARTPrescTable = external local_unnamed_addr constant [12 x i16], align 16

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @usart_set_speed(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 152
  %4 = getelementptr inbounds i8, ptr %0, i64 160
  store i32 %1, ptr %4, align 8, !tbaa !5
  %5 = tail call i32 @HAL_RCC_GetPCLK1Freq() #2
  %6 = getelementptr inbounds i8, ptr %0, i64 192
  %7 = load i32, ptr %6, align 8, !tbaa !22
  %8 = zext i32 %7 to i64
  %9 = getelementptr inbounds [12 x i16], ptr @UARTPrescTable, i64 0, i64 %8
  %10 = load i16, ptr %9, align 2, !tbaa !23
  %11 = zext i16 %10 to i32
  %12 = udiv i32 %5, %11
  %13 = load i32, ptr %4, align 8, !tbaa !5
  %14 = lshr i32 %13, 1
  %15 = add i32 %14, %12
  %16 = udiv i32 %15, %13
  %17 = and i32 %16, 65535
  %18 = load ptr, ptr %3, align 8, !tbaa !24
  %19 = getelementptr inbounds i8, ptr %18, i64 12
  store volatile i32 %17, ptr %19, align 4, !tbaa !25
  ret i32 0
}

declare i32 @HAL_RCC_GetPCLK1Freq() local_unnamed_addr #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 = { "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, !13, i64 160}
!6 = !{!"", !7, i64 0, !7, i64 32, !9, i64 64, !11, i64 152, !16, i64 336, !16, i64 528, !13, i64 720, !10, i64 728, !10, i64 736, !13, i64 744, !13, i64 748, !13, i64 752, !13, i64 756, !13, i64 760, !20, i64 768}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"file_operations", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"__UART_HandleTypeDef", !10, i64 0, !12, i64 8, !14, i64 44, !10, i64 88, !15, i64 96, !15, i64 98, !10, i64 104, !15, i64 112, !15, i64 114, !15, i64 116, !13, i64 120, !15, i64 124, !15, i64 126, !13, i64 128, !10, i64 136, !10, i64 144, !10, i64 152, !10, i64 160, !13, i64 168, !13, i64 172, !13, i64 176, !13, i64 180}
!12 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28, !13, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28, !13, i64 32, !13, i64 36}
!15 = !{!"short", !7, i64 0}
!16 = !{!"", !17, i64 0, !13, i64 184, !19, i64 188}
!17 = !{!"__DMA_HandleTypeDef", !10, i64 0, !18, i64 8, !13, i64 56, !13, i64 60, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !13, i64 120, !13, i64 124, !13, i64 128, !10, i64 136, !10, i64 144, !13, i64 152, !10, i64 160, !10, i64 168, !13, i64 176}
!18 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28, !13, i64 32, !13, i64 36, !13, i64 40, !13, i64 44}
!19 = !{!"", !13, i64 0}
!20 = !{!"", !13, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !13, i64 40, !13, i64 44, !13, i64 48, !13, i64 52, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !13, i64 88, !21, i64 92, !21, i64 93, !21, i64 94, !21, i64 95, !21, i64 96}
!21 = !{!"_Bool", !7, i64 0}
!22 = !{!6, !13, i64 192}
!23 = !{!15, !15, i64 0}
!24 = !{!6, !10, i64 152}
!25 = !{!18, !13, i64 12}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,TailCallElimPass