; LLVM IR from ashishzero/karma :: src/modules/core (stb)

; 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.stbi__zbuf = type { ptr, ptr, i32, i32, ptr, ptr, ptr, i32, %struct.stbi__zhuffman, %struct.stbi__zhuffman }
%struct.stbi__zhuffman = type { [512 x i16], [16 x i16], [17 x i32], [16 x i16], [288 x i8], [288 x i16] }
%struct.Allocator = type { ptr, ptr }
%struct.Thread_Context = type { %union.Handle, i64, %struct.Allocator, %struct.Temporary_Memory, ptr, ptr }
%union.Handle = type { ptr }
%struct.Temporary_Memory = type { ptr, ptr, i64 }

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11memory_freePKv9Allocator(ptr noundef, ptr, ptr) #0

; Function Attrs: uwtable
declare hidden noundef ptr @_ZTW7context() #1

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

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

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

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL12stbi__mallocm(i64 noundef) #4

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL13stbi__do_zlibP10stbi__zbufPciii(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef) #4

; Function Attrs: mustprogress uwtable
define dso_local ptr @stbi_zlib_decode_malloc_guesssize_headerflag(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3, i32 noundef %4) #4 {
  %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 i32, align 4
  %12 = alloca %struct.stbi__zbuf, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca %struct.Allocator, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store i32 %2, ptr %9, align 4, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4096, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %16 = load i32, ptr %9, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = call noundef ptr @_ZL12stbi__mallocm(i64 noundef %17)
  store ptr %18, ptr %13, align 8, !tbaa !5
  %19 = load ptr, ptr %13, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %5
  store ptr null, ptr %6, align 8
  store i32 1, ptr %14, align 4
  br label %60

22:                                               ; preds = %5
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.stbi__zbuf, ptr %12, i32 0, i32 0
  store ptr %23, ptr %24, align 8, !tbaa !11
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = load i32, ptr %8, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.stbi__zbuf, ptr %12, i32 0, i32 1
  store ptr %28, ptr %29, align 8, !tbaa !14
  %30 = load ptr, ptr %13, align 8, !tbaa !5
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = call noundef i32 @_ZL13stbi__do_zlibP10stbi__zbufPciii(ptr noundef %12, ptr noundef %30, i32 noundef %31, i32 noundef 1, i32 noundef %32)
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %51

35:                                               ; preds = %22
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = icmp ne ptr %36, null
  br i1 %37, label %38, label %48

38:                                               ; preds = %35
  %39 = getelementptr inbounds %struct.stbi__zbuf, ptr %12, i32 0, i32 4
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  %41 = getelementptr inbounds %struct.stbi__zbuf, ptr %12, i32 0, i32 5
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = ptrtoint ptr %40 to i64
  %44 = ptrtoint ptr %42 to i64
  %45 = sub i64 %43, %44
  %46 = trunc i64 %45 to i32
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  store i32 %46, ptr %47, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %38, %35
  %49 = getelementptr inbounds %struct.stbi__zbuf, ptr %12, i32 0, i32 5
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  store ptr %50, ptr %6, align 8
  store i32 1, ptr %14, align 4
  br label %60

51:                                               ; preds = %22
  %52 = getelementptr inbounds %struct.stbi__zbuf, ptr %12, i32 0, i32 5
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = call ptr @_ZTW7context()
  %55 = getelementptr inbounds %struct.Thread_Context, ptr %54, i32 0, i32 2
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %15, ptr align 8 %55, i64 16, i1 false), !tbaa.struct !17
  %56 = getelementptr inbounds { ptr, ptr }, ptr %15, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8
  %58 = getelementptr inbounds { ptr, ptr }, ptr %15, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8
  call void @_Z11memory_freePKv9Allocator(ptr noundef %53, ptr %57, ptr %59)
  store ptr null, ptr %6, align 8
  store i32 1, ptr %14, align 4
  br label %60

60:                                               ; preds = %51, %48, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4096, ptr %12) #5
  %61 = load ptr, ptr %6, align 8
  ret ptr %61
}

attributes #0 = { inlinehint 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 = { uwtable "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { 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, !6, i64 0}
!12 = !{!"_ZTS10stbi__zbuf", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !6, i64 24, !6, i64 32, !6, i64 40, !10, i64 48, !13, i64 52, !13, i64 2072}
!13 = !{!"_ZTS14stbi__zhuffman", !7, i64 0, !7, i64 1024, !7, i64 1056, !7, i64 1124, !7, i64 1156, !7, i64 1444}
!14 = !{!12, !6, i64 8}
!15 = !{!12, !6, i64 24}
!16 = !{!12, !6, i64 32}
!17 = !{i64 0, i64 8, !5, i64 8, i64 8, !5}


; 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.stbi__zbuf = type { ptr, ptr, i32, i32, ptr, ptr, ptr, i32, %struct.stbi__zhuffman, %struct.stbi__zhuffman }
%struct.stbi__zhuffman = type { [512 x i16], [16 x i16], [17 x i32], [16 x i16], [288 x i8], [288 x i16] }

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z11memory_freePKv9Allocator(ptr noundef, ptr, ptr) local_unnamed_addr #0

; Function Attrs: uwtable
declare hidden noundef ptr @_ZTW7context() local_unnamed_addr #1

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

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

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL12stbi__mallocm(i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL13stbi__do_zlibP10stbi__zbufPciii(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
define dso_local ptr @stbi_zlib_decode_malloc_guesssize_headerflag(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef writeonly %3, i32 noundef %4) local_unnamed_addr #3 {
  %6 = alloca %struct.stbi__zbuf, align 8
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %6) #4
  %7 = sext i32 %2 to i64
  %8 = tail call noundef ptr @_ZL12stbi__mallocm(i64 noundef %7)
  %9 = icmp eq ptr %8, null
  br i1 %9, label %30, label %10

10:                                               ; preds = %5
  store ptr %0, ptr %6, align 8, !tbaa !5
  %11 = sext i32 %1 to i64
  %12 = getelementptr inbounds i8, ptr %0, i64 %11
  %13 = getelementptr inbounds i8, ptr %6, i64 8
  store ptr %12, ptr %13, align 8, !tbaa !12
  %14 = call noundef i32 @_ZL13stbi__do_zlibP10stbi__zbufPciii(ptr noundef nonnull %6, ptr noundef nonnull %8, i32 noundef %2, i32 noundef 1, i32 noundef %4)
  %.not = icmp eq i32 %14, 0
  br i1 %.not, label %25, label %15

15:                                               ; preds = %10
  %.not12 = icmp eq ptr %3, null
  br i1 %.not12, label %._crit_edge, label %16

._crit_edge:                                      ; preds = %15
  %.phi.trans.insert = getelementptr inbounds i8, ptr %6, i64 32
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !13
  br label %30

16:                                               ; preds = %15
  %17 = getelementptr inbounds i8, ptr %6, i64 24
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %6, i64 32
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = ptrtoint ptr %18 to i64
  %22 = ptrtoint ptr %20 to i64
  %23 = sub i64 %21, %22
  %24 = trunc i64 %23 to i32
  store i32 %24, ptr %3, align 4, !tbaa !15
  br label %30

25:                                               ; preds = %10
  %26 = getelementptr inbounds i8, ptr %6, i64 32
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = call ptr @_ZTW7context()
  %29 = getelementptr inbounds i8, ptr %28, i64 16
  %.sroa.0.0.copyload = load ptr, ptr %29, align 8, !tbaa !16
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %28, i64 24
  %.sroa.2.0.copyload = load ptr, ptr %.sroa.2.0..sroa_idx, align 8, !tbaa !16
  call void @_Z11memory_freePKv9Allocator(ptr noundef %27, ptr %.sroa.0.0.copyload, ptr %.sroa.2.0.copyload)
  br label %30

30:                                               ; preds = %16, %._crit_edge, %5, %25
  %.0 = phi ptr [ null, %25 ], [ null, %5 ], [ %.pre, %._crit_edge ], [ %20, %16 ]
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %6) #4
  ret ptr %.0
}

attributes #0 = { inlinehint 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 = { uwtable "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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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, !7, i64 0}
!6 = !{!"_ZTS10stbi__zbuf", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !7, i64 24, !7, i64 32, !7, i64 40, !10, i64 48, !11, i64 52, !11, i64 2072}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_ZTS14stbi__zhuffman", !8, i64 0, !8, i64 1024, !8, i64 1056, !8, i64 1124, !8, i64 1156, !8, i64 1444}
!12 = !{!6, !7, i64 8}
!13 = !{!6, !7, i64 32}
!14 = !{!6, !7, i64 24}
!15 = !{!10, !10, i64 0}
!16 = !{!7, !7, i64 0}


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