; LLVM IR from Simple-XX/SimpleKernel :: src/kernel/firstfit.ll (ZNK8FIRSTFIT8find_lenEmb)

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

%class.ALLOCATOR = type { ptr, ptr, i64, i64, i64, i64 }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144), i64 noundef) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZNK8FIRSTFIT8find_lenEmb(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %1, i1 noundef zeroext %2) #0 align 2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  %12 = zext i1 %2 to i8
  store i8 %12, ptr %7, align 1, !tbaa !11
  %13 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %41, %3
  %15 = load i64, ptr %10, align 8, !tbaa !9
  %16 = getelementptr inbounds %class.ALLOCATOR, ptr %13, i32 0, i32 3
  %17 = load i64, ptr %16, align 8, !tbaa !13
  %18 = icmp ult i64 %15, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %14
  store i32 2, ptr %11, align 4
  br label %44

20:                                               ; preds = %14
  %21 = load i64, ptr %10, align 8, !tbaa !9
  %22 = call noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144) %13, i64 noundef %21)
  %23 = zext i1 %22 to i32
  %24 = load i8, ptr %7, align 1, !tbaa !11, !range !15, !noundef !16
  %25 = trunc i8 %24 to i1
  %26 = zext i1 %25 to i32
  %27 = icmp ne i32 %23, %26
  br i1 %27, label %28, label %31

28:                                               ; preds = %20
  store i64 0, ptr %8, align 8, !tbaa !9
  %29 = load i64, ptr %10, align 8, !tbaa !9
  %30 = add i64 %29, 1
  store i64 %30, ptr %9, align 8, !tbaa !9
  br label %34

31:                                               ; preds = %20
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = add i64 %32, 1
  store i64 %33, ptr %8, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %31, %28
  %35 = load i64, ptr %8, align 8, !tbaa !9
  %36 = load i64, ptr %6, align 8, !tbaa !9
  %37 = icmp eq i64 %35, %36
  br i1 %37, label %38, label %40

38:                                               ; preds = %34
  %39 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %39, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %44

40:                                               ; preds = %34
  br label %41

41:                                               ; preds = %40
  %42 = load i64, ptr %10, align 8, !tbaa !9
  %43 = add i64 %42, 1
  store i64 %43, ptr %10, align 8, !tbaa !9
  br label %14, !llvm.loop !17

44:                                               ; preds = %38, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  %45 = load i32, ptr %11, align 4
  switch i32 %45, label %47 [
    i32 2, label %46
  ]

46:                                               ; preds = %44
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %47

47:                                               ; preds = %46, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %48 = load i64, ptr %4, align 8
  ret i64 %48
}

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!14, !10, i64 24}
!14 = !{!"_ZTS9ALLOCATOR", !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_ZNK8FIRSTFIT8find_lenEmb(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %1, i1 noundef zeroext %2) local_unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  br label %5

5:                                                ; preds = %7, %3
  %.013 = phi i64 [ 0, %3 ], [ %.114, %7 ]
  %.012 = phi i64 [ 0, %3 ], [ %.1, %7 ]
  %.011 = phi i64 [ 0, %3 ], [ %10, %7 ]
  %6 = load i64, ptr %4, align 8, !tbaa !5
  %.not = icmp ult i64 %.011, %6
  br i1 %.not, label %7, label %13

7:                                                ; preds = %5
  %8 = tail call noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %.011)
  %9 = xor i1 %8, %2
  %10 = add nuw i64 %.011, 1
  %11 = add i64 %.013, 1
  %.114 = select i1 %9, i64 0, i64 %11
  %.1 = select i1 %9, i64 %10, i64 %.012
  %12 = icmp eq i64 %.114, %1
  br i1 %12, label %13, label %5, !llvm.loop !11

13:                                               ; preds = %7, %5
  %spec.select = phi i64 [ -1, %5 ], [ %.1, %7 ]
  ret i64 %spec.select
}

attributes #0 = { 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" }

!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, !10, i64 24}
!6 = !{!"_ZTS9ALLOCATOR", !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass