; 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.file = type { i32, ptr, ptr }
%struct.node = type { [64 x i8], %struct.file_operations, ptr, ptr, ptr }
%struct.file_operations = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%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.__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 }

@FreeRTOS_errno = external global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local i32 @usart_read(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.file, ptr %11, i32 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds %struct.node, ptr %13, i32 0, i32 1
  %15 = getelementptr inbounds %struct.file_operations, ptr %14, i32 0, i32 10
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  store ptr %16, ptr %9, align 8, !tbaa !5
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.usart_t, ptr %17, i32 0, i32 14
  %19 = getelementptr inbounds %struct.usart_private_t, ptr %18, i32 0, i32 10
  %20 = load ptr, ptr %19, align 8, !tbaa !18
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load i64, ptr %7, align 8, !tbaa !9
  %23 = trunc i64 %22 to i32
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.usart_t, ptr %24, i32 0, i32 13
  %26 = load i32, ptr %25, align 8, !tbaa !30
  %27 = call i32 @ring_buf_read_timeout(ptr noundef %20, ptr noundef %21, i32 noundef %23, i32 noundef %26)
  store i32 %27, ptr %8, align 4, !tbaa !11
  %28 = load i32, ptr %8, align 4, !tbaa !11
  %29 = icmp slt i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %3
  store i32 116, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

31:                                               ; preds = %3
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.usart_t, ptr %32, i32 0, i32 14
  %34 = getelementptr inbounds %struct.usart_private_t, ptr %33, i32 0, i32 13
  %35 = load i32, ptr %34, align 8, !tbaa !31
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %31
  store i32 71, ptr @FreeRTOS_errno, align 4, !tbaa !11
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

38:                                               ; preds = %31
  %39 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %39, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

40:                                               ; preds = %38, %37, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %41 = load i32, ptr %4, align 4
  ret i32 %41
}

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

declare i32 @ring_buf_read_timeout(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"file", !12, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!16, !6, i64 144}
!16 = !{!"node", !7, i64 0, !17, i64 64, !6, i64 152, !6, i64 160, !6, i64 168}
!17 = !{!"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}
!18 = !{!19, !6, i64 832}
!19 = !{!"", !7, i64 0, !7, i64 32, !17, i64 64, !20, i64 152, !24, i64 336, !24, i64 528, !12, i64 720, !6, i64 728, !6, i64 736, !12, i64 744, !12, i64 748, !12, i64 752, !12, i64 756, !12, i64 760, !28, i64 768}
!20 = !{!"__UART_HandleTypeDef", !6, i64 0, !21, i64 8, !22, i64 44, !6, i64 88, !23, i64 96, !23, i64 98, !6, i64 104, !23, i64 112, !23, i64 114, !23, i64 116, !12, i64 120, !23, i64 124, !23, i64 126, !12, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !12, i64 168, !12, i64 172, !12, i64 176, !12, i64 180}
!21 = !{!"", !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}
!22 = !{!"", !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}
!23 = !{!"short", !7, i64 0}
!24 = !{!"", !25, i64 0, !12, i64 184, !27, i64 188}
!25 = !{!"__DMA_HandleTypeDef", !6, i64 0, !26, 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}
!26 = !{!"", !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}
!27 = !{!"", !12, i64 0}
!28 = !{!"", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !12, i64 40, !12, i64 44, !12, i64 48, !12, i64 52, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !12, i64 88, !29, i64 92, !29, i64 93, !29, i64 94, !29, i64 95, !29, i64 96}
!29 = !{!"_Bool", !7, i64 0}
!30 = !{!19, !12, i64 760}
!31 = !{!19, !12, i64 856}


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

@FreeRTOS_errno = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, -2147483648) i32 @usart_read(ptr nocapture noundef readonly %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds i8, ptr %5, i64 144
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %7, i64 832
  %9 = load ptr, ptr %8, align 8, !tbaa !15
  %10 = trunc i64 %2 to i32
  %11 = getelementptr inbounds i8, ptr %7, i64 760
  %12 = load i32, ptr %11, align 8, !tbaa !27
  %13 = tail call i32 @ring_buf_read_timeout(ptr noundef %9, ptr noundef %1, i32 noundef %10, i32 noundef %12) #2
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %.sink.split, label %15

15:                                               ; preds = %3
  %16 = getelementptr inbounds i8, ptr %7, i64 856
  %17 = load i32, ptr %16, align 8, !tbaa !28
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %18, label %.sink.split

.sink.split:                                      ; preds = %15, %3
  %.sink = phi i32 [ 116, %3 ], [ 71, %15 ]
  store i32 %.sink, ptr @FreeRTOS_errno, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %.sink.split, %15
  %.0 = phi i32 [ %13, %15 ], [ -1, %.sink.split ]
  ret i32 %.0
}

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


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