; LLVM IR from facebookresearch/habitat-sim :: src/deps/basis-universal/encoder/lodepng.ll (Z20lodepng_chunk_appendPPhPmPKh)

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

; 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 dso_local noundef i32 @_Z20lodepng_chunk_lengthPKh(ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z20lodepng_chunk_appendPPhPmPKh(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = call noundef i32 @_Z20lodepng_chunk_lengthPKh(ptr noundef %14)
  %16 = add i32 %15, 12
  store i32 %16, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i64, ptr %17, align 8, !tbaa !11
  %19 = load i32, ptr %9, align 4, !tbaa !9
  %20 = zext i32 %19 to i64
  %21 = add i64 %18, %20
  store i64 %21, ptr %12, align 8, !tbaa !11
  %22 = load i64, ptr %12, align 8, !tbaa !11
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = icmp ult i64 %22, %24
  br i1 %25, label %31, label %26

26:                                               ; preds = %3
  %27 = load i64, ptr %12, align 8, !tbaa !11
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = load i64, ptr %28, align 8, !tbaa !11
  %30 = icmp ult i64 %27, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %26, %3
  store i32 77, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %70

32:                                               ; preds = %26
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  %35 = load i64, ptr %12, align 8, !tbaa !11
  %36 = call noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef %34, i64 noundef %35)
  store ptr %36, ptr %11, align 8, !tbaa !5
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = icmp ne ptr %37, null
  br i1 %38, label %40, label %39

39:                                               ; preds = %32
  store i32 83, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %70

40:                                               ; preds = %32
  %41 = load ptr, ptr %11, align 8, !tbaa !5
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %41, ptr %42, align 8, !tbaa !5
  %43 = load i64, ptr %12, align 8, !tbaa !11
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  store i64 %43, ptr %44, align 8, !tbaa !11
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  %47 = load i64, ptr %12, align 8, !tbaa !11
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = zext i32 %48 to i64
  %50 = sub i64 %47, %49
  %51 = getelementptr inbounds i8, ptr %46, i64 %50
  store ptr %51, ptr %10, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %66, %40
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = load i32, ptr %9, align 4, !tbaa !9
  %55 = icmp ne i32 %53, %54
  br i1 %55, label %56, label %69

56:                                               ; preds = %52
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !9
  %59 = zext i32 %58 to i64
  %60 = getelementptr inbounds i8, ptr %57, i64 %59
  %61 = load i8, ptr %60, align 1, !tbaa !13
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = load i32, ptr %8, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = getelementptr inbounds i8, ptr %62, i64 %64
  store i8 %61, ptr %65, align 1, !tbaa !13
  br label %66

66:                                               ; preds = %56
  %67 = load i32, ptr %8, align 4, !tbaa !9
  %68 = add i32 %67, 1
  store i32 %68, ptr %8, align 4, !tbaa !9
  br label %52, !llvm.loop !14

69:                                               ; preds = %52
  store i32 0, ptr %4, align 4
  store i32 1, ptr %13, align 4
  br label %70

70:                                               ; preds = %69, %39, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %71 = load i32, ptr %4, align 4
  ret i32 %71
}

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

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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 i32 @_Z20lodepng_chunk_lengthPKh(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef range(i32 0, 84) i32 @_Z20lodepng_chunk_appendPPhPmPKh(ptr nocapture noundef %0, ptr nocapture noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = ptrtoint ptr %2 to i64
  %5 = tail call noundef i32 @_Z20lodepng_chunk_lengthPKh(ptr noundef %2)
  %6 = add i32 %5, 12
  %7 = load i64, ptr %1, align 8, !tbaa !5
  %8 = zext i32 %6 to i64
  %9 = add i64 %7, %8
  %10 = icmp ult i64 %9, %8
  br i1 %10, label %.loopexit, label %11

11:                                               ; preds = %3
  %12 = load ptr, ptr %0, align 8, !tbaa !9
  %13 = tail call noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef %12, i64 noundef %9)
  %14 = ptrtoint ptr %13 to i64
  %.not = icmp eq ptr %13, null
  br i1 %.not, label %.loopexit, label %15

15:                                               ; preds = %11
  store ptr %13, ptr %0, align 8, !tbaa !9
  store i64 %9, ptr %1, align 8, !tbaa !5
  %16 = getelementptr i8, ptr %13, i64 %7
  %.not2728 = icmp eq i32 %6, 0
  br i1 %.not2728, label %.loopexit, label %iter.check

iter.check:                                       ; preds = %15
  %min.iters.check = icmp ult i32 %6, 8
  br i1 %min.iters.check, label %.lr.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %iter.check
  %17 = add i64 %7, %14
  %18 = sub i64 %17, %4
  %diff.check = icmp ult i64 %18, 32
  br i1 %diff.check, label %.lr.ph.preheader, label %vector.main.loop.iter.check

vector.main.loop.iter.check:                      ; preds = %vector.memcheck
  %min.iters.check31 = icmp ult i32 %6, 32
  br i1 %min.iters.check31, label %vec.epilog.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.main.loop.iter.check
  %n.vec = and i64 %8, 4294967264
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %19 = getelementptr inbounds i8, ptr %2, i64 %index
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  %wide.load = load <16 x i8>, ptr %19, align 1, !tbaa !11
  %wide.load32 = load <16 x i8>, ptr %20, align 1, !tbaa !11
  %21 = getelementptr inbounds i8, ptr %16, i64 %index
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  store <16 x i8> %wide.load, ptr %21, align 1, !tbaa !11
  store <16 x i8> %wide.load32, ptr %22, align 1, !tbaa !11
  %index.next = add nuw i64 %index, 32
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %8
  br i1 %cmp.n, label %.loopexit, label %vec.epilog.iter.check

vec.epilog.iter.check:                            ; preds = %middle.block
  %n.vec.remaining = and i64 %8, 24
  %min.epilog.iters.check = icmp eq i64 %n.vec.remaining, 0
  br i1 %min.epilog.iters.check, label %.lr.ph.preheader, label %vec.epilog.ph

vec.epilog.ph:                                    ; preds = %vector.main.loop.iter.check, %vec.epilog.iter.check
  %vec.epilog.resume.val = phi i64 [ %n.vec, %vec.epilog.iter.check ], [ 0, %vector.main.loop.iter.check ]
  %n.vec34 = and i64 %8, 4294967288
  br label %vec.epilog.vector.body

vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index35 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next37, %vec.epilog.vector.body ]
  %24 = getelementptr inbounds i8, ptr %2, i64 %index35
  %wide.load36 = load <8 x i8>, ptr %24, align 1, !tbaa !11
  %25 = getelementptr inbounds i8, ptr %16, i64 %index35
  store <8 x i8> %wide.load36, ptr %25, align 1, !tbaa !11
  %index.next37 = add nuw i64 %index35, 8
  %26 = icmp eq i64 %index.next37, %n.vec34
  br i1 %26, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !16

vec.epilog.middle.block:                          ; preds = %vec.epilog.vector.body
  %cmp.n38 = icmp eq i64 %n.vec34, %8
  br i1 %cmp.n38, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %vec.epilog.middle.block, %vector.memcheck, %iter.check, %vec.epilog.iter.check
  %indvars.iv.ph = phi i64 [ 0, %iter.check ], [ 0, %vector.memcheck ], [ %n.vec, %vec.epilog.iter.check ], [ %n.vec34, %vec.epilog.middle.block ]
  %xtraiter = and i64 %8, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.lr.ph.prol ], [ %indvars.iv.ph, %.lr.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %27 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv.prol
  %28 = load i8, ptr %27, align 1, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %16, i64 %indvars.iv.prol
  store i8 %28, ptr %29, align 1, !tbaa !11
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !17

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %.lr.ph.preheader ], [ %indvars.iv.next.prol, %.lr.ph.prol ]
  %30 = sub nsw i64 %indvars.iv.ph, %8
  %31 = icmp ugt i64 %30, -4
  br i1 %31, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.lr.ph ], [ %indvars.iv.unr, %.lr.ph.prol.loopexit ]
  %32 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %16, i64 %indvars.iv
  store i8 %33, ptr %34, align 1, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %35 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv.next
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = getelementptr inbounds i8, ptr %16, i64 %indvars.iv.next
  store i8 %36, ptr %37, align 1, !tbaa !11
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %38 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv.next.1
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = getelementptr inbounds i8, ptr %16, i64 %indvars.iv.next.1
  store i8 %39, ptr %40, align 1, !tbaa !11
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %41 = getelementptr inbounds i8, ptr %2, i64 %indvars.iv.next.2
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = getelementptr inbounds i8, ptr %16, i64 %indvars.iv.next.2
  store i8 %42, ptr %43, align 1, !tbaa !11
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %.not27.3 = icmp eq i64 %indvars.iv.next.3, %8
  br i1 %.not27.3, label %.loopexit, label %.lr.ph, !llvm.loop !19

.loopexit:                                        ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %middle.block, %vec.epilog.middle.block, %15, %11, %3
  %.0 = phi i32 [ 77, %3 ], [ 83, %11 ], [ 0, %15 ], [ 0, %vec.epilog.middle.block ], [ 0, %middle.block ], [ 0, %.lr.ph ], [ 0, %.lr.ph.prol.loopexit ]
  ret i32 %.0
}

; Function Attrs: mustprogress nounwind uwtable
declare hidden noundef ptr @_ZL15lodepng_reallocPvm(ptr noundef, i64 noundef) local_unnamed_addr #0

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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13, !14, !15}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !13, !14, !15}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.unroll.disable"}
!19 = distinct !{!19, !13, !14}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass