; LLVM IR from sskorol/respeaker-websockets :: src (cAPA102)

; 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.cAPA102_LEDs = type { i32, i32, ptr, i8 }

@cAPA012_BUF = external hidden global %struct.cAPA102_LEDs, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @cAPA102_Init(i32 noundef %0, i8 noundef zeroext %1, i8 noundef zeroext %2, i8 noundef zeroext %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !9
  store i8 %2, ptr %8, align 1, !tbaa !9
  store i8 %3, ptr %9, align 1, !tbaa !9
  %10 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %10, ptr @cAPA012_BUF, align 8, !tbaa !10
  %11 = load i8, ptr %9, align 1, !tbaa !9
  %12 = zext i8 %11 to i32
  %13 = icmp sgt i32 %12, 31
  br i1 %13, label %14, label %15

14:                                               ; preds = %4
  store i8 -1, ptr getelementptr inbounds (%struct.cAPA102_LEDs, ptr @cAPA012_BUF, i32 0, i32 3), align 8, !tbaa !13
  br label %21

15:                                               ; preds = %4
  %16 = load i8, ptr %9, align 1, !tbaa !9
  %17 = zext i8 %16 to i32
  %18 = and i32 31, %17
  %19 = or i32 224, %18
  %20 = trunc i32 %19 to i8
  store i8 %20, ptr getelementptr inbounds (%struct.cAPA102_LEDs, ptr @cAPA012_BUF, i32 0, i32 3), align 8, !tbaa !13
  br label %21

21:                                               ; preds = %15, %14
  %22 = load i32, ptr @cAPA012_BUF, align 8, !tbaa !10
  %23 = mul i32 %22, 4
  %24 = zext i32 %23 to i64
  %25 = call noalias ptr @malloc(i64 noundef %24) #2
  store ptr %25, ptr getelementptr inbounds (%struct.cAPA102_LEDs, ptr @cAPA012_BUF, i32 0, i32 2), align 8, !tbaa !14
  %26 = load i8, ptr %7, align 1, !tbaa !9
  %27 = load i8, ptr %8, align 1, !tbaa !9
  %28 = call i32 @cAPA102_Try_Open_SPI_Dev(i8 noundef zeroext 5, i8 noundef zeroext 10, i8 noundef zeroext %26, i8 noundef zeroext %27)
  store i32 %28, ptr getelementptr inbounds (%struct.cAPA102_LEDs, ptr @cAPA012_BUF, i32 0, i32 1), align 4, !tbaa !15
  %29 = load i32, ptr getelementptr inbounds (%struct.cAPA102_LEDs, ptr @cAPA012_BUF, i32 0, i32 1), align 4, !tbaa !15
  %30 = icmp eq i32 -1, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %21
  store i32 -1, ptr %5, align 4
  br label %33

32:                                               ; preds = %21
  call void @cAPA102_Clear_All()
  store i32 0, ptr %5, align 4
  br label %33

33:                                               ; preds = %32, %31
  %34 = load i32, ptr %5, align 4
  ret i32 %34
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i32 @cAPA102_Try_Open_SPI_Dev(i8 noundef zeroext, i8 noundef zeroext, i8 noundef zeroext, i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare dso_local void @cAPA102_Clear_All() #0

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 = { nounwind allocsize(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 allocsize(0) }

!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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"", !6, i64 0, !6, i64 4, !12, i64 8, !7, i64 16}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!11, !7, i64 16}
!14 = !{!11, !12, i64 8}
!15 = !{!11, !6, i64 4}


; 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.cAPA102_LEDs = type { i32, i32, ptr, i8 }

@cAPA012_BUF = external hidden local_unnamed_addr global %struct.cAPA102_LEDs, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @cAPA102_Init(i32 noundef %0, i8 noundef zeroext %1, i8 noundef zeroext %2, i8 noundef zeroext %3) local_unnamed_addr #0 {
  store i32 %0, ptr @cAPA012_BUF, align 8, !tbaa !5
  %5 = icmp ugt i8 %3, 31
  %6 = or disjoint i8 %3, -32
  %storemerge = select i1 %5, i8 -1, i8 %6
  store i8 %storemerge, ptr getelementptr inbounds (i8, ptr @cAPA012_BUF, i64 16), align 8, !tbaa !11
  %7 = shl i32 %0, 2
  %8 = zext i32 %7 to i64
  %9 = tail call noalias ptr @malloc(i64 noundef %8) #2
  store ptr %9, ptr getelementptr inbounds (i8, ptr @cAPA012_BUF, i64 8), align 8, !tbaa !12
  %10 = tail call i32 @cAPA102_Try_Open_SPI_Dev(i8 noundef zeroext 5, i8 noundef zeroext 10, i8 noundef zeroext %1, i8 noundef zeroext %2)
  store i32 %10, ptr getelementptr inbounds (i8, ptr @cAPA012_BUF, i64 4), align 4, !tbaa !13
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %13, label %12

12:                                               ; preds = %4
  tail call void @cAPA102_Clear_All()
  br label %13

13:                                               ; preds = %4, %12
  %.0 = phi i32 [ 0, %12 ], [ -1, %4 ]
  ret i32 %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @cAPA102_Try_Open_SPI_Dev(i8 noundef zeroext, i8 noundef zeroext, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @cAPA102_Clear_All() local_unnamed_addr #0

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !8, 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 = !{!6, !8, i64 16}
!12 = !{!6, !10, i64 8}
!13 = !{!6, !7, i64 4}


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