; LLVM IR from mkazhdan/DMG :: Util/TIFF (tif_compress)

; 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.TIFFCodec = type { ptr, i16, ptr }
%struct._codec = type { ptr, ptr }

@registeredCODECS = external hidden global ptr, align 8
@_TIFFBuiltinCODECS = external global [0 x %struct.TIFFCodec], align 8

; 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

declare void @_TIFFfree(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @TIFFGetConfiguredCODECs() #2 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 1, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store ptr null, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = load ptr, ptr @registeredCODECS, align 8, !tbaa !9
  store ptr %8, ptr %3, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %33, %0
  %10 = load ptr, ptr %3, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %37

12:                                               ; preds = %9
  %13 = load ptr, ptr %5, align 8, !tbaa !9
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = mul i64 %15, 24
  %17 = trunc i64 %16 to i32
  %18 = call ptr @_TIFFrealloc(ptr noundef %13, i32 noundef %17)
  store ptr %18, ptr %6, align 8, !tbaa !9
  %19 = load ptr, ptr %6, align 8, !tbaa !9
  %20 = icmp ne ptr %19, null
  br i1 %20, label %23, label %21

21:                                               ; preds = %12
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  call void @_TIFFfree(ptr noundef %22)
  store ptr null, ptr %1, align 8
  store i32 1, ptr %7, align 4
  br label %93

23:                                               ; preds = %12
  %24 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %24, ptr %5, align 8, !tbaa !9
  %25 = load ptr, ptr %5, align 8, !tbaa !9
  %26 = load i32, ptr %2, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds %struct.TIFFCodec, ptr %25, i64 %27
  %29 = getelementptr inbounds %struct.TIFFCodec, ptr %28, i64 -1
  %30 = load ptr, ptr %3, align 8, !tbaa !9
  call void @_TIFFmemcpy(ptr noundef %29, ptr noundef %30, i32 noundef 24)
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %2, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %23
  %34 = load ptr, ptr %3, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct._codec, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  store ptr %36, ptr %3, align 8, !tbaa !9
  br label %9, !llvm.loop !13

37:                                               ; preds = %9
  store ptr @_TIFFBuiltinCODECS, ptr %4, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %71, %37
  %39 = load ptr, ptr %4, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.TIFFCodec, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !15
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %74

43:                                               ; preds = %38
  %44 = load ptr, ptr %4, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.TIFFCodec, ptr %44, i32 0, i32 1
  %46 = load i16, ptr %45, align 8, !tbaa !18
  %47 = call i32 @TIFFIsCODECConfigured(i16 noundef zeroext %46)
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %70

49:                                               ; preds = %43
  %50 = load ptr, ptr %5, align 8, !tbaa !9
  %51 = load i32, ptr %2, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = mul i64 %52, 24
  %54 = trunc i64 %53 to i32
  %55 = call ptr @_TIFFrealloc(ptr noundef %50, i32 noundef %54)
  store ptr %55, ptr %6, align 8, !tbaa !9
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = icmp ne ptr %56, null
  br i1 %57, label %60, label %58

58:                                               ; preds = %49
  %59 = load ptr, ptr %5, align 8, !tbaa !9
  call void @_TIFFfree(ptr noundef %59)
  store ptr null, ptr %1, align 8
  store i32 1, ptr %7, align 4
  br label %93

60:                                               ; preds = %49
  %61 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %61, ptr %5, align 8, !tbaa !9
  %62 = load ptr, ptr %5, align 8, !tbaa !9
  %63 = load i32, ptr %2, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds %struct.TIFFCodec, ptr %62, i64 %64
  %66 = getelementptr inbounds %struct.TIFFCodec, ptr %65, i64 -1
  %67 = load ptr, ptr %4, align 8, !tbaa !9
  call void @_TIFFmemcpy(ptr noundef %66, ptr noundef %67, i32 noundef 24)
  %68 = load i32, ptr %2, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %2, align 4, !tbaa !5
  br label %70

70:                                               ; preds = %60, %43
  br label %71

71:                                               ; preds = %70
  %72 = load ptr, ptr %4, align 8, !tbaa !9
  %73 = getelementptr inbounds %struct.TIFFCodec, ptr %72, i32 1
  store ptr %73, ptr %4, align 8, !tbaa !9
  br label %38, !llvm.loop !19

74:                                               ; preds = %38
  %75 = load ptr, ptr %5, align 8, !tbaa !9
  %76 = load i32, ptr %2, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = mul i64 %77, 24
  %79 = trunc i64 %78 to i32
  %80 = call ptr @_TIFFrealloc(ptr noundef %75, i32 noundef %79)
  store ptr %80, ptr %6, align 8, !tbaa !9
  %81 = load ptr, ptr %6, align 8, !tbaa !9
  %82 = icmp ne ptr %81, null
  br i1 %82, label %85, label %83

83:                                               ; preds = %74
  %84 = load ptr, ptr %5, align 8, !tbaa !9
  call void @_TIFFfree(ptr noundef %84)
  store ptr null, ptr %1, align 8
  store i32 1, ptr %7, align 4
  br label %93

85:                                               ; preds = %74
  %86 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %86, ptr %5, align 8, !tbaa !9
  %87 = load ptr, ptr %5, align 8, !tbaa !9
  %88 = load i32, ptr %2, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds %struct.TIFFCodec, ptr %87, i64 %89
  %91 = getelementptr inbounds %struct.TIFFCodec, ptr %90, i64 -1
  call void @_TIFFmemset(ptr noundef %91, i32 noundef 0, i32 noundef 24)
  %92 = load ptr, ptr %5, align 8, !tbaa !9
  store ptr %92, ptr %1, align 8
  store i32 1, ptr %7, align 4
  br label %93

93:                                               ; preds = %85, %83, %58, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %94 = load ptr, ptr %1, align 8
  ret ptr %94
}

declare ptr @_TIFFrealloc(ptr noundef, i32 noundef) #1

declare void @_TIFFmemcpy(ptr noundef, ptr noundef, i32 noundef) #1

declare i32 @TIFFIsCODECConfigured(i16 noundef zeroext) #1

declare void @_TIFFmemset(ptr noundef, i32 noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #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 #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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_codec", !10, i64 0, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !10, i64 0}
!16 = !{!"", !10, i64 0, !17, i64 8, !10, i64 16}
!17 = !{!"short", !7, i64 0}
!18 = !{!16, !17, i64 8}
!19 = distinct !{!19, !14}


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

@registeredCODECS = external hidden local_unnamed_addr global ptr, align 8
@_TIFFBuiltinCODECS = external global [0 x %struct.TIFFCodec], align 8

declare void @_TIFFfree(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @TIFFGetConfiguredCODECs() local_unnamed_addr #1 {
  %.03249 = load ptr, ptr @registeredCODECS, align 8, !tbaa !5
  %.not50 = icmp eq ptr %.03249, null
  br i1 %.not50, label %.preheader, label %.lr.ph

.preheader.loopexit:                              ; preds = %7
  %1 = trunc nuw i64 %indvars.iv.next to i32
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %0
  %.033.lcssa = phi i32 [ 1, %0 ], [ %1, %.preheader.loopexit ]
  %.0.lcssa = phi ptr [ null, %0 ], [ %5, %.preheader.loopexit ]
  %2 = load ptr, ptr @_TIFFBuiltinCODECS, align 8, !tbaa !9
  %.not4055 = icmp eq ptr %2, null
  br i1 %.not4055, label %._crit_edge, label %.lr.ph59

.lr.ph:                                           ; preds = %0, %7
  %indvars.iv = phi i64 [ %indvars.iv.next, %7 ], [ 1, %0 ]
  %.03253 = phi ptr [ %.032, %7 ], [ %.03249, %0 ]
  %.052 = phi ptr [ %5, %7 ], [ null, %0 ]
  %3 = trunc nuw nsw i64 %indvars.iv to i32
  %4 = mul i32 %3, 24
  %5 = tail call ptr @_TIFFrealloc(ptr noundef %.052, i32 noundef %4) #2
  %.not44 = icmp eq ptr %5, null
  br i1 %.not44, label %6, label %7

6:                                                ; preds = %.lr.ph
  tail call void @_TIFFfree(ptr noundef %.052) #2
  br label %32

7:                                                ; preds = %.lr.ph
  %8 = getelementptr inbounds %struct.TIFFCodec, ptr %5, i64 %indvars.iv
  %9 = getelementptr inbounds i8, ptr %8, i64 -24
  tail call void @_TIFFmemcpy(ptr noundef nonnull %9, ptr noundef nonnull %.03253, i32 noundef 24) #2
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.032 = load ptr, ptr %.03253, align 8, !tbaa !5
  %.not = icmp eq ptr %.032, null
  br i1 %.not, label %.preheader.loopexit, label %.lr.ph, !llvm.loop !12

.lr.ph59:                                         ; preds = %.preheader, %22
  %.158 = phi ptr [ %.2, %22 ], [ %.0.lcssa, %.preheader ]
  %.03157 = phi ptr [ %23, %22 ], [ @_TIFFBuiltinCODECS, %.preheader ]
  %.13456 = phi i32 [ %.235, %22 ], [ %.033.lcssa, %.preheader ]
  %10 = getelementptr inbounds i8, ptr %.03157, i64 8
  %11 = load i16, ptr %10, align 8, !tbaa !14
  %12 = tail call i32 @TIFFIsCODECConfigured(i16 noundef zeroext %11) #2
  %.not42 = icmp eq i32 %12, 0
  br i1 %.not42, label %22, label %13

13:                                               ; preds = %.lr.ph59
  %14 = mul i32 %.13456, 24
  %15 = tail call ptr @_TIFFrealloc(ptr noundef %.158, i32 noundef %14) #2
  %.not43 = icmp eq ptr %15, null
  br i1 %.not43, label %16, label %17

16:                                               ; preds = %13
  tail call void @_TIFFfree(ptr noundef %.158) #2
  br label %32

17:                                               ; preds = %13
  %18 = sext i32 %.13456 to i64
  %19 = getelementptr inbounds %struct.TIFFCodec, ptr %15, i64 %18
  %20 = getelementptr inbounds i8, ptr %19, i64 -24
  tail call void @_TIFFmemcpy(ptr noundef nonnull %20, ptr noundef nonnull %.03157, i32 noundef 24) #2
  %21 = add nsw i32 %.13456, 1
  br label %22

22:                                               ; preds = %.lr.ph59, %17
  %.235 = phi i32 [ %21, %17 ], [ %.13456, %.lr.ph59 ]
  %.2 = phi ptr [ %15, %17 ], [ %.158, %.lr.ph59 ]
  %23 = getelementptr inbounds i8, ptr %.03157, i64 24
  %24 = load ptr, ptr %23, align 8, !tbaa !9
  %.not40 = icmp eq ptr %24, null
  br i1 %.not40, label %._crit_edge, label %.lr.ph59, !llvm.loop !15

._crit_edge:                                      ; preds = %22, %.preheader
  %.134.lcssa = phi i32 [ %.033.lcssa, %.preheader ], [ %.235, %22 ]
  %.1.lcssa = phi ptr [ %.0.lcssa, %.preheader ], [ %.2, %22 ]
  %25 = mul i32 %.134.lcssa, 24
  %26 = tail call ptr @_TIFFrealloc(ptr noundef %.1.lcssa, i32 noundef %25) #2
  %.not41 = icmp eq ptr %26, null
  br i1 %.not41, label %27, label %28

27:                                               ; preds = %._crit_edge
  tail call void @_TIFFfree(ptr noundef %.1.lcssa) #2
  br label %32

28:                                               ; preds = %._crit_edge
  %29 = sext i32 %.134.lcssa to i64
  %30 = getelementptr inbounds %struct.TIFFCodec, ptr %26, i64 %29
  %31 = getelementptr inbounds i8, ptr %30, i64 -24
  tail call void @_TIFFmemset(ptr noundef nonnull %31, i32 noundef 0, i32 noundef 24) #2
  br label %32

32:                                               ; preds = %28, %27, %16, %6
  %.036 = phi ptr [ null, %6 ], [ null, %16 ], [ %26, %28 ], [ null, %27 ]
  ret ptr %.036
}

declare ptr @_TIFFrealloc(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @_TIFFmemcpy(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @TIFFIsCODECConfigured(i16 noundef zeroext) local_unnamed_addr #0

declare void @_TIFFmemset(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

attributes #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 = { 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 = { 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, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !6, i64 16}
!11 = !{!"short", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !11, i64 8}
!15 = distinct !{!15, !13}


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