; LLVM IR from ewconnell/Netlib :: Sources/ImageCodecs (png)

; 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.BufferItem = type { ptr, i64, i64, ptr, ptr, ptr }

; 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: 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: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @writeFn(ptr noundef %0, ptr noundef %1, i64 noundef %2) #3 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = call ptr @png_get_io_ptr(ptr noundef %13)
  store ptr %14, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.BufferItem, ptr %15, i32 0, i32 4
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  store ptr %17, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.BufferItem, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.BufferItem, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !14
  %24 = sub i64 %20, %23
  store i64 %24, ptr %9, align 8, !tbaa !9
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = load i64, ptr %9, align 8, !tbaa !9
  %27 = icmp ule i64 %25, %26
  br i1 %27, label %28, label %43

28:                                               ; preds = %3
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.BufferItem, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !15
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.BufferItem, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !14
  %35 = getelementptr inbounds i8, ptr %31, i64 %34
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %35, ptr align 1 %36, i64 %37, i1 false)
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.BufferItem, ptr %39, i32 0, i32 2
  %41 = load i64, ptr %40, align 8, !tbaa !14
  %42 = add i64 %41, %38
  store i64 %42, ptr %40, align 8, !tbaa !14
  br label %116

43:                                               ; preds = %3
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.BufferItem, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.BufferItem, ptr %47, i32 0, i32 2
  %49 = load i64, ptr %48, align 8, !tbaa !14
  %50 = getelementptr inbounds i8, ptr %46, i64 %49
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i64, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %50, ptr align 1 %51, i64 %52, i1 false)
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.BufferItem, ptr %53, i32 0, i32 1
  %55 = load i64, ptr %54, align 8, !tbaa !13
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.BufferItem, ptr %56, i32 0, i32 2
  store i64 %55, ptr %57, align 8, !tbaa !14
  %58 = load i64, ptr %9, align 8, !tbaa !9
  %59 = load i64, ptr %6, align 8, !tbaa !9
  %60 = sub i64 %59, %58
  store i64 %60, ptr %6, align 8, !tbaa !9
  %61 = load i64, ptr %9, align 8, !tbaa !9
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds i8, ptr %62, i64 %61
  store ptr %63, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %64 = call noalias ptr @malloc(i64 noundef 48) #6
  store ptr %64, ptr %10, align 8, !tbaa !5
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.BufferItem, ptr %66, i32 0, i32 5
  store ptr %65, ptr %67, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %68 = load i64, ptr %6, align 8, !tbaa !9
  %69 = add i64 %68, 1024
  store i64 %69, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.BufferItem, ptr %70, i32 0, i32 1
  %72 = load i64, ptr %71, align 8, !tbaa !13
  %73 = uitofp i64 %72 to double
  %74 = fmul double %73, 5.000000e-01
  %75 = fptoui double %74 to i64
  store i64 %75, ptr %12, align 8, !tbaa !9
  %76 = load i64, ptr %11, align 8, !tbaa !9
  %77 = load i64, ptr %12, align 8, !tbaa !9
  %78 = icmp ugt i64 %76, %77
  br i1 %78, label %79, label %81

79:                                               ; preds = %43
  %80 = load i64, ptr %11, align 8, !tbaa !9
  br label %83

81:                                               ; preds = %43
  %82 = load i64, ptr %12, align 8, !tbaa !9
  br label %83

83:                                               ; preds = %81, %79
  %84 = phi i64 [ %80, %79 ], [ %82, %81 ]
  %85 = load ptr, ptr %10, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.BufferItem, ptr %85, i32 0, i32 1
  store i64 %84, ptr %86, align 8, !tbaa !13
  %87 = load ptr, ptr %10, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.BufferItem, ptr %87, i32 0, i32 1
  %89 = load i64, ptr %88, align 8, !tbaa !13
  %90 = icmp ult i64 %89, 1024
  br i1 %90, label %91, label %94

91:                                               ; preds = %83
  %92 = load ptr, ptr %10, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.BufferItem, ptr %92, i32 0, i32 1
  store i64 1024, ptr %93, align 8, !tbaa !13
  br label %94

94:                                               ; preds = %91, %83
  %95 = load ptr, ptr %10, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.BufferItem, ptr %95, i32 0, i32 2
  store i64 0, ptr %96, align 8, !tbaa !14
  %97 = load ptr, ptr %10, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.BufferItem, ptr %97, i32 0, i32 1
  %99 = load i64, ptr %98, align 8, !tbaa !13
  %100 = call noalias ptr @malloc(i64 noundef %99) #6
  %101 = load ptr, ptr %10, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.BufferItem, ptr %101, i32 0, i32 0
  store ptr %100, ptr %102, align 8, !tbaa !15
  %103 = load ptr, ptr %10, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.BufferItem, ptr %103, i32 0, i32 4
  store ptr null, ptr %104, align 8, !tbaa !11
  %105 = load ptr, ptr %10, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.BufferItem, ptr %105, i32 0, i32 3
  store ptr null, ptr %106, align 8, !tbaa !17
  %107 = load ptr, ptr %10, align 8, !tbaa !5
  %108 = load ptr, ptr %8, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.BufferItem, ptr %108, i32 0, i32 3
  store ptr %107, ptr %109, align 8, !tbaa !17
  %110 = load ptr, ptr %10, align 8, !tbaa !5
  %111 = load ptr, ptr %7, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.BufferItem, ptr %111, i32 0, i32 4
  store ptr %110, ptr %112, align 8, !tbaa !11
  %113 = load ptr, ptr %4, align 8, !tbaa !5
  %114 = load ptr, ptr %5, align 8, !tbaa !5
  %115 = load i64, ptr %6, align 8, !tbaa !9
  call void @writeFn(ptr noundef %113, ptr noundef %114, i64 noundef %115)
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %116

116:                                              ; preds = %94, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  ret void
}

declare ptr @png_get_io_ptr(ptr noundef) #4

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

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 32}
!12 = !{!"BufferItem", !6, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !6, i64 40}
!17 = !{!12, !6, i64 24}


; 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 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @writeFn(ptr noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #2 {
  %4 = tail call ptr @png_get_io_ptr(ptr noundef %0) #6
  %5 = getelementptr inbounds i8, ptr %4, i64 32
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %6, i64 16
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = sub i64 %8, %10
  %.not49 = icmp ult i64 %11, %2
  br i1 %.not49, label %tailrecurse, label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %3
  %.tr45.lcssa = phi ptr [ %1, %3 ], [ %26, %tailrecurse ]
  %.tr46.lcssa = phi i64 [ %2, %3 ], [ %25, %tailrecurse ]
  %.lcssa48 = phi ptr [ %6, %3 ], [ %41, %tailrecurse ]
  %.lcssa47 = phi ptr [ %9, %3 ], [ %44, %tailrecurse ]
  %.lcssa = phi i64 [ %10, %3 ], [ %45, %tailrecurse ]
  %12 = load ptr, ptr %.lcssa48, align 8, !tbaa !13
  %13 = getelementptr inbounds i8, ptr %12, i64 %.lcssa
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %13, ptr align 1 %.tr45.lcssa, i64 %.tr46.lcssa, i1 false)
  %14 = load i64, ptr %.lcssa47, align 8, !tbaa !12
  %15 = add i64 %14, %.tr46.lcssa
  store i64 %15, ptr %.lcssa47, align 8, !tbaa !12
  ret void

tailrecurse:                                      ; preds = %3, %tailrecurse
  %16 = phi i64 [ %46, %tailrecurse ], [ %11, %3 ]
  %17 = phi i64 [ %45, %tailrecurse ], [ %10, %3 ]
  %18 = phi ptr [ %44, %tailrecurse ], [ %9, %3 ]
  %19 = phi ptr [ %42, %tailrecurse ], [ %7, %3 ]
  %20 = phi ptr [ %41, %tailrecurse ], [ %6, %3 ]
  %21 = phi ptr [ %40, %tailrecurse ], [ %5, %3 ]
  %.tr4651 = phi i64 [ %25, %tailrecurse ], [ %2, %3 ]
  %.tr4550 = phi ptr [ %26, %tailrecurse ], [ %1, %3 ]
  %22 = load ptr, ptr %20, align 8, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %22, i64 %17
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %23, ptr align 1 %.tr4550, i64 %16, i1 false)
  %24 = load i64, ptr %19, align 8, !tbaa !11
  store i64 %24, ptr %18, align 8, !tbaa !12
  %25 = sub i64 %.tr4651, %16
  %26 = getelementptr inbounds i8, ptr %.tr4550, i64 %16
  %27 = tail call noalias dereferenceable_or_null(48) ptr @malloc(i64 noundef 48) #7
  %28 = getelementptr inbounds i8, ptr %27, i64 40
  store ptr %27, ptr %28, align 8, !tbaa !14
  %29 = add i64 %25, 1024
  %30 = uitofp i64 %24 to double
  %31 = fmul double %30, 5.000000e-01
  %32 = fptoui double %31 to i64
  %33 = tail call i64 @llvm.umax.i64(i64 %29, i64 %32)
  %34 = getelementptr inbounds i8, ptr %27, i64 8
  %spec.select = tail call i64 @llvm.umax.i64(i64 %33, i64 1024)
  store i64 %spec.select, ptr %34, align 8, !tbaa !11
  %35 = getelementptr inbounds i8, ptr %27, i64 16
  store i64 0, ptr %35, align 8, !tbaa !12
  %36 = tail call noalias ptr @malloc(i64 noundef %spec.select) #7
  store ptr %36, ptr %27, align 8, !tbaa !13
  %37 = getelementptr inbounds i8, ptr %27, i64 24
  %38 = getelementptr inbounds i8, ptr %20, i64 24
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %37, i8 0, i64 16, i1 false)
  store ptr %27, ptr %38, align 8, !tbaa !15
  store ptr %27, ptr %21, align 8, !tbaa !5
  %39 = tail call ptr @png_get_io_ptr(ptr noundef %0) #6
  %40 = getelementptr inbounds i8, ptr %39, i64 32
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 8
  %43 = load i64, ptr %42, align 8, !tbaa !11
  %44 = getelementptr inbounds i8, ptr %41, i64 16
  %45 = load i64, ptr %44, align 8, !tbaa !12
  %46 = sub i64 %43, %45
  %.not = icmp ult i64 %46, %25
  br i1 %.not, label %tailrecurse, label %tailrecurse._crit_edge
}

declare ptr @png_get_io_ptr(ptr noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #4

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

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 32}
!6 = !{!"BufferItem", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !7, i64 40}
!15 = !{!6, !7, i64 24}


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