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

@TIFFWriteBufferSetup.module = external hidden constant [21 x i8], align 16
@.str.12 = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFWriteBufferSetup(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.tiff, ptr %8, i32 0, i32 43
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %29

12:                                               ; preds = %3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.tiff, ptr %13, i32 0, i32 3
  %15 = load i32, ptr %14, align 8, !tbaa !20
  %16 = and i32 %15, 512
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %26

18:                                               ; preds = %12
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.tiff, ptr %19, i32 0, i32 43
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  call void @_TIFFfree(ptr noundef %21)
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.tiff, ptr %22, i32 0, i32 3
  %24 = load i32, ptr %23, align 8, !tbaa !20
  %25 = and i32 %24, -513
  store i32 %25, ptr %23, align 8, !tbaa !20
  br label %26

26:                                               ; preds = %18, %12
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.tiff, ptr %27, i32 0, i32 43
  store ptr null, ptr %28, align 8, !tbaa !11
  br label %29

29:                                               ; preds = %26, %3
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = icmp eq i32 %30, -1
  br i1 %31, label %32, label %51

32:                                               ; preds = %29
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.tiff, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 8, !tbaa !20
  %36 = and i32 %35, 1024
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %42

38:                                               ; preds = %32
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.tiff, ptr %39, i32 0, i32 21
  %41 = load i32, ptr %40, align 4, !tbaa !21
  br label %45

42:                                               ; preds = %32
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = call i32 @TIFFStripSize(ptr noundef %43)
  br label %45

45:                                               ; preds = %42, %38
  %46 = phi i32 [ %41, %38 ], [ %44, %42 ]
  store i32 %46, ptr %7, align 4, !tbaa !9
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = icmp slt i32 %47, 8192
  br i1 %48, label %49, label %50

49:                                               ; preds = %45
  store i32 8192, ptr %7, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %49, %45
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %50, %29
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = icmp eq ptr %52, null
  br i1 %53, label %54, label %71

54:                                               ; preds = %51
  %55 = load i32, ptr %7, align 4, !tbaa !9
  %56 = call ptr @_TIFFmalloc(i32 noundef %55)
  store ptr %56, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = icmp eq ptr %57, null
  br i1 %58, label %59, label %66

59:                                               ; preds = %54
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.tiff, ptr %60, i32 0, i32 51
  %62 = load ptr, ptr %61, align 8, !tbaa !22
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.tiff, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !23
  call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %62, ptr noundef @TIFFWriteBufferSetup.module, ptr noundef @.str.12, ptr noundef %65)
  store i32 0, ptr %4, align 4
  br label %94

66:                                               ; preds = %54
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.tiff, ptr %67, i32 0, i32 3
  %69 = load i32, ptr %68, align 8, !tbaa !20
  %70 = or i32 %69, 512
  store i32 %70, ptr %68, align 8, !tbaa !20
  br label %76

71:                                               ; preds = %51
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.tiff, ptr %72, i32 0, i32 3
  %74 = load i32, ptr %73, align 8, !tbaa !20
  %75 = and i32 %74, -513
  store i32 %75, ptr %73, align 8, !tbaa !20
  br label %76

76:                                               ; preds = %71, %66
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.tiff, ptr %78, i32 0, i32 43
  store ptr %77, ptr %79, align 8, !tbaa !11
  %80 = load i32, ptr %7, align 4, !tbaa !9
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.tiff, ptr %81, i32 0, i32 44
  store i32 %80, ptr %82, align 8, !tbaa !24
  %83 = load ptr, ptr %5, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.tiff, ptr %83, i32 0, i32 46
  store i32 0, ptr %84, align 8, !tbaa !25
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.tiff, ptr %85, i32 0, i32 43
  %87 = load ptr, ptr %86, align 8, !tbaa !11
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.tiff, ptr %88, i32 0, i32 45
  store ptr %87, ptr %89, align 8, !tbaa !26
  %90 = load ptr, ptr %5, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.tiff, ptr %90, i32 0, i32 3
  %92 = load i32, ptr %91, align 8, !tbaa !20
  %93 = or i32 %92, 16
  store i32 %93, ptr %91, align 8, !tbaa !20
  store i32 1, ptr %4, align 4
  br label %94

94:                                               ; preds = %76, %59
  %95 = load i32, ptr %4, align 4
  ret i32 %95
}

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

declare ptr @_TIFFmalloc(i32 noundef) #1

declare void @_TIFFfree(ptr noundef) #1

declare i32 @TIFFStripSize(ptr noundef) #1

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

!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, !6, i64 544}
!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 = !{!12, !10, i64 16}
!21 = !{!12, !10, i64 380}
!22 = !{!12, !6, i64 608}
!23 = !{!12, !6, i64 0}
!24 = !{!12, !10, i64 552}
!25 = !{!12, !10, i64 568}
!26 = !{!12, !6, i64 560}


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

@TIFFWriteBufferSetup.module = external hidden constant [21 x i8], align 16
@.str.12 = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @TIFFWriteBufferSetup(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 544
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %14, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load i32, ptr %7, align 8, !tbaa !18
  %9 = and i32 %8, 512
  %.not30 = icmp eq i32 %9, 0
  br i1 %.not30, label %13, label %10

10:                                               ; preds = %6
  tail call void @_TIFFfree(ptr noundef nonnull %5) #3
  %11 = load i32, ptr %7, align 8, !tbaa !18
  %12 = and i32 %11, -513
  store i32 %12, ptr %7, align 8, !tbaa !18
  br label %13

13:                                               ; preds = %10, %6
  store ptr null, ptr %4, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %13, %3
  %15 = icmp eq i32 %2, -1
  br i1 %15, label %16, label %26

16:                                               ; preds = %14
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load i32, ptr %17, align 8, !tbaa !18
  %19 = and i32 %18, 1024
  %.not31 = icmp eq i32 %19, 0
  br i1 %.not31, label %23, label %20

20:                                               ; preds = %16
  %21 = getelementptr inbounds i8, ptr %0, i64 380
  %22 = load i32, ptr %21, align 4, !tbaa !19
  br label %.thread

23:                                               ; preds = %16
  %24 = tail call i32 @TIFFStripSize(ptr noundef nonnull %0) #3
  br label %.thread

.thread:                                          ; preds = %20, %23
  %25 = phi i32 [ %22, %20 ], [ %24, %23 ]
  %spec.store.select = tail call i32 @llvm.smax.i32(i32 %25, i32 8192)
  br label %28

26:                                               ; preds = %14
  %27 = icmp eq ptr %1, null
  br i1 %27, label %28, label %39

28:                                               ; preds = %.thread, %26
  %.034 = phi i32 [ %spec.store.select, %.thread ], [ %2, %26 ]
  %29 = tail call ptr @_TIFFmalloc(i32 noundef %.034) #3
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %35

31:                                               ; preds = %28
  %32 = getelementptr inbounds i8, ptr %0, i64 608
  %33 = load ptr, ptr %32, align 8, !tbaa !20
  %34 = load ptr, ptr %0, align 8, !tbaa !21
  tail call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %33, ptr noundef nonnull @TIFFWriteBufferSetup.module, ptr noundef nonnull @.str.12, ptr noundef %34) #3
  br label %50

35:                                               ; preds = %28
  %36 = getelementptr inbounds i8, ptr %0, i64 16
  %37 = load i32, ptr %36, align 8, !tbaa !18
  %38 = or i32 %37, 512
  br label %43

39:                                               ; preds = %26
  %40 = getelementptr inbounds i8, ptr %0, i64 16
  %41 = load i32, ptr %40, align 8, !tbaa !18
  %42 = and i32 %41, -513
  br label %43

43:                                               ; preds = %39, %35
  %44 = phi i32 [ %38, %35 ], [ %42, %39 ]
  %.035 = phi i32 [ %.034, %35 ], [ %2, %39 ]
  %.1 = phi ptr [ %29, %35 ], [ %1, %39 ]
  store ptr %.1, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %0, i64 552
  store i32 %.035, ptr %45, align 8, !tbaa !22
  %46 = getelementptr inbounds i8, ptr %0, i64 568
  store i32 0, ptr %46, align 8, !tbaa !23
  %47 = getelementptr inbounds i8, ptr %0, i64 560
  store ptr %.1, ptr %47, align 8, !tbaa !24
  %48 = getelementptr inbounds i8, ptr %0, i64 16
  %49 = or i32 %44, 16
  store i32 %49, ptr %48, align 8, !tbaa !18
  br label %50

50:                                               ; preds = %43, %31
  %.026 = phi i32 [ 0, %31 ], [ 1, %43 ]
  ret i32 %.026
}

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

declare ptr @_TIFFmalloc(i32 noundef) local_unnamed_addr #1

declare void @_TIFFfree(ptr noundef) local_unnamed_addr #1

declare i32 @TIFFStripSize(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !7, i64 544}
!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 = !{!6, !10, i64 16}
!19 = !{!6, !10, i64 380}
!20 = !{!6, !7, i64 608}
!21 = !{!6, !7, i64 0}
!22 = !{!6, !10, i64 552}
!23 = !{!6, !10, i64 568}
!24 = !{!6, !7, i64 560}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass