; 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
define dso_local i32 @SPI_read(i32 noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca %struct.spi_ioc_transfer, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 32, ptr %10) #4
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = call i32 @SPI_getBitsPerWord(i32 noundef %12)
  %14 = trunc i32 %13 to i8
  store i8 %14, ptr %8, align 1, !tbaa !11
  %15 = load i8, ptr %8, align 1, !tbaa !11
  %16 = zext i8 %15 to i32
  %17 = icmp slt i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %3
  %19 = load i8, ptr %8, align 1, !tbaa !11
  %20 = zext i8 %19 to i32
  store i32 %20, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %60

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

33:                                               ; preds = %21
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %60

34:                                               ; preds = %21
  %35 = load i32, ptr %9, align 4, !tbaa !5
  %36 = icmp ugt i32 %35, 4096
  br i1 %36, label %37, label %38

37:                                               ; preds = %34
  store i32 4096, ptr %9, align 4, !tbaa !5
  br label %38

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

53:                                               ; preds = %38
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %60

54:                                               ; preds = %38
  %55 = load i32, ptr %9, align 4, !tbaa !5
  %56 = shl i32 %55, 3
  %57 = load i8, ptr %8, align 1, !tbaa !11
  %58 = zext i8 %57 to i32
  %59 = udiv i32 %56, %58
  store i32 %59, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %60

60:                                               ; preds = %54, %53, %33, %18
  call void @llvm.lifetime.end.p0(i64 32, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #4
  %61 = load i32, ptr %4, align 4
  ret i32 %61
}

; 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

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
define dso_local range(i32 -1, -7) i32 @SPI_read(i32 noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = alloca %struct.spi_ioc_transfer, align 8
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #5
  %5 = tail call i32 @SPI_getBitsPerWord(i32 noundef %0)
  %6 = and i32 %5, 255
  %7 = mul nsw i32 %6, %2
  %8 = sitofp i32 %7 to float
  %9 = fpext float %8 to double
  %10 = fmul double %9, 1.250000e-01
  %11 = fadd double %10, 5.000000e-01
  %12 = fptoui double %11 to i32
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %24, label %13

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

22:                                               ; preds = %13
  %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 %6 to i16
  %23 = udiv i16 %.lhs.trunc, %.rhs.trunc
  %.zext = zext i16 %23 to i32
  br label %24

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

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

; Function Attrs: mustprogress 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, ...) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umin.i32(i32, i32) #4

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 = { mustprogress 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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 8}
!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, !10, i64 16}
!13 = !{!6, !11, i64 24}
!14 = !{!6, !8, i64 26}


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