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

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

@TIFFReadRawTile.module = external hidden constant [16 x i8], align 16
@.str.6 = external hidden unnamed_addr constant [32 x i8], align 1

; 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 hidden i32 @TIFFCheckRead(ptr noundef, i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFReadRawTile(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
  %12 = 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
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.tiff, ptr %13, i32 0, i32 8
  store ptr %14, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = call i32 @TIFFCheckRead(ptr noundef %15, i32 noundef 1)
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %19, label %18

18:                                               ; preds = %4
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %60

19:                                               ; preds = %4
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.TIFFDirectory, ptr %21, i32 0, i32 33
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = icmp uge i32 %20, %23
  br i1 %24, label %25, label %38

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.tiff, ptr %26, i32 0, i32 51
  %28 = load ptr, ptr %27, align 8, !tbaa !16
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.tiff, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !21
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = zext i32 %32 to i64
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.TIFFDirectory, ptr %34, i32 0, i32 33
  %36 = load i32, ptr %35, align 4, !tbaa !11
  %37 = zext i32 %36 to i64
  call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %28, ptr noundef %31, ptr noundef @.str.6, i64 noundef %33, i64 noundef %37)
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %60

38:                                               ; preds = %19
  %39 = load ptr, ptr %10, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.TIFFDirectory, ptr %39, i32 0, i32 35
  %41 = load ptr, ptr %40, align 8, !tbaa !22
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %41, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  store i32 %45, ptr %11, align 4, !tbaa !9
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = icmp ne i32 %46, -1
  br i1 %47, label %48, label %54

48:                                               ; preds = %38
  %49 = load i32, ptr %9, align 4, !tbaa !9
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %52, label %54

52:                                               ; preds = %48
  %53 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %53, ptr %11, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %52, %48, %38
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load i32, ptr %7, align 4, !tbaa !9
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = call i32 @TIFFReadRawTile1(ptr noundef %55, i32 noundef %56, ptr noundef %57, i32 noundef %58, ptr noundef @TIFFReadRawTile.module)
  store i32 %59, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %60

60:                                               ; preds = %54, %25, %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %61 = load i32, ptr %5, align 4
  ret i32 %61
}

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

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 172}
!12 = !{!"", !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, !14, i64 88, !14, i64 96, !15, i64 104, !15, i64 108, !13, i64 112, !13, i64 114, !15, i64 116, !15, 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}
!13 = !{!"short", !7, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!"float", !7, i64 0}
!16 = !{!17, !6, i64 608}
!17 = !{!"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, !12, i64 48, !18, 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, !19, i64 672, !6, i64 680, !20, i64 688, !6, i64 712}
!18 = !{!"", !13, i64 0, !13, i64 2, !10, i64 4}
!19 = !{!"long", !7, i64 0}
!20 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!21 = !{!17, !6, i64 0}
!22 = !{!12, !6, i64 184}


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

@TIFFReadRawTile.module = external hidden constant [16 x i8], align 16
@.str.6 = external hidden unnamed_addr constant [32 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @TIFFReadRawTile(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = tail call i32 @TIFFCheckRead(ptr noundef %0, i32 noundef 1)
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %23, label %6

6:                                                ; preds = %4
  %7 = getelementptr inbounds i8, ptr %0, i64 220
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %.not23 = icmp ugt i32 %8, %1
  br i1 %.not23, label %15, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %0, i64 608
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %12 = load ptr, ptr %0, align 8, !tbaa !19
  %13 = zext i32 %1 to i64
  %14 = zext i32 %8 to i64
  tail call void (ptr, ptr, ptr, ...) @TIFFErrorExt(ptr noundef %11, ptr noundef %12, ptr noundef nonnull @.str.6, i64 noundef %13, i64 noundef %14) #3
  br label %23

15:                                               ; preds = %6
  %16 = getelementptr inbounds i8, ptr %0, i64 232
  %17 = load ptr, ptr %16, align 8, !tbaa !20
  %18 = zext i32 %1 to i64
  %19 = getelementptr inbounds i32, ptr %17, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !21
  %.not24.not = icmp eq i32 %3, -1
  %21 = tail call i32 @llvm.smin.i32(i32 %20, i32 %3)
  %.0 = select i1 %.not24.not, i32 %20, i32 %21
  %22 = tail call i32 @TIFFReadRawTile1(ptr noundef nonnull %0, i32 noundef %1, ptr noundef %2, i32 noundef %.0, ptr noundef nonnull @TIFFReadRawTile.module)
  br label %23

23:                                               ; preds = %4, %15, %9
  %.018 = phi i32 [ -1, %9 ], [ %22, %15 ], [ -1, %4 ]
  ret i32 %.018
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.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, !9, i64 172}
!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 = !{!15, !13, i64 608}
!15 = !{!"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, !16, 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, !17, i64 672, !13, i64 680, !18, i64 688, !13, i64 712}
!16 = !{!"", !10, i64 0, !10, i64 2, !9, i64 4}
!17 = !{!"long", !7, i64 0}
!18 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16}
!19 = !{!15, !13, i64 0}
!20 = !{!6, !13, i64 184}
!21 = !{!9, !9, i64 0}


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