; LLVM IR from mkazhdan/DMG :: Util/TIFF/tif_dirinfo.ll (TIFFSampleToTagType)

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

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

; 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
define dso_local i32 @_TIFFSampleToTagType(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.tiff, ptr %6, i32 0, i32 8
  %8 = getelementptr inbounds %struct.TIFFDirectory, ptr %7, i32 0, i32 8
  %9 = load i16, ptr %8, align 4, !tbaa !9
  %10 = zext i16 %9 to i32
  %11 = and i32 %10, 7
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %21

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.tiff, ptr %14, i32 0, i32 8
  %16 = getelementptr inbounds %struct.TIFFDirectory, ptr %15, i32 0, i32 8
  %17 = load i16, ptr %16, align 4, !tbaa !9
  %18 = zext i16 %17 to i32
  %19 = lshr i32 %18, 3
  %20 = add i32 %19, 1
  br label %28

21:                                               ; preds = %1
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.tiff, ptr %22, i32 0, i32 8
  %24 = getelementptr inbounds %struct.TIFFDirectory, ptr %23, i32 0, i32 8
  %25 = load i16, ptr %24, align 4, !tbaa !9
  %26 = zext i16 %25 to i32
  %27 = lshr i32 %26, 3
  br label %28

28:                                               ; preds = %21, %13
  %29 = phi i32 [ %20, %13 ], [ %27, %21 ]
  store i32 %29, ptr %4, align 4, !tbaa !19
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tiff, ptr %30, i32 0, i32 8
  %32 = getelementptr inbounds %struct.TIFFDirectory, ptr %31, i32 0, i32 9
  %33 = load i16, ptr %32, align 2, !tbaa !20
  %34 = zext i16 %33 to i32
  switch i32 %34, label %63 [
    i32 3, label %35
    i32 2, label %40
    i32 1, label %51
    i32 4, label %62
  ]

35:                                               ; preds = %28
  %36 = load i32, ptr %4, align 4, !tbaa !19
  %37 = icmp eq i32 %36, 4
  %38 = zext i1 %37 to i64
  %39 = select i1 %37, i32 11, i32 12
  store i32 %39, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %64

40:                                               ; preds = %28
  %41 = load i32, ptr %4, align 4, !tbaa !19
  %42 = icmp ule i32 %41, 1
  br i1 %42, label %43, label %44

43:                                               ; preds = %40
  br label %49

44:                                               ; preds = %40
  %45 = load i32, ptr %4, align 4, !tbaa !19
  %46 = icmp ule i32 %45, 2
  %47 = zext i1 %46 to i64
  %48 = select i1 %46, i32 8, i32 9
  br label %49

49:                                               ; preds = %44, %43
  %50 = phi i32 [ 6, %43 ], [ %48, %44 ]
  store i32 %50, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %64

51:                                               ; preds = %28
  %52 = load i32, ptr %4, align 4, !tbaa !19
  %53 = icmp ule i32 %52, 1
  br i1 %53, label %54, label %55

54:                                               ; preds = %51
  br label %60

55:                                               ; preds = %51
  %56 = load i32, ptr %4, align 4, !tbaa !19
  %57 = icmp ule i32 %56, 2
  %58 = zext i1 %57 to i64
  %59 = select i1 %57, i32 3, i32 4
  br label %60

60:                                               ; preds = %55, %54
  %61 = phi i32 [ 1, %54 ], [ %59, %55 ]
  store i32 %61, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %64

62:                                               ; preds = %28
  store i32 7, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %64

63:                                               ; preds = %28
  store i32 7, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %64

64:                                               ; preds = %63, %62, %60, %49, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %65 = load i32, ptr %2, align 4
  ret i32 %65
}

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 = { 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, !12, i64 108}
!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 = !{!11, !11, i64 0}
!20 = !{!10, !12, i64 110}


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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local range(i32 1, 13) i32 @_TIFFSampleToTagType(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 108
  %3 = load i16, ptr %2, align 4, !tbaa !5
  %4 = zext i16 %3 to i32
  %5 = and i32 %4, 7
  %.not = icmp ne i32 %5, 0
  %6 = lshr i32 %4, 3
  %7 = zext i1 %.not to i32
  %8 = add nuw nsw i32 %6, %7
  %9 = getelementptr inbounds i8, ptr %0, i64 110
  %10 = load i16, ptr %9, align 2, !tbaa !18
  switch i16 %10, label %23 [
    i16 3, label %11
    i16 2, label %14
    i16 1, label %19
  ]

11:                                               ; preds = %1
  %12 = icmp eq i32 %8, 4
  %13 = select i1 %12, i32 11, i32 12
  br label %23

14:                                               ; preds = %1
  %15 = icmp ult i32 %8, 2
  %16 = icmp eq i32 %8, 2
  %17 = select i1 %16, i32 8, i32 9
  %18 = select i1 %15, i32 6, i32 %17
  br label %23

19:                                               ; preds = %1
  %20 = icmp eq i32 %8, 2
  %21 = select i1 %20, i32 3, i32 4
  %.inv = icmp ugt i32 %8, 1
  %22 = select i1 %.inv, i32 %21, i32 1
  br label %23

23:                                               ; preds = %1, %19, %14, %11
  %.0 = phi i32 [ %22, %19 ], [ %18, %14 ], [ %13, %11 ], [ 7, %1 ]
  ret i32 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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, !11, i64 108}
!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, !11, i64 110}


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