; LLVM IR from alibaba/GCanvas :: core/test/linux/util/lodepng.ll (Z32lodepng_color_mode_alloc_paletteP16LodePNGColorMode)

; 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.LodePNGColorMode = type { i32, i32, ptr, i64, i32, i32, i32, 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
declare hidden noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef, i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z32lodepng_color_mode_alloc_paletteP16LodePNGColorMode(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.LodePNGColorMode, ptr %5, i32 0, i32 2
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = call noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef %7, i64 noundef 1024)
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.LodePNGColorMode, ptr %9, i32 0, i32 2
  store ptr %8, ptr %10, align 8, !tbaa !9
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.LodePNGColorMode, ptr %11, i32 0, i32 2
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %16, label %15

15:                                               ; preds = %1
  store i32 1, ptr %4, align 4
  br label %53

16:                                               ; preds = %1
  store i64 0, ptr %3, align 8, !tbaa !14
  br label %17

17:                                               ; preds = %49, %16
  %18 = load i64, ptr %3, align 8, !tbaa !14
  %19 = icmp ne i64 %18, 256
  br i1 %19, label %20, label %52

20:                                               ; preds = %17
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.LodePNGColorMode, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = load i64, ptr %3, align 8, !tbaa !14
  %25 = mul i64 %24, 4
  %26 = add i64 %25, 0
  %27 = getelementptr inbounds i8, ptr %23, i64 %26
  store i8 0, ptr %27, align 1, !tbaa !15
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.LodePNGColorMode, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !9
  %31 = load i64, ptr %3, align 8, !tbaa !14
  %32 = mul i64 %31, 4
  %33 = add i64 %32, 1
  %34 = getelementptr inbounds i8, ptr %30, i64 %33
  store i8 0, ptr %34, align 1, !tbaa !15
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.LodePNGColorMode, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !9
  %38 = load i64, ptr %3, align 8, !tbaa !14
  %39 = mul i64 %38, 4
  %40 = add i64 %39, 2
  %41 = getelementptr inbounds i8, ptr %37, i64 %40
  store i8 0, ptr %41, align 1, !tbaa !15
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.LodePNGColorMode, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  %45 = load i64, ptr %3, align 8, !tbaa !14
  %46 = mul i64 %45, 4
  %47 = add i64 %46, 3
  %48 = getelementptr inbounds i8, ptr %44, i64 %47
  store i8 -1, ptr %48, align 1, !tbaa !15
  br label %49

49:                                               ; preds = %20
  %50 = load i64, ptr %3, align 8, !tbaa !14
  %51 = add i64 %50, 1
  store i64 %51, ptr %3, align 8, !tbaa !14
  br label %17, !llvm.loop !16

52:                                               ; preds = %17
  store i32 0, ptr %4, align 4
  br label %53

53:                                               ; preds = %52, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  %54 = load i32, ptr %4, align 4
  switch i32 %54, label %56 [
    i32 0, label %55
    i32 1, label %55
  ]

55:                                               ; preds = %53, %53
  ret void

56:                                               ; preds = %53
  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 = { 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 #3 = { 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, !6, i64 8}
!10 = !{!"_ZTS16LodePNGColorMode", !11, i64 0, !12, i64 4, !6, i64 8, !13, i64 16, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36}
!11 = !{!"_ZTS16LodePNGColorType", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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 hidden noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z32lodepng_color_mode_alloc_paletteP16LodePNGColorMode(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = tail call noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef %3, i64 noundef 1024)
  store ptr %4, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1, %.preheader
  %.013 = phi i64 [ %17, %.preheader ], [ 0, %1 ]
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = shl i64 %.013, 2
  %7 = getelementptr inbounds i8, ptr %5, i64 %6
  store i8 0, ptr %7, align 1, !tbaa !13
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = or disjoint i64 %6, 1
  %10 = getelementptr inbounds i8, ptr %8, i64 %9
  store i8 0, ptr %10, align 1, !tbaa !13
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = or disjoint i64 %6, 2
  %13 = getelementptr inbounds i8, ptr %11, i64 %12
  store i8 0, ptr %13, align 1, !tbaa !13
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = or disjoint i64 %6, 3
  %16 = getelementptr inbounds i8, ptr %14, i64 %15
  store i8 -1, ptr %16, align 1, !tbaa !13
  %17 = add nuw nsw i64 %.013, 1
  %.not12 = icmp eq i64 %17, 256
  br i1 %.not12, label %.loopexit, label %.preheader, !llvm.loop !14

.loopexit:                                        ; preds = %.preheader, %1
  ret void
}

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, !11, i64 8}
!6 = !{!"_ZTS16LodePNGColorMode", !7, i64 0, !10, i64 4, !11, i64 8, !12, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36}
!7 = !{!"_ZTS16LodePNGColorType", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!8, !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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