; LLVM IR from Try/Tempest :: Engine/thirdparty/libpng (pngmem)

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

@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

; Function Attrs: noreturn
declare void @png_error(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden ptr @png_malloc_array_checked(ptr noalias noundef, i32 noundef, i64 noundef) #4

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @png_realloc_array(ptr noalias noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i64 noundef %4) #4 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i64 %4, ptr %11, align 8, !tbaa !11
  %14 = load i32, ptr %10, align 4, !tbaa !9
  %15 = icmp sle i32 %14, 0
  br i1 %15, label %28, label %16

16:                                               ; preds = %5
  %17 = load i64, ptr %11, align 8, !tbaa !11
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %28, label %19

19:                                               ; preds = %16
  %20 = load i32, ptr %9, align 4, !tbaa !9
  %21 = icmp slt i32 %20, 0
  br i1 %21, label %28, label %22

22:                                               ; preds = %19
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %30

25:                                               ; preds = %22
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = icmp sgt i32 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %25, %19, %16, %5
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  call void @png_error(ptr noundef %29, ptr noundef @.str.1) #5
  unreachable

30:                                               ; preds = %25, %22
  %31 = load i32, ptr %10, align 4, !tbaa !9
  %32 = load i32, ptr %9, align 4, !tbaa !9
  %33 = sub nsw i32 2147483647, %32
  %34 = icmp sle i32 %31, %33
  br i1 %34, label %35, label %70

35:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = load i32, ptr %10, align 4, !tbaa !9
  %39 = add nsw i32 %37, %38
  %40 = load i64, ptr %11, align 8, !tbaa !11
  %41 = call ptr @png_malloc_array_checked(ptr noundef %36, i32 noundef %39, i64 noundef %40)
  store ptr %41, ptr %12, align 8, !tbaa !5
  %42 = load ptr, ptr %12, align 8, !tbaa !5
  %43 = icmp ne ptr %42, null
  br i1 %43, label %44, label %66

44:                                               ; preds = %35
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %47, label %54

47:                                               ; preds = %44
  %48 = load ptr, ptr %12, align 8, !tbaa !5
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = load i64, ptr %11, align 8, !tbaa !11
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = zext i32 %51 to i64
  %53 = mul i64 %50, %52
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %48, ptr align 1 %49, i64 %53, i1 false)
  br label %54

54:                                               ; preds = %47, %44
  %55 = load ptr, ptr %12, align 8, !tbaa !5
  %56 = load i64, ptr %11, align 8, !tbaa !11
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = zext i32 %57 to i64
  %59 = mul i64 %56, %58
  %60 = getelementptr inbounds i8, ptr %55, i64 %59
  %61 = load i64, ptr %11, align 8, !tbaa !11
  %62 = load i32, ptr %10, align 4, !tbaa !9
  %63 = zext i32 %62 to i64
  %64 = mul i64 %61, %63
  call void @llvm.memset.p0.i64(ptr align 1 %60, i8 0, i64 %64, i1 false)
  %65 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %65, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %67

66:                                               ; preds = %35
  store i32 0, ptr %13, align 4
  br label %67

67:                                               ; preds = %66, %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  %68 = load i32, ptr %13, align 4
  switch i32 %68, label %73 [
    i32 0, label %69
    i32 1, label %71
  ]

69:                                               ; preds = %67
  br label %70

70:                                               ; preds = %69, %30
  store ptr null, ptr %6, align 8
  br label %71

71:                                               ; preds = %70, %67
  %72 = load ptr, ptr %6, align 8
  ret ptr %72

73:                                               ; preds = %67
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { noreturn "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 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 #5 = { noreturn }
attributes #6 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}


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

@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: noreturn
declare void @png_error(ptr noundef, ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @png_realloc_array(ptr noalias noundef %0, ptr noundef readonly %1, i32 noundef %2, i32 noundef %3, i64 noundef %4) local_unnamed_addr #3 {
  %6 = icmp slt i32 %3, 1
  %7 = icmp eq i64 %4, 0
  %or.cond = or i1 %6, %7
  %8 = icmp slt i32 %2, 0
  %or.cond3 = or i1 %8, %or.cond
  br i1 %or.cond3, label %12, label %9

9:                                                ; preds = %5
  %10 = icmp eq ptr %1, null
  %11 = icmp ne i32 %2, 0
  %or.cond5 = and i1 %10, %11
  br i1 %or.cond5, label %12, label %13

12:                                               ; preds = %9, %5
  tail call void @png_error(ptr noundef %0, ptr noundef nonnull @.str.1) #4
  unreachable

13:                                               ; preds = %9
  %14 = sub nuw nsw i32 2147483647, %2
  %.not = icmp ult i32 %14, %3
  br i1 %.not, label %.thread, label %15

15:                                               ; preds = %13
  %16 = add nuw nsw i32 %3, %2
  %17 = tail call ptr @png_malloc_array_checked(ptr noundef %0, i32 noundef %16, i64 noundef %4)
  %.not35.not = icmp eq ptr %17, null
  br i1 %.not35.not, label %.thread, label %18

18:                                               ; preds = %15
  br i1 %11, label %19, label %._crit_edge

19:                                               ; preds = %18
  %20 = zext nneg i32 %2 to i64
  %21 = mul i64 %20, %4
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %17, ptr align 1 %1, i64 %21, i1 false)
  br label %._crit_edge

._crit_edge:                                      ; preds = %18, %19
  %.pre-phi38 = phi i64 [ %21, %19 ], [ 0, %18 ]
  %22 = getelementptr inbounds i8, ptr %17, i64 %.pre-phi38
  %23 = zext nneg i32 %3 to i64
  %24 = mul i64 %23, %4
  tail call void @llvm.memset.p0.i64(ptr nonnull align 1 %22, i8 0, i64 %24, i1 false)
  br label %.thread

.thread:                                          ; preds = %15, %13, %._crit_edge
  %.1 = phi ptr [ %17, %._crit_edge ], [ null, %13 ], [ null, %15 ]
  ret ptr %.1
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { noreturn "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 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 = { noreturn 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)"}


; Effective O3 passes: ADCEPass,BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass