; LLVM IR from aap/librw :: src/skin.ll (ZN2rw4Skin14findNumWeightsEi)

; 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.rw::Skin" = type { i32, i32, i32, ptr, ptr, ptr, ptr, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, i32 }

; 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: mustprogress nounwind uwtable
define dso_local void @_ZN2rw4Skin14findNumWeightsEi(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %"struct.rw::Skin", ptr %7, i32 0, i32 2
  store i32 1, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %9 = getelementptr inbounds %"struct.rw::Skin", ptr %7, i32 0, i32 6
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  store ptr %10, ptr %5, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %33, %2
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %4, align 4, !tbaa !9
  %14 = icmp ne i32 %12, 0
  br i1 %14, label %15, label %36

15:                                               ; preds = %11
  br label %16

16:                                               ; preds = %32, %15
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %"struct.rw::Skin", ptr %7, i32 0, i32 2
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds float, ptr %17, i64 %20
  %22 = load float, ptr %21, align 4, !tbaa !14
  %23 = fcmp une float %22, 0.000000e+00
  br i1 %23, label %24, label %33

24:                                               ; preds = %16
  %25 = getelementptr inbounds %"struct.rw::Skin", ptr %7, i32 0, i32 2
  %26 = load i32, ptr %25, align 8, !tbaa !11
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %25, align 8, !tbaa !11
  %28 = getelementptr inbounds %"struct.rw::Skin", ptr %7, i32 0, i32 2
  %29 = load i32, ptr %28, align 8, !tbaa !11
  %30 = icmp eq i32 %29, 4
  br i1 %30, label %31, label %32

31:                                               ; preds = %24
  store i32 1, ptr %6, align 4
  br label %37

32:                                               ; preds = %24
  br label %16, !llvm.loop !16

33:                                               ; preds = %16
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds float, ptr %34, i64 4
  store ptr %35, ptr %5, align 8, !tbaa !5
  br label %11, !llvm.loop !18

36:                                               ; preds = %11
  store i32 0, ptr %6, align 4
  br label %37

37:                                               ; preds = %36, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %38 = load i32, ptr %6, align 4
  switch i32 %38, label %40 [
    i32 0, label %39
    i32 1, label %39
  ]

39:                                               ; preds = %37, %37
  ret void

40:                                               ; preds = %37
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTSN2rw4SkinE", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !10, i64 104}
!13 = !{!12, !6, i64 40}
!14 = !{!15, !15, i64 0}
!15 = !{!"float", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN2rw4Skin14findNumWeightsEi(ptr nocapture noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 1, ptr %3, align 8, !tbaa !5
  %.not9 = icmp eq i32 %1, 0
  br i1 %.not9, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 40
  %5 = load ptr, ptr %4, align 8, !tbaa !11
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %14
  %.in = phi i32 [ %6, %14 ], [ %1, %.preheader.preheader ]
  %.011 = phi ptr [ %15, %14 ], [ %5, %.preheader.preheader ]
  %.promoted710 = phi i64 [ %indvars.iv, %14 ], [ 1, %.preheader.preheader ]
  %6 = add nsw i32 %.in, -1
  %sext = shl i64 %.promoted710, 32
  %7 = ashr exact i64 %sext, 32
  br label %8

8:                                                ; preds = %.preheader, %12
  %indvars.iv = phi i64 [ %7, %.preheader ], [ %indvars.iv.next, %12 ]
  %9 = getelementptr inbounds float, ptr %.011, i64 %indvars.iv
  %10 = load float, ptr %9, align 4, !tbaa !12
  %11 = fcmp une float %10, 0.000000e+00
  br i1 %11, label %12, label %14

12:                                               ; preds = %8
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %13 = trunc i64 %indvars.iv.next to i32
  store i32 %13, ptr %3, align 8, !tbaa !5
  %exitcond = icmp eq i32 %13, 4
  br i1 %exitcond, label %.loopexit, label %8, !llvm.loop !14

14:                                               ; preds = %8
  %15 = getelementptr inbounds i8, ptr %.011, i64 16
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %.loopexit, label %.preheader, !llvm.loop !16

.loopexit:                                        ; preds = %14, %12, %2
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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" }

!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 = !{!"_ZTSN2rw4SkinE", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !7, i64 48, !7, i64 52, !7, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !7, i64 104}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 40}
!12 = !{!13, !13, i64 0}
!13 = !{!"float", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass