; LLVM IR from aap/librw :: src/lodepng/lodepng.ll (Z23lode_png_test_bitreaderPKhmmPKmPj)

; 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.LodePNGBitReader = type { ptr, i64, i64, i64, 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 uwtable
define dso_local noundef i32 @_Z23lode_png_test_bitreaderPKhmmPKmPj(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca %struct.LodePNGBitReader, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !9
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 40, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load i64, ptr %8, align 8, !tbaa !9
  %20 = call noundef i32 @_ZL21LodePNGBitReader_initP16LodePNGBitReaderPKhm(ptr noundef %13, ptr noundef %18, i64 noundef %19)
  store i32 %20, ptr %14, align 4, !tbaa !11
  %21 = load i32, ptr %14, align 4, !tbaa !11
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %5
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %73

24:                                               ; preds = %5
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %69, %24
  %26 = load i64, ptr %12, align 8, !tbaa !9
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = icmp ult i64 %26, %27
  br i1 %28, label %29, label %72

29:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = load i64, ptr %12, align 8, !tbaa !9
  %32 = getelementptr inbounds i64, ptr %30, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !9
  store i64 %33, ptr %16, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  %34 = load i64, ptr %16, align 8, !tbaa !9
  %35 = icmp ugt i64 %34, 25
  br i1 %35, label %36, label %39

36:                                               ; preds = %29
  %37 = load i64, ptr %16, align 8, !tbaa !9
  %38 = call noundef i32 @_ZL12ensureBits32P16LodePNGBitReaderm(ptr noundef %13, i64 noundef %37)
  store i32 %38, ptr %17, align 4, !tbaa !11
  br label %56

39:                                               ; preds = %29
  %40 = load i64, ptr %16, align 8, !tbaa !9
  %41 = icmp ugt i64 %40, 17
  br i1 %41, label %42, label %45

42:                                               ; preds = %39
  %43 = load i64, ptr %16, align 8, !tbaa !9
  %44 = call noundef i32 @_ZL12ensureBits25P16LodePNGBitReaderm(ptr noundef %13, i64 noundef %43)
  store i32 %44, ptr %17, align 4, !tbaa !11
  br label %55

45:                                               ; preds = %39
  %46 = load i64, ptr %16, align 8, !tbaa !9
  %47 = icmp ugt i64 %46, 9
  br i1 %47, label %48, label %51

48:                                               ; preds = %45
  %49 = load i64, ptr %16, align 8, !tbaa !9
  %50 = call noundef i32 @_ZL12ensureBits17P16LodePNGBitReaderm(ptr noundef %13, i64 noundef %49)
  store i32 %50, ptr %17, align 4, !tbaa !11
  br label %54

51:                                               ; preds = %45
  %52 = load i64, ptr %16, align 8, !tbaa !9
  %53 = call noundef i32 @_ZL11ensureBits9P16LodePNGBitReaderm(ptr noundef %13, i64 noundef %52)
  store i32 %53, ptr %17, align 4, !tbaa !11
  br label %54

54:                                               ; preds = %51, %48
  br label %55

55:                                               ; preds = %54, %42
  br label %56

56:                                               ; preds = %55, %36
  %57 = load i32, ptr %17, align 4, !tbaa !11
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %60, label %59

59:                                               ; preds = %56
  store i32 0, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %66

60:                                               ; preds = %56
  %61 = load i64, ptr %16, align 8, !tbaa !9
  %62 = call noundef i32 @_ZL8readBitsP16LodePNGBitReaderm(ptr noundef %13, i64 noundef %61)
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  %64 = load i64, ptr %12, align 8, !tbaa !9
  %65 = getelementptr inbounds i32, ptr %63, i64 %64
  store i32 %62, ptr %65, align 4, !tbaa !11
  store i32 0, ptr %15, align 4
  br label %66

66:                                               ; preds = %60, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  %67 = load i32, ptr %15, align 4
  switch i32 %67, label %73 [
    i32 0, label %68
  ]

68:                                               ; preds = %66
  br label %69

69:                                               ; preds = %68
  %70 = load i64, ptr %12, align 8, !tbaa !9
  %71 = add i64 %70, 1
  store i64 %71, ptr %12, align 8, !tbaa !9
  br label %25, !llvm.loop !13

72:                                               ; preds = %25
  store i32 1, ptr %6, align 4
  store i32 1, ptr %15, align 4
  br label %73

73:                                               ; preds = %72, %66, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 40, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  %74 = load i32, ptr %6, align 4
  ret i32 %74
}

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL21LodePNGBitReader_initP16LodePNGBitReaderPKhm(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL12ensureBits32P16LodePNGBitReaderm(ptr noundef, i64 noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL12ensureBits25P16LodePNGBitReaderm(ptr noundef, i64 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL12ensureBits17P16LodePNGBitReaderm(ptr noundef, i64 noundef) #3

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL11ensureBits9P16LodePNGBitReaderm(ptr noundef, i64 noundef) #3

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL8readBitsP16LodePNGBitReaderm(ptr noundef, i64 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { inlinehint 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 #3 = { 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 #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 = !{!"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 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.LodePNGBitReader = type { ptr, i64, i64, i64, i32 }

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z23lode_png_test_bitreaderPKhmmPKmPj(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr nocapture noundef readonly %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #1 {
  %6 = alloca %struct.LodePNGBitReader, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %6) #4
  %7 = call noundef i32 @_ZL21LodePNGBitReader_initP16LodePNGBitReaderPKhm(ptr noundef nonnull %6, ptr noundef %0, i64 noundef %1)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %.preheader, label %.critedge

.preheader:                                       ; preds = %5
  %.not29 = icmp eq i64 %2, 0
  br i1 %.not29, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %24
  %.02226 = phi i64 [ %27, %24 ], [ 0, %.preheader ]
  %8 = getelementptr inbounds i64, ptr %3, i64 %.02226
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %10 = icmp ugt i64 %9, 25
  br i1 %10, label %11, label %13

11:                                               ; preds = %.lr.ph
  %12 = call noundef i32 @_ZL12ensureBits32P16LodePNGBitReaderm(ptr noundef nonnull %6, i64 noundef %9)
  br label %23

13:                                               ; preds = %.lr.ph
  %14 = icmp ugt i64 %9, 17
  br i1 %14, label %15, label %17

15:                                               ; preds = %13
  %16 = call noundef i32 @_ZL12ensureBits25P16LodePNGBitReaderm(ptr noundef nonnull %6, i64 noundef %9)
  br label %23

17:                                               ; preds = %13
  %18 = icmp ugt i64 %9, 9
  br i1 %18, label %19, label %21

19:                                               ; preds = %17
  %20 = call noundef i32 @_ZL12ensureBits17P16LodePNGBitReaderm(ptr noundef nonnull %6, i64 noundef %9)
  br label %23

21:                                               ; preds = %17
  %22 = call noundef i32 @_ZL11ensureBits9P16LodePNGBitReaderm(ptr noundef nonnull %6, i64 noundef %9)
  br label %23

23:                                               ; preds = %15, %21, %19, %11
  %.0 = phi i32 [ %12, %11 ], [ %16, %15 ], [ %20, %19 ], [ %22, %21 ]
  %.not25.not = icmp eq i32 %.0, 0
  br i1 %.not25.not, label %.critedge, label %24

24:                                               ; preds = %23
  %25 = call noundef i32 @_ZL8readBitsP16LodePNGBitReaderm(ptr noundef nonnull %6, i64 noundef %9)
  %26 = getelementptr inbounds i32, ptr %4, i64 %.02226
  store i32 %25, ptr %26, align 4, !tbaa !9
  %27 = add nuw i64 %.02226, 1
  %exitcond.not = icmp eq i64 %27, %2
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !11

.critedge:                                        ; preds = %24, %23, %.preheader, %5
  %.2 = phi i32 [ 0, %5 ], [ 1, %.preheader ], [ 1, %24 ], [ 0, %23 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %6) #4
  ret i32 %.2
}

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL21LodePNGBitReader_initP16LodePNGBitReaderPKhm(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL12ensureBits32P16LodePNGBitReaderm(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL12ensureBits25P16LodePNGBitReaderm(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL12ensureBits17P16LodePNGBitReaderm(ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL11ensureBits9P16LodePNGBitReaderm(ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL8readBitsP16LodePNGBitReaderm(ptr noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { inlinehint 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 #3 = { 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 #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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass