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

; 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.tiff = type { ptr, i32, i32, i32, i32, i32, ptr, i16, %struct.TIFFDirectory, %struct.TIFFHeader, ptr, ptr, i32, i16, i32, i32, i32, i16, i32, i32, i32, i32, i32, ptr, ptr, ptr, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, ptr, i32, ptr, i32, ptr, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i64, ptr, %struct.TIFFTagMethods, ptr }
%struct.TIFFDirectory = type { [4 x i64], i32, i32, i32, i32, i32, i32, i32, i16, i16, i16, i16, i16, i16, i16, i16, i32, i16, i16, double, double, float, float, i16, i16, float, float, [2 x i16], [3 x ptr], [2 x i16], i16, ptr, i32, i32, ptr, ptr, i32, i16, ptr, [2 x i16], i16, [3 x ptr], i32, ptr, i32, ptr }
%struct.TIFFHeader = type { i16, i16, i32 }
%struct.TIFFTagMethods = type { ptr, ptr, ptr }

@.str.2 = external hidden unnamed_addr constant [56 x i8], align 1
@TIFFWriteRawStrip.module = external hidden constant [18 x i8], align 16

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @TIFFWriteCheck(ptr noundef, i32 noundef, ptr noundef) #1

declare void @TIFFErrorExt(ptr noundef, ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @TIFFGrowStrips(ptr noundef, i32 noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare hidden i32 @TIFFAppendToStrip(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFWriteRawStrip(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.tiff, ptr %12, i32 0, i32 8
  store ptr %13, ptr %10, align 8, !tbaa !5
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.tiff, ptr %14, i32 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = and i32 %16, 64
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %24, label %19

19:                                               ; preds = %4
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = call i32 @TIFFWriteCheck(ptr noundef %20, i32 noundef 0, ptr noundef @TIFFWriteRawStrip.module)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %96

24:                                               ; preds = %19, %4
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TIFFDirectory, ptr %26, i32 0, i32 33
  %28 = load i32, ptr %27, align 4, !tbaa !20
  %29 = icmp uge i32 %25, %28
  br i1 %29, label %30, label %70

30:                                               ; preds = %24
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.TIFFDirectory, ptr %31, i32 0, i32 24
  %33 = load i16, ptr %32, align 2, !tbaa !21
  %34 = zext i16 %33 to i32
  %35 = icmp eq i32 %34, 2
  br i1 %35, label %36, label %43

36:                                               ; preds = %30
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.tiff, ptr %37, i32 0, i32 51
  %39 = load ptr, ptr %38, align 8, !tbaa !22
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.tiff, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !23
  call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %39, ptr noundef %42, ptr noundef @.str.2)
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %96

43:                                               ; preds = %30
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = load ptr, ptr %10, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.TIFFDirectory, ptr %45, i32 0, i32 32
  %47 = load i32, ptr %46, align 8, !tbaa !24
  %48 = icmp uge i32 %44, %47
  br i1 %48, label %49, label %64

49:                                               ; preds = %43
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.TIFFDirectory, ptr %50, i32 0, i32 2
  %52 = load i32, ptr %51, align 4, !tbaa !25
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.TIFFDirectory, ptr %53, i32 0, i32 16
  %55 = load i32, ptr %54, align 4, !tbaa !26
  %56 = sub i32 %55, 1
  %57 = add i32 %52, %56
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.TIFFDirectory, ptr %58, i32 0, i32 16
  %60 = load i32, ptr %59, align 4, !tbaa !26
  %61 = udiv i32 %57, %60
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.TIFFDirectory, ptr %62, i32 0, i32 32
  store i32 %61, ptr %63, align 8, !tbaa !24
  br label %64

64:                                               ; preds = %49, %43
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = call i32 @TIFFGrowStrips(ptr noundef %65, i32 noundef 1, ptr noundef @TIFFWriteRawStrip.module)
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %69, label %68

68:                                               ; preds = %64
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %96

69:                                               ; preds = %64
  br label %70

70:                                               ; preds = %69, %24
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.tiff, ptr %72, i32 0, i32 14
  store i32 %71, ptr %73, align 8, !tbaa !27
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = load ptr, ptr %10, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.TIFFDirectory, ptr %75, i32 0, i32 32
  %77 = load i32, ptr %76, align 8, !tbaa !24
  %78 = urem i32 %74, %77
  %79 = load ptr, ptr %10, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.TIFFDirectory, ptr %79, i32 0, i32 16
  %81 = load i32, ptr %80, align 4, !tbaa !26
  %82 = mul i32 %78, %81
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.tiff, ptr %83, i32 0, i32 12
  store i32 %82, ptr %84, align 8, !tbaa !28
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = load i32, ptr %7, align 4, !tbaa !9
  %87 = load ptr, ptr %8, align 8, !tbaa !5
  %88 = load i32, ptr %9, align 4, !tbaa !9
  %89 = call i32 @TIFFAppendToStrip(ptr noundef %85, i32 noundef %86, ptr noundef %87, i32 noundef %88)
  %90 = icmp ne i32 %89, 0
  br i1 %90, label %91, label %93

91:                                               ; preds = %70
  %92 = load i32, ptr %9, align 4, !tbaa !9
  br label %94

93:                                               ; preds = %70
  br label %94

94:                                               ; preds = %93, %91
  %95 = phi i32 [ %92, %91 ], [ -1, %93 ]
  store i32 %95, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %96

96:                                               ; preds = %94, %68, %36, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %97 = load i32, ptr %5, align 4
  ret i32 %97
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"tiff", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !6, i64 32, !13, i64 40, !14, i64 48, !17, i64 320, !6, i64 328, !6, i64 336, !10, i64 344, !13, i64 348, !10, i64 352, !10, i64 356, !10, i64 360, !13, i64 364, !10, i64 368, !10, i64 372, !10, i64 376, !10, i64 380, !10, i64 384, !6, i64 392, !6, i64 400, !6, i64 408, !10, i64 416, !6, i64 424, !6, i64 432, !6, i64 440, !6, i64 448, !6, i64 456, !6, i64 464, !6, i64 472, !6, i64 480, !6, i64 488, !6, i64 496, !6, i64 504, !6, i64 512, !6, i64 520, !6, i64 528, !10, i64 536, !10, i64 540, !6, i64 544, !10, i64 552, !6, i64 560, !10, i64 568, !6, i64 576, !10, i64 584, !6, i64 592, !6, i64 600, !6, i64 608, !6, i64 616, !6, i64 624, !6, i64 632, !6, i64 640, !6, i64 648, !6, i64 656, !6, i64 664, !18, i64 672, !6, i64 680, !19, i64 688, !6, i64 712}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !7, i64 0, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !13, i64 60, !13, i64 62, !13, i64 64, !13, i64 66, !13, i64 68, !13, i64 70, !13, i64 72, !13, i64 74, !10, i64 76, !13, i64 80, !13, i64 82, !15, i64 88, !15, i64 96, !16, i64 104, !16, i64 108, !13, i64 112, !13, i64 114, !16, i64 116, !16, i64 120, !7, i64 124, !7, i64 128, !7, i64 152, !13, i64 156, !6, i64 160, !10, i64 168, !10, i64 172, !6, i64 176, !6, i64 184, !10, i64 192, !13, i64 196, !6, i64 200, !7, i64 208, !13, i64 212, !7, i64 216, !10, i64 240, !6, i64 248, !10, i64 256, !6, i64 264}
!15 = !{!"double", !7, i64 0}
!16 = !{!"float", !7, i64 0}
!17 = !{!"", !13, i64 0, !13, i64 2, !10, i64 4}
!18 = !{!"long", !7, i64 0}
!19 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!20 = !{!14, !10, i64 172}
!21 = !{!14, !13, i64 114}
!22 = !{!12, !6, i64 608}
!23 = !{!12, !6, i64 0}
!24 = !{!14, !10, i64 168}
!25 = !{!14, !10, i64 36}
!26 = !{!14, !10, i64 76}
!27 = !{!12, !10, i64 352}
!28 = !{!12, !10, i64 344}


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

@.str.2 = external hidden unnamed_addr constant [56 x i8], align 1
@TIFFWriteRawStrip.module = external hidden constant [18 x i8], align 16

; Function Attrs: nounwind uwtable
declare dso_local i32 @TIFFWriteCheck(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

declare void @TIFFErrorExt(ptr noundef, ptr noundef, ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @TIFFGrowStrips(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i32 @TIFFAppendToStrip(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFWriteRawStrip(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = and i32 %6, 64
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %10

8:                                                ; preds = %4
  %9 = tail call i32 @TIFFWriteCheck(ptr noundef nonnull %0, i32 noundef 0, ptr noundef nonnull @TIFFWriteRawStrip.module)
  %.not27 = icmp eq i32 %9, 0
  br i1 %.not27, label %45, label %10

10:                                               ; preds = %8, %4
  %11 = getelementptr inbounds i8, ptr %0, i64 220
  %12 = load i32, ptr %11, align 4, !tbaa !18
  %.not28 = icmp ugt i32 %12, %1
  br i1 %.not28, label %34, label %13

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 162
  %15 = load i16, ptr %14, align 2, !tbaa !19
  %16 = icmp eq i16 %15, 2
  br i1 %16, label %17, label %21

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 608
  %19 = load ptr, ptr %18, align 8, !tbaa !20
  %20 = load ptr, ptr %0, align 8, !tbaa !21
  tail call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %19, ptr noundef %20, ptr noundef nonnull @.str.2) #2
  br label %45

21:                                               ; preds = %13
  %22 = getelementptr inbounds i8, ptr %0, i64 216
  %23 = load i32, ptr %22, align 8, !tbaa !22
  %.not29 = icmp ugt i32 %23, %1
  br i1 %.not29, label %32, label %24

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %0, i64 84
  %26 = load i32, ptr %25, align 4, !tbaa !23
  %27 = getelementptr inbounds i8, ptr %0, i64 124
  %28 = load i32, ptr %27, align 4, !tbaa !24
  %29 = add i32 %26, -1
  %30 = add i32 %29, %28
  %31 = udiv i32 %30, %28
  store i32 %31, ptr %22, align 8, !tbaa !22
  br label %32

32:                                               ; preds = %24, %21
  %33 = tail call i32 @TIFFGrowStrips(ptr noundef nonnull %0, i32 noundef 1, ptr noundef nonnull @TIFFWriteRawStrip.module)
  %.not30 = icmp eq i32 %33, 0
  br i1 %.not30, label %45, label %34

34:                                               ; preds = %32, %10
  %35 = getelementptr inbounds i8, ptr %0, i64 352
  store i32 %1, ptr %35, align 8, !tbaa !25
  %36 = getelementptr inbounds i8, ptr %0, i64 216
  %37 = load i32, ptr %36, align 8, !tbaa !22
  %38 = urem i32 %1, %37
  %39 = getelementptr inbounds i8, ptr %0, i64 124
  %40 = load i32, ptr %39, align 4, !tbaa !24
  %41 = mul i32 %40, %38
  %42 = getelementptr inbounds i8, ptr %0, i64 344
  store i32 %41, ptr %42, align 8, !tbaa !26
  %43 = tail call i32 @TIFFAppendToStrip(ptr noundef nonnull %0, i32 noundef %1, ptr noundef %2, i32 noundef %3)
  %.not31 = icmp eq i32 %43, 0
  %44 = select i1 %.not31, i32 -1, i32 %3
  br label %45

45:                                               ; preds = %32, %8, %34, %17
  %.0 = phi i32 [ -1, %17 ], [ %44, %34 ], [ -1, %8 ], [ -1, %32 ]
  ret i32 %.0
}

attributes #0 = { 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 #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 }

!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, !10, i64 16}
!6 = !{!"tiff", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 32, !11, i64 40, !12, i64 48, !15, i64 320, !7, i64 328, !7, i64 336, !10, i64 344, !11, i64 348, !10, i64 352, !10, i64 356, !10, i64 360, !11, i64 364, !10, i64 368, !10, i64 372, !10, i64 376, !10, i64 380, !10, i64 384, !7, i64 392, !7, i64 400, !7, i64 408, !10, i64 416, !7, i64 424, !7, i64 432, !7, i64 440, !7, i64 448, !7, i64 456, !7, i64 464, !7, i64 472, !7, i64 480, !7, i64 488, !7, i64 496, !7, i64 504, !7, i64 512, !7, i64 520, !7, i64 528, !10, i64 536, !10, i64 540, !7, i64 544, !10, i64 552, !7, i64 560, !10, i64 568, !7, i64 576, !10, i64 584, !7, i64 592, !7, i64 600, !7, i64 608, !7, i64 616, !7, i64 624, !7, i64 632, !7, i64 640, !7, i64 648, !7, i64 656, !7, i64 664, !16, i64 672, !7, i64 680, !17, i64 688, !7, i64 712}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"", !8, i64 0, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !11, i64 60, !11, i64 62, !11, i64 64, !11, i64 66, !11, i64 68, !11, i64 70, !11, i64 72, !11, i64 74, !10, i64 76, !11, i64 80, !11, i64 82, !13, i64 88, !13, i64 96, !14, i64 104, !14, i64 108, !11, i64 112, !11, i64 114, !14, i64 116, !14, i64 120, !8, i64 124, !8, i64 128, !8, i64 152, !11, i64 156, !7, i64 160, !10, i64 168, !10, i64 172, !7, i64 176, !7, i64 184, !10, i64 192, !11, i64 196, !7, i64 200, !8, i64 208, !11, i64 212, !8, i64 216, !10, i64 240, !7, i64 248, !10, i64 256, !7, i64 264}
!13 = !{!"double", !8, i64 0}
!14 = !{!"float", !8, i64 0}
!15 = !{!"", !11, i64 0, !11, i64 2, !10, i64 4}
!16 = !{!"long", !8, i64 0}
!17 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16}
!18 = !{!12, !10, i64 172}
!19 = !{!12, !11, i64 114}
!20 = !{!6, !7, i64 608}
!21 = !{!6, !7, i64 0}
!22 = !{!12, !10, i64 168}
!23 = !{!12, !10, i64 36}
!24 = !{!12, !10, i64 76}
!25 = !{!6, !10, i64 352}
!26 = !{!6, !10, i64 344}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass