; LLVM IR from fhanau/Efficient-Compression-Tool :: src/lodepng/lodepng.ll (Z15lodepng_inflatePPhPmPKhm)

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z15lodepng_inflatePPhPmPKhm(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca %struct.z_stream_s, align 8
  %11 = alloca [32768 x i8], align 16
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 112, ptr %10) #5
  %16 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 8
  store ptr null, ptr %16, align 8, !tbaa !11
  %17 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 9
  store ptr null, ptr %17, align 8, !tbaa !14
  %18 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 10
  store ptr null, ptr %18, align 8, !tbaa !15
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 0
  store ptr %19, ptr %20, align 8, !tbaa !16
  %21 = load i64, ptr %9, align 8, !tbaa !9
  %22 = trunc i64 %21 to i32
  %23 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 1
  store i32 %22, ptr %23, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 32768, ptr %11) #5
  %24 = getelementptr inbounds [32768 x i8], ptr %11, i64 0, i64 0
  %25 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 3
  store ptr %24, ptr %25, align 8, !tbaa !18
  %26 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 4
  store i32 32768, ptr %26, align 8, !tbaa !19
  %27 = call i32 @inflateInit2_(ptr noundef %10, i32 noundef -15, ptr noundef @.str.1, i32 noundef 112)
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %30

29:                                               ; preds = %4
  store i32 83, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %101

30:                                               ; preds = %4
  br label %31

31:                                               ; preds = %95, %30
  br label %32

32:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %33 = call i32 @inflate(ptr noundef %10, i32 noundef 2)
  store i32 %33, ptr %13, align 4, !tbaa !20
  %34 = load i32, ptr %13, align 4, !tbaa !20
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %56

36:                                               ; preds = %32
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = load i64, ptr %39, align 8, !tbaa !9
  %41 = add i64 %40, 32768
  %42 = call ptr @realloc(ptr noundef %38, i64 noundef %41) #6
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %42, ptr %43, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i64, ptr %46, align 8, !tbaa !9
  %48 = getelementptr inbounds i8, ptr %45, i64 %47
  %49 = getelementptr inbounds [32768 x i8], ptr %11, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %48, ptr align 16 %49, i64 32768, i1 false)
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = load i64, ptr %50, align 8, !tbaa !9
  %52 = add i64 %51, 32768
  store i64 %52, ptr %50, align 8, !tbaa !9
  %53 = getelementptr inbounds [32768 x i8], ptr %11, i64 0, i64 0
  %54 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 3
  store ptr %53, ptr %54, align 8, !tbaa !18
  %55 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 4
  store i32 32768, ptr %55, align 8, !tbaa !19
  br label %92

56:                                               ; preds = %32
  %57 = load i32, ptr %13, align 4, !tbaa !20
  %58 = icmp eq i32 %57, 1
  br i1 %58, label %59, label %85

59:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %60 = getelementptr inbounds %struct.z_stream_s, ptr %10, i32 0, i32 4
  %61 = load i32, ptr %60, align 8, !tbaa !19
  %62 = sub i32 32768, %61
  store i32 %62, ptr %14, align 4, !tbaa !20
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load ptr, ptr %63, align 8, !tbaa !5
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = load i64, ptr %65, align 8, !tbaa !9
  %67 = load i32, ptr %14, align 4, !tbaa !20
  %68 = zext i32 %67 to i64
  %69 = add i64 %66, %68
  %70 = call ptr @realloc(ptr noundef %64, i64 noundef %69) #6
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %70, ptr %71, align 8, !tbaa !5
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = load ptr, ptr %72, align 8, !tbaa !5
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = load i64, ptr %74, align 8, !tbaa !9
  %76 = getelementptr inbounds i8, ptr %73, i64 %75
  %77 = getelementptr inbounds [32768 x i8], ptr %11, i64 0, i64 0
  %78 = load i32, ptr %14, align 4, !tbaa !20
  %79 = zext i32 %78 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %76, ptr align 16 %77, i64 %79, i1 false)
  %80 = load i32, ptr %14, align 4, !tbaa !20
  %81 = zext i32 %80 to i64
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = load i64, ptr %82, align 8, !tbaa !9
  %84 = add i64 %83, %81
  store i64 %84, ptr %82, align 8, !tbaa !9
  store i32 3, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %93

85:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  store i32 95, ptr %15, align 4, !tbaa !20
  %86 = load i32, ptr %13, align 4, !tbaa !20
  %87 = icmp eq i32 %86, -4
  br i1 %87, label %88, label %89

88:                                               ; preds = %85
  store i32 83, ptr %15, align 4, !tbaa !20
  br label %89

89:                                               ; preds = %88, %85
  %90 = call i32 @inflateEnd(ptr noundef %10)
  %91 = load i32, ptr %15, align 4, !tbaa !20
  store i32 %91, ptr %5, align 4
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %93

92:                                               ; preds = %36
  store i32 0, ptr %12, align 4
  br label %93

93:                                               ; preds = %92, %89, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  %94 = load i32, ptr %12, align 4
  switch i32 %94, label %101 [
    i32 0, label %95
    i32 3, label %96
  ]

95:                                               ; preds = %93
  br label %31, !llvm.loop !21

96:                                               ; preds = %93
  %97 = call i32 @inflateEnd(ptr noundef %10)
  %98 = icmp ne i32 %97, 0
  br i1 %98, label %99, label %100

99:                                               ; preds = %96
  store i32 83, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %101

100:                                              ; preds = %96
  store i32 0, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %101

101:                                              ; preds = %100, %99, %93, %29
  call void @llvm.lifetime.end.p0(i64 32768, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 112, ptr %10) #5
  %102 = load i32, ptr %5, align 4
  ret i32 %102
}

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

declare i32 @inflateInit2_(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #2

declare i32 @inflate(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

; 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) #4

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

declare i32 @inflateEnd(ptr noundef) #2

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

!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, !6, i64 64}
!12 = !{!"_ZTS10z_stream_s", !6, i64 0, !13, i64 8, !10, i64 16, !6, i64 24, !13, i64 32, !10, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !13, i64 88, !10, i64 96, !10, i64 104}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !6, i64 72}
!15 = !{!12, !6, i64 80}
!16 = !{!12, !6, i64 0}
!17 = !{!12, !13, i64 8}
!18 = !{!12, !6, i64 24}
!19 = !{!12, !13, i64 32}
!20 = !{!13, !13, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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.z_stream_s = type { ptr, i32, i64, ptr, i32, i64, ptr, ptr, ptr, ptr, ptr, i32, i64, i64 }

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z15lodepng_inflatePPhPmPKhm(ptr nocapture noundef %0, ptr nocapture noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.z_stream_s, align 8
  %6 = alloca [32768 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 112, ptr nonnull %5) #6
  %7 = getelementptr inbounds i8, ptr %5, i64 64
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %7, i8 0, i64 24, i1 false)
  store ptr %2, ptr %5, align 8, !tbaa !5
  %8 = trunc i64 %3 to i32
  %9 = getelementptr inbounds i8, ptr %5, i64 8
  store i32 %8, ptr %9, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 32768, ptr nonnull %6) #6
  %10 = getelementptr inbounds i8, ptr %5, i64 24
  store ptr %6, ptr %10, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %5, i64 32
  store i32 32768, ptr %11, align 8, !tbaa !14
  %12 = call i32 @inflateInit2_(ptr noundef nonnull %5, i32 noundef -15, ptr noundef nonnull @.str.1, i32 noundef 112)
  %.not = icmp eq i32 %12, 0
  br i1 %.not, label %.preheader, label %36

.preheader:                                       ; preds = %4, %16
  %13 = call i32 @inflate(ptr noundef nonnull %5, i32 noundef 2)
  switch i32 %13, label %.thread [
    i32 0, label %16
    i32 1, label %24
  ]

.thread:                                          ; preds = %.preheader
  %14 = icmp eq i32 %13, -4
  %spec.store.select = select i1 %14, i32 83, i32 95
  %15 = call i32 @inflateEnd(ptr noundef nonnull %5)
  br label %36

16:                                               ; preds = %.preheader
  %17 = load ptr, ptr %0, align 8, !tbaa !15
  %18 = load i64, ptr %1, align 8, !tbaa !16
  %19 = add i64 %18, 32768
  %20 = call ptr @realloc(ptr noundef %17, i64 noundef %19) #7
  store ptr %20, ptr %0, align 8, !tbaa !15
  %21 = load i64, ptr %1, align 8, !tbaa !16
  %22 = getelementptr inbounds i8, ptr %20, i64 %21
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(32768) %22, ptr noundef nonnull align 16 dereferenceable(32768) %6, i64 32768, i1 false)
  %23 = add i64 %21, 32768
  store i64 %23, ptr %1, align 8, !tbaa !16
  store ptr %6, ptr %10, align 8, !tbaa !13
  store i32 32768, ptr %11, align 8, !tbaa !14
  br label %.preheader

24:                                               ; preds = %.preheader
  %25 = load i32, ptr %11, align 8, !tbaa !14
  %26 = sub i32 32768, %25
  %27 = load ptr, ptr %0, align 8, !tbaa !15
  %28 = load i64, ptr %1, align 8, !tbaa !16
  %29 = zext i32 %26 to i64
  %30 = add i64 %28, %29
  %31 = call ptr @realloc(ptr noundef %27, i64 noundef %30) #7
  store ptr %31, ptr %0, align 8, !tbaa !15
  %32 = load i64, ptr %1, align 8, !tbaa !16
  %33 = getelementptr inbounds i8, ptr %31, i64 %32
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr nonnull align 16 %6, i64 %29, i1 false)
  %34 = add i64 %32, %29
  store i64 %34, ptr %1, align 8, !tbaa !16
  %35 = call i32 @inflateEnd(ptr noundef nonnull %5)
  %.not24 = icmp eq i32 %35, 0
  %. = select i1 %.not24, i32 0, i32 83
  br label %36

36:                                               ; preds = %.thread, %24, %4
  %.2 = phi i32 [ 83, %4 ], [ %., %24 ], [ %spec.store.select, %.thread ]
  call void @llvm.lifetime.end.p0(i64 32768, ptr nonnull %6) #6
  call void @llvm.lifetime.end.p0(i64 112, ptr nonnull %5) #6
  ret i32 %.2
}

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

declare i32 @inflateInit2_(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @inflate(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #3

; 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) #4

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

declare i32 @inflateEnd(ptr noundef) local_unnamed_addr #2

; 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 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { nounwind }
attributes #7 = { nounwind allocsize(1) }

!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 0}
!6 = !{!"_ZTS10z_stream_s", !7, i64 0, !10, i64 8, !11, i64 16, !7, i64 24, !10, i64 32, !11, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !11, i64 96, !11, i64 104}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !10, i64 8}
!13 = !{!6, !7, i64 24}
!14 = !{!6, !10, i64 32}
!15 = !{!7, !7, i64 0}
!16 = !{!11, !11, i64 0}


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