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

declare void @TIFFReverseBits(ptr noundef, i64 noundef) #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 @TIFFFlushData1(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.tiff, ptr %4, i32 0, i32 46
  %6 = load i32, ptr %5, align 8, !tbaa !9
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %8, label %67

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.tiff, ptr %9, i32 0, i32 3
  %11 = load i32, ptr %10, align 8, !tbaa !19
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.tiff, ptr %12, i32 0, i32 8
  %14 = getelementptr inbounds %struct.TIFFDirectory, ptr %13, i32 0, i32 13
  %15 = load i16, ptr %14, align 2, !tbaa !20
  %16 = zext i16 %15 to i32
  %17 = and i32 %11, %16
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %33, label %19

19:                                               ; preds = %8
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.tiff, ptr %20, i32 0, i32 3
  %22 = load i32, ptr %21, align 8, !tbaa !19
  %23 = and i32 %22, 256
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %33

25:                                               ; preds = %19
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.tiff, ptr %26, i32 0, i32 43
  %28 = load ptr, ptr %27, align 8, !tbaa !21
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.tiff, ptr %29, i32 0, i32 46
  %31 = load i32, ptr %30, align 8, !tbaa !9
  %32 = sext i32 %31 to i64
  call void @TIFFReverseBits(ptr noundef %28, i64 noundef %32)
  br label %33

33:                                               ; preds = %25, %19, %8
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.tiff, ptr %35, i32 0, i32 3
  %37 = load i32, ptr %36, align 8, !tbaa !19
  %38 = and i32 %37, 1024
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %44

40:                                               ; preds = %33
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.tiff, ptr %41, i32 0, i32 20
  %43 = load i32, ptr %42, align 8, !tbaa !22
  br label %48

44:                                               ; preds = %33
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.tiff, ptr %45, i32 0, i32 14
  %47 = load i32, ptr %46, align 8, !tbaa !23
  br label %48

48:                                               ; preds = %44, %40
  %49 = phi i32 [ %43, %40 ], [ %47, %44 ]
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.tiff, ptr %50, i32 0, i32 43
  %52 = load ptr, ptr %51, align 8, !tbaa !21
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.tiff, ptr %53, i32 0, i32 46
  %55 = load i32, ptr %54, align 8, !tbaa !9
  %56 = call i32 @TIFFAppendToStrip(ptr noundef %34, i32 noundef %49, ptr noundef %52, i32 noundef %55)
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %59, label %58

58:                                               ; preds = %48
  store i32 0, ptr %2, align 4
  br label %68

59:                                               ; preds = %48
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.tiff, ptr %60, i32 0, i32 46
  store i32 0, ptr %61, align 8, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.tiff, ptr %62, i32 0, i32 43
  %64 = load ptr, ptr %63, align 8, !tbaa !21
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.tiff, ptr %65, i32 0, i32 45
  store ptr %64, ptr %66, align 8, !tbaa !24
  br label %67

67:                                               ; preds = %59, %1
  store i32 1, ptr %2, align 4
  br label %68

68:                                               ; preds = %67, %58
  %69 = load i32, ptr %2, align 4
  ret i32 %69
}

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

!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, !11, i64 568}
!10 = !{!"tiff", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !6, i64 32, !12, i64 40, !13, i64 48, !16, i64 320, !6, i64 328, !6, i64 336, !11, i64 344, !12, i64 348, !11, i64 352, !11, i64 356, !11, i64 360, !12, i64 364, !11, i64 368, !11, i64 372, !11, i64 376, !11, i64 380, !11, i64 384, !6, i64 392, !6, i64 400, !6, i64 408, !11, 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, !11, i64 536, !11, i64 540, !6, i64 544, !11, i64 552, !6, i64 560, !11, i64 568, !6, i64 576, !11, 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, !17, i64 672, !6, i64 680, !18, i64 688, !6, i64 712}
!11 = !{!"int", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"", !7, i64 0, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !12, i64 60, !12, i64 62, !12, i64 64, !12, i64 66, !12, i64 68, !12, i64 70, !12, i64 72, !12, i64 74, !11, i64 76, !12, i64 80, !12, i64 82, !14, i64 88, !14, i64 96, !15, i64 104, !15, i64 108, !12, i64 112, !12, i64 114, !15, i64 116, !15, i64 120, !7, i64 124, !7, i64 128, !7, i64 152, !12, i64 156, !6, i64 160, !11, i64 168, !11, i64 172, !6, i64 176, !6, i64 184, !11, i64 192, !12, i64 196, !6, i64 200, !7, i64 208, !12, i64 212, !7, i64 216, !11, i64 240, !6, i64 248, !11, i64 256, !6, i64 264}
!14 = !{!"double", !7, i64 0}
!15 = !{!"float", !7, i64 0}
!16 = !{!"", !12, i64 0, !12, i64 2, !11, i64 4}
!17 = !{!"long", !7, i64 0}
!18 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!19 = !{!10, !11, i64 16}
!20 = !{!10, !12, i64 118}
!21 = !{!10, !6, i64 544}
!22 = !{!10, !11, i64 376}
!23 = !{!10, !11, i64 352}
!24 = !{!10, !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"

declare void @TIFFReverseBits(ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @TIFFFlushData1(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 568
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %5, label %28

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i32, ptr %6, align 8, !tbaa !18
  %8 = getelementptr inbounds i8, ptr %0, i64 118
  %9 = load i16, ptr %8, align 2, !tbaa !19
  %10 = or i16 %9, 256
  %11 = zext i16 %10 to i32
  %12 = and i32 %7, %11
  %or.cond = icmp eq i32 %12, 0
  br i1 %or.cond, label %13, label %17

13:                                               ; preds = %5
  %14 = getelementptr inbounds i8, ptr %0, i64 544
  %15 = load ptr, ptr %14, align 8, !tbaa !20
  %16 = zext nneg i32 %3 to i64
  tail call void @TIFFReverseBits(ptr noundef %15, i64 noundef %16) #2
  %.pre = load i32, ptr %6, align 8, !tbaa !18
  %.pre19 = load i32, ptr %2, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %13, %5
  %18 = phi i32 [ %.pre19, %13 ], [ %3, %5 ]
  %19 = phi i32 [ %.pre, %13 ], [ %7, %5 ]
  %20 = and i32 %19, 1024
  %.not16 = icmp eq i32 %20, 0
  %.in.v = select i1 %.not16, i64 352, i64 376
  %.in = getelementptr inbounds i8, ptr %0, i64 %.in.v
  %21 = load i32, ptr %.in, align 8, !tbaa !21
  %22 = getelementptr inbounds i8, ptr %0, i64 544
  %23 = load ptr, ptr %22, align 8, !tbaa !20
  %24 = tail call i32 @TIFFAppendToStrip(ptr noundef nonnull %0, i32 noundef %21, ptr noundef %23, i32 noundef %18)
  %.not17 = icmp eq i32 %24, 0
  br i1 %.not17, label %28, label %25

25:                                               ; preds = %17
  store i32 0, ptr %2, align 8, !tbaa !5
  %26 = load ptr, ptr %22, align 8, !tbaa !20
  %27 = getelementptr inbounds i8, ptr %0, i64 560
  store ptr %26, ptr %27, align 8, !tbaa !22
  br label %28

28:                                               ; preds = %1, %25, %17
  %.0 = phi i32 [ 0, %17 ], [ 1, %25 ], [ 1, %1 ]
  ret i32 %.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, !10, i64 568}
!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, !11, i64 118}
!20 = !{!6, !7, i64 544}
!21 = !{!10, !10, i64 0}
!22 = !{!6, !7, i64 560}


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