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

; 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 = external hidden unnamed_addr constant [34 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFComputeStrip(ptr noundef %0, i32 noundef %1, i16 noundef zeroext %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i16, align 2
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i16 %2, ptr %7, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.tiff, ptr %11, i32 0, i32 8
  store ptr %12, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load ptr, ptr %8, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.TIFFDirectory, ptr %14, i32 0, i32 16
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = udiv i32 %13, %16
  store i32 %17, ptr %9, align 4, !tbaa !9
  %18 = load ptr, ptr %8, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.TIFFDirectory, ptr %18, i32 0, i32 24
  %20 = load i16, ptr %19, align 2, !tbaa !17
  %21 = zext i16 %20 to i32
  %22 = icmp eq i32 %21, 2
  br i1 %22, label %23, label %53

23:                                               ; preds = %3
  %24 = load i16, ptr %7, align 2, !tbaa !11
  %25 = zext i16 %24 to i32
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TIFFDirectory, ptr %26, i32 0, i32 15
  %28 = load i16, ptr %27, align 2, !tbaa !18
  %29 = zext i16 %28 to i32
  %30 = icmp sge i32 %25, %29
  br i1 %30, label %31, label %44

31:                                               ; preds = %23
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.tiff, ptr %32, i32 0, i32 51
  %34 = load ptr, ptr %33, align 8, !tbaa !19
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.tiff, ptr %35, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !24
  %38 = load i16, ptr %7, align 2, !tbaa !11
  %39 = zext i16 %38 to i64
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.TIFFDirectory, ptr %40, i32 0, i32 15
  %42 = load i16, ptr %41, align 2, !tbaa !18
  %43 = zext i16 %42 to i64
  call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %34, ptr noundef %37, ptr noundef @.str, i64 noundef %39, i64 noundef %43)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %55

44:                                               ; preds = %23
  %45 = load i16, ptr %7, align 2, !tbaa !11
  %46 = zext i16 %45 to i32
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.TIFFDirectory, ptr %47, i32 0, i32 32
  %49 = load i32, ptr %48, align 8, !tbaa !25
  %50 = mul i32 %46, %49
  %51 = load i32, ptr %9, align 4, !tbaa !9
  %52 = add i32 %51, %50
  store i32 %52, ptr %9, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %44, %3
  %54 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %54, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %55

55:                                               ; preds = %53, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %56 = load i32, ptr %4, align 4
  ret i32 %56
}

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !10, i64 76}
!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, !12, i64 60, !12, i64 62, !12, i64 64, !12, i64 66, !12, i64 68, !12, i64 70, !12, i64 72, !12, i64 74, !10, i64 76, !12, i64 80, !12, i64 82, !15, i64 88, !15, i64 96, !16, i64 104, !16, i64 108, !12, i64 112, !12, i64 114, !16, i64 116, !16, i64 120, !7, i64 124, !7, i64 128, !7, i64 152, !12, i64 156, !6, i64 160, !10, i64 168, !10, i64 172, !6, i64 176, !6, i64 184, !10, i64 192, !12, i64 196, !6, i64 200, !7, i64 208, !12, 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 = !{!14, !12, i64 114}
!18 = !{!14, !12, i64 74}
!19 = !{!20, !6, i64 608}
!20 = !{!"tiff", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !6, i64 32, !12, i64 40, !14, i64 48, !21, i64 320, !6, i64 328, !6, i64 336, !10, i64 344, !12, i64 348, !10, i64 352, !10, i64 356, !10, i64 360, !12, 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, !22, i64 672, !6, i64 680, !23, i64 688, !6, i64 712}
!21 = !{!"", !12, i64 0, !12, i64 2, !10, i64 4}
!22 = !{!"long", !7, i64 0}
!23 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!24 = !{!20, !6, i64 0}
!25 = !{!14, !10, i64 168}


; 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 = external hidden unnamed_addr constant [34 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFComputeStrip(ptr nocapture noundef readonly %0, i32 noundef %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 124
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = udiv i32 %1, %5
  %7 = getelementptr inbounds i8, ptr %0, i64 162
  %8 = load i16, ptr %7, align 2, !tbaa !14
  %9 = icmp eq i16 %8, 2
  br i1 %9, label %10, label %25

10:                                               ; preds = %3
  %11 = getelementptr inbounds i8, ptr %0, i64 122
  %12 = load i16, ptr %11, align 2, !tbaa !15
  %.not = icmp ugt i16 %12, %2
  br i1 %.not, label %19, label %13

13:                                               ; preds = %10
  %14 = getelementptr inbounds i8, ptr %0, i64 608
  %15 = load ptr, ptr %14, align 8, !tbaa !16
  %16 = load ptr, ptr %0, align 8, !tbaa !21
  %17 = zext i16 %2 to i64
  %18 = zext i16 %12 to i64
  tail call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %15, ptr noundef %16, ptr noundef nonnull @.str, i64 noundef %17, i64 noundef %18) #2
  br label %25

19:                                               ; preds = %10
  %20 = zext i16 %2 to i32
  %21 = getelementptr inbounds i8, ptr %0, i64 216
  %22 = load i32, ptr %21, align 8, !tbaa !22
  %23 = mul i32 %22, %20
  %24 = add i32 %23, %6
  br label %25

25:                                               ; preds = %3, %19, %13
  %.014 = phi i32 [ 0, %13 ], [ %24, %19 ], [ %6, %3 ]
  ret i32 %.014
}

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


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