; LLVM IR from IOsonata/IOsonata :: src/stddev.ll (read)

; 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.StdDev_t = type { [8 x i8], ptr, ptr, ptr, ptr, ptr, ptr }

@g_DevTable = external dso_local global [6 x ptr], align 16

; 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 @_read(i32 noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = and i32 %10, 15
  store i32 %11, ptr %8, align 4, !tbaa !5
  %12 = load i32, ptr %8, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %17, label %14

14:                                               ; preds = %3
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = icmp sge i32 %15, 6
  br i1 %16, label %17, label %18

17:                                               ; preds = %14, %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %56

18:                                               ; preds = %14
  %19 = load i32, ptr %8, align 4, !tbaa !5
  %20 = icmp sge i32 %19, 3
  br i1 %20, label %21, label %24

21:                                               ; preds = %18
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = ashr i32 %22, 4
  store i32 %23, ptr %5, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %21, %18
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [6 x ptr], ptr @g_DevTable, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %55

30:                                               ; preds = %24
  %31 = load i32, ptr %8, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [6 x ptr], ptr @g_DevTable, i64 0, i64 %32
  %34 = load ptr, ptr %33, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.StdDev_t, ptr %34, i32 0, i32 4
  %36 = load ptr, ptr %35, align 4, !tbaa !13
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %55

38:                                               ; preds = %30
  %39 = load i32, ptr %8, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [6 x ptr], ptr @g_DevTable, i64 0, i64 %40
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.StdDev_t, ptr %42, i32 0, i32 4
  %44 = load ptr, ptr %43, align 4, !tbaa !13
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [6 x ptr], ptr @g_DevTable, i64 0, i64 %46
  %48 = load ptr, ptr %47, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.StdDev_t, ptr %48, i32 0, i32 1
  %50 = load ptr, ptr %49, align 4, !tbaa !15
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !9
  %53 = load i64, ptr %7, align 8, !tbaa !11
  %54 = call i32 %44(ptr noundef %50, i32 noundef %51, ptr noundef %52, i64 noundef %53)
  store i32 %54, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %56

55:                                               ; preds = %30, %24
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %56

56:                                               ; preds = %55, %38, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %57 = load i32, ptr %4, align 4
  ret i32 %57
}

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 = { 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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 32}
!14 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!15 = !{!14, !10, i64 8}


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

@g_DevTable = external dso_local local_unnamed_addr global [6 x ptr], align 16

; Function Attrs: nounwind uwtable
define dso_local i32 @_read(i32 noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = and i32 %0, 15
  %5 = icmp ugt i32 %4, 5
  br i1 %5, label %19, label %6

6:                                                ; preds = %3
  %7 = icmp ugt i32 %4, 2
  %8 = ashr i32 %0, 4
  %spec.select = select i1 %7, i32 %8, i32 %0
  %9 = zext nneg i32 %4 to i64
  %10 = getelementptr inbounds [6 x ptr], ptr @g_DevTable, i64 0, i64 %9
  %11 = load ptr, ptr %10, align 8, !tbaa !5
  %.not = icmp eq ptr %11, null
  br i1 %.not, label %19, label %12

12:                                               ; preds = %6
  %13 = getelementptr inbounds i8, ptr %11, i64 32
  %14 = load ptr, ptr %13, align 4, !tbaa !9
  %.not17 = icmp eq ptr %14, null
  br i1 %.not17, label %19, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %11, i64 8
  %17 = load ptr, ptr %16, align 4, !tbaa !11
  %18 = tail call i32 %14(ptr noundef %17, i32 noundef %spec.select, ptr noundef %1, i64 noundef %2) #1
  br label %19

19:                                               ; preds = %6, %12, %3, %15
  %.0 = phi i32 [ %18, %15 ], [ -1, %3 ], [ -1, %12 ], [ -1, %6 ]
  ret i32 %.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 }

!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, !6, i64 32}
!10 = !{!"", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!11 = !{!10, !6, i64 8}


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