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

; 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.can_frame_t = type { %struct.can_header_t, [8 x i8], ptr }
%struct.can_header_t = type { i32, i32, i32 }
%struct.file = type { i32, ptr, ptr }
%struct.can_channel_t = type { ptr, ptr, i32, i32, i32, i32, ptr, ptr }

@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 @can_write(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
  %11 = alloca %struct.can_frame_t, align 8
  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) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.file, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  store ptr %14, ptr %9, align 8, !tbaa !5
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !14
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = icmp ugt i64 %15, 8
  br i1 %16, label %17, label %18

17:                                               ; preds = %3
  store i32 90, ptr @FreeRTOS_errno, align 4, !tbaa !14
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %58

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 32, ptr %11) #4
  %19 = getelementptr inbounds %struct.can_frame_t, ptr %11, i32 0, i32 0
  %20 = getelementptr inbounds %struct.can_header_t, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.can_channel_t, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 8, !tbaa !15
  store i32 %23, ptr %20, align 8, !tbaa !17
  %24 = getelementptr inbounds %struct.can_header_t, ptr %19, i32 0, i32 1
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.can_channel_t, ptr %25, i32 0, i32 5
  %27 = load i32, ptr %26, align 4, !tbaa !19
  store i32 %27, ptr %24, align 4, !tbaa !20
  %28 = getelementptr inbounds %struct.can_header_t, ptr %19, i32 0, i32 2
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = trunc i64 %29 to i32
  store i32 %30, ptr %28, align 8, !tbaa !21
  %31 = getelementptr inbounds %struct.can_frame_t, ptr %11, i32 0, i32 1
  store i8 0, ptr %31, align 1, !tbaa !22
  %32 = getelementptr inbounds i8, ptr %31, i64 1
  %33 = getelementptr inbounds i8, ptr %31, i64 8
  br label %34

34:                                               ; preds = %34, %18
  %35 = phi ptr [ %32, %18 ], [ %36, %34 ]
  store i8 0, ptr %35, align 1, !tbaa !22
  %36 = getelementptr inbounds i8, ptr %35, i64 1
  %37 = icmp eq ptr %36, %33
  br i1 %37, label %38, label %34

38:                                               ; preds = %34
  %39 = getelementptr inbounds %struct.can_frame_t, ptr %11, i32 0, i32 2
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %40, ptr %39, align 8, !tbaa !23
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.can_channel_t, ptr %41, i32 0, i32 5
  %43 = load i32, ptr %42, align 4, !tbaa !19
  %44 = icmp eq i32 %43, 0
  br i1 %44, label %45, label %50

45:                                               ; preds = %38
  %46 = getelementptr inbounds %struct.can_frame_t, ptr %11, i32 0, i32 1
  %47 = getelementptr inbounds [8 x i8], ptr %46, i64 0, i64 0
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %7, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %47, ptr align 1 %48, i64 %49, i1 false)
  br label %50

50:                                               ; preds = %45, %38
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.can_channel_t, ptr %51, i32 0, i32 6
  %53 = load ptr, ptr %52, align 8, !tbaa !25
  %54 = call i64 @xQueueGenericSend(ptr noundef %53, ptr noundef %11, i32 noundef -1, i64 noundef 0)
  %55 = load i64, ptr %7, align 8, !tbaa !9
  %56 = trunc i64 %55 to i32
  store i32 %56, ptr %8, align 4, !tbaa !14
  %57 = load i32, ptr %8, align 4, !tbaa !14
  store i32 %57, ptr %4, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 32, ptr %11) #4
  br label %58

58:                                               ; preds = %50, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %59 = load i32, ptr %4, align 4
  ret i32 %59
}

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!12, !6, i64 16}
!12 = !{!"file", !13, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!16, !13, i64 16}
!16 = !{!"", !6, i64 0, !6, i64 8, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28, !6, i64 32, !6, i64 40}
!17 = !{!18, !13, i64 0}
!18 = !{!"", !13, i64 0, !13, i64 4, !13, i64 8}
!19 = !{!16, !13, i64 28}
!20 = !{!18, !13, i64 4}
!21 = !{!18, !13, i64 8}
!22 = !{!7, !7, i64 0}
!23 = !{!24, !6, i64 24}
!24 = !{!"", !18, i64 0, !7, i64 12, !6, i64 24}
!25 = !{!16, !6, i64 32}


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

%struct.can_frame_t = type { %struct.can_header_t, [8 x i8], ptr }
%struct.can_header_t = type { i32, i32, i32 }

@FreeRTOS_errno = external local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress 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 range(i32 -1, 9) i32 @can_write(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = alloca %struct.can_frame_t, align 8
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  store i32 0, ptr @FreeRTOS_errno, align 4, !tbaa !11
  %7 = icmp ugt i64 %2, 8
  br i1 %7, label %8, label %9

8:                                                ; preds = %3
  store i32 90, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %25

9:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #4
  %10 = getelementptr inbounds i8, ptr %6, i64 16
  %11 = load i32, ptr %10, align 8, !tbaa !12
  store i32 %11, ptr %4, align 8, !tbaa !14
  %12 = getelementptr inbounds i8, ptr %4, i64 4
  %13 = getelementptr inbounds i8, ptr %6, i64 28
  %14 = load i32, ptr %13, align 4, !tbaa !16
  store i32 %14, ptr %12, align 4, !tbaa !17
  %15 = getelementptr inbounds i8, ptr %4, i64 8
  %16 = trunc nuw nsw i64 %2 to i32
  store i32 %16, ptr %15, align 8, !tbaa !18
  %17 = getelementptr inbounds i8, ptr %4, i64 12
  %18 = getelementptr inbounds i8, ptr %4, i64 24
  store i64 0, ptr %17, align 4
  store ptr %6, ptr %18, align 8, !tbaa !19
  %19 = icmp eq i32 %14, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %9
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 4 %17, ptr align 1 %1, i64 %2, i1 false)
  br label %21

21:                                               ; preds = %20, %9
  %22 = getelementptr inbounds i8, ptr %6, i64 32
  %23 = load ptr, ptr %22, align 8, !tbaa !21
  %24 = call i64 @xQueueGenericSend(ptr noundef %23, ptr noundef nonnull %4, i32 noundef -1, i64 noundef 0) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #4
  br label %25

25:                                               ; preds = %21, %8
  %.0 = phi i32 [ -1, %8 ], [ %16, %21 ]
  ret i32 %.0
}

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 16}
!6 = !{!"file", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !7, i64 16}
!13 = !{!"", !10, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !10, i64 32, !10, i64 40}
!14 = !{!15, !7, i64 0}
!15 = !{!"", !7, i64 0, !7, i64 4, !7, i64 8}
!16 = !{!13, !7, i64 28}
!17 = !{!15, !7, i64 4}
!18 = !{!15, !7, i64 8}
!19 = !{!20, !10, i64 24}
!20 = !{!"", !15, i64 0, !8, i64 12, !10, i64 24}
!21 = !{!13, !10, i64 32}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass