; LLVM IR from graycatlabs/serbus :: src (spidriver)

; 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_ioc_transfer = type { i64, i64, i32, i32, i16, i8, i8, i8, i8, i8, i8 }

; 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
declare dso_local i32 @SPI_getBitsPerWord(i32 noundef) #1

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

; Function Attrs: nounwind
declare i32 @ioctl(i32 noundef, i64 noundef, ...) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @SPI_transfer(i32 noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  %12 = alloca %struct.spi_ioc_transfer, align 8
  %13 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr %12) #4
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = call i32 @SPI_getBitsPerWord(i32 noundef %14)
  %16 = trunc i32 %15 to i8
  store i8 %16, ptr %10, align 1, !tbaa !11
  %17 = load i8, ptr %10, align 1, !tbaa !11
  %18 = zext i8 %17 to i32
  %19 = icmp slt i32 %18, 0
  br i1 %19, label %20, label %23

20:                                               ; preds = %4
  %21 = load i8, ptr %10, align 1, !tbaa !11
  %22 = zext i8 %21 to i32
  store i32 %22, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %64

23:                                               ; preds = %4
  %24 = load i8, ptr %10, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = load i32, ptr %9, align 4, !tbaa !5
  %27 = mul nsw i32 %25, %26
  %28 = sitofp i32 %27 to float
  %29 = fpext float %28 to double
  %30 = fdiv double %29, 8.000000e+00
  %31 = fadd double %30, 5.000000e-01
  %32 = fptoui double %31 to i32
  store i32 %32, ptr %11, align 4, !tbaa !5
  %33 = load i32, ptr %11, align 4, !tbaa !5
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %36, label %35

35:                                               ; preds = %23
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %64

36:                                               ; preds = %23
  %37 = load i32, ptr %11, align 4, !tbaa !5
  %38 = icmp ugt i32 %37, 4096
  br i1 %38, label %39, label %40

39:                                               ; preds = %36
  store i32 4096, ptr %11, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %39, %36
  call void @llvm.memset.p0.i64(ptr align 8 %12, i8 0, i64 32, i1 false)
  %41 = load ptr, ptr %7, align 8, !tbaa !9
  %42 = ptrtoint ptr %41 to i64
  %43 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 0
  store i64 %42, ptr %43, align 8, !tbaa !12
  %44 = load ptr, ptr %8, align 8, !tbaa !9
  %45 = ptrtoint ptr %44 to i64
  %46 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 1
  store i64 %45, ptr %46, align 8, !tbaa !16
  %47 = load i32, ptr %11, align 4, !tbaa !5
  %48 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 2
  store i32 %47, ptr %48, align 8, !tbaa !17
  %49 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 3
  store i32 0, ptr %49, align 4, !tbaa !18
  %50 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 4
  store i16 0, ptr %50, align 8, !tbaa !19
  %51 = load i8, ptr %10, align 1, !tbaa !11
  %52 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 5
  store i8 %51, ptr %52, align 2, !tbaa !20
  %53 = getelementptr inbounds %struct.spi_ioc_transfer, ptr %12, i32 0, i32 6
  store i8 0, ptr %53, align 1, !tbaa !21
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = call i32 (i32, i64, ...) @ioctl(i32 noundef %54, i64 noundef 1075866368, ptr noundef %12) #4
  %56 = icmp slt i32 %55, 0
  br i1 %56, label %57, label %58

57:                                               ; preds = %40
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %64

58:                                               ; preds = %40
  %59 = load i32, ptr %11, align 4, !tbaa !5
  %60 = shl i32 %59, 3
  %61 = load i8, ptr %10, align 1, !tbaa !11
  %62 = zext i8 %61 to i32
  %63 = udiv i32 %60, %62
  store i32 %63, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %64

64:                                               ; preds = %58, %57, %35, %20
  call void @llvm.lifetime.end.p0(i64 32, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #4
  %65 = load i32, ptr %5, align 4
  ret i32 %65
}

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nounwind "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"spi_ioc_transfer", !14, i64 0, !14, i64 8, !6, i64 16, !6, i64 20, !15, i64 24, !7, i64 26, !7, i64 27, !7, i64 28, !7, i64 29, !7, i64 30, !7, i64 31}
!14 = !{!"long long", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!13, !14, i64 8}
!17 = !{!13, !6, i64 16}
!18 = !{!13, !6, i64 20}
!19 = !{!13, !15, i64 24}
!20 = !{!13, !7, i64 26}
!21 = !{!13, !7, i64 27}


; 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.spi_ioc_transfer = type { i64, i64, i32, i32, i16, i8, i8, i8, i8, i8, i8 }

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @SPI_getBitsPerWord(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @ioctl(i32 noundef, i64 noundef, ...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, -7) i32 @SPI_transfer(i32 noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.spi_ioc_transfer, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %5) #4
  %6 = tail call i32 @SPI_getBitsPerWord(i32 noundef %0)
  %7 = and i32 %6, 255
  %8 = mul nsw i32 %7, %3
  %9 = sitofp i32 %8 to float
  %10 = fpext float %9 to double
  %11 = fmul double %10, 1.250000e-01
  %12 = fadd double %11, 5.000000e-01
  %13 = fptoui double %12 to i32
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %27, label %14

14:                                               ; preds = %4
  %15 = trunc i32 %6 to i8
  %spec.store.select = tail call i32 @llvm.umin.i32(i32 %13, i32 4096)
  %16 = getelementptr inbounds i8, ptr %5, i64 24
  store i64 0, ptr %16, align 8
  %17 = ptrtoint ptr %1 to i64
  store i64 %17, ptr %5, align 8, !tbaa !5
  %18 = ptrtoint ptr %2 to i64
  %19 = getelementptr inbounds i8, ptr %5, i64 8
  store i64 %18, ptr %19, align 8, !tbaa !12
  %20 = getelementptr inbounds i8, ptr %5, i64 16
  store i32 %spec.store.select, ptr %20, align 8, !tbaa !13
  %21 = getelementptr inbounds i8, ptr %5, i64 20
  store i32 0, ptr %21, align 4, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %5, i64 26
  store i8 %15, ptr %22, align 2, !tbaa !15
  %23 = call i32 (i32, i64, ...) @ioctl(i32 noundef %0, i64 noundef 1075866368, ptr noundef nonnull %5) #4
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %27, label %25

25:                                               ; preds = %14
  %spec.store.select.tr = trunc nuw nsw i32 %spec.store.select to i16
  %.lhs.trunc = shl nuw i16 %spec.store.select.tr, 3
  %.rhs.trunc = trunc nuw nsw i32 %7 to i16
  %26 = udiv i16 %.lhs.trunc, %.rhs.trunc
  %.zext = zext i16 %26 to i32
  br label %27

27:                                               ; preds = %14, %4, %25
  %.0 = phi i32 [ %.zext, %25 ], [ 0, %4 ], [ -1, %14 ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %5) #4
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #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 = { nounwind "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 nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 0}
!6 = !{!"spi_ioc_transfer", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !11, i64 24, !8, i64 26, !8, i64 27, !8, i64 28, !8, i64 29, !8, i64 30, !8, i64 31}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !10, i64 20}
!15 = !{!6, !8, i64 26}


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