; LLVM IR from aagdev/mlimgsynth :: src/ccommon (image_io)

; 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.ImageCodec = type { ptr, %struct.ImageCodecSub, %struct.ImageCodecSub, ptr, ptr }
%struct.ImageCodecSub = type { ptr, i32, i32, ptr, ptr, ptr, ptr, ptr }

@imgio_codecs = external dso_local global [32 x ptr], align 16

; 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 ptr @img_codec_detect_ext(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca [8 x i8], align 1
  %7 = alloca i8, align 1
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = getelementptr inbounds [8 x i8], ptr %6, i64 0, i64 0
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = call i64 @str_tolower(ptr noundef %10, i64 noundef 8, ptr noundef %11)
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #3
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = and i32 %13, 2
  %15 = icmp ne i32 %14, 0
  %16 = zext i1 %15 to i8
  store i8 %16, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %71, %2
  %18 = load i32, ptr %8, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %24, label %23

23:                                               ; preds = %17
  store i32 2, ptr %9, align 4
  br label %74

24:                                               ; preds = %17
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %26
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.ImageCodec, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %70

32:                                               ; preds = %24
  %33 = load i8, ptr %7, align 1, !tbaa !11, !range !16, !noundef !17
  %34 = trunc i8 %33 to i1
  br i1 %34, label %35, label %44

35:                                               ; preds = %32
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %37
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.ImageCodec, ptr %39, i32 0, i32 2
  %41 = getelementptr inbounds %struct.ImageCodecSub, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !18
  %43 = icmp ne ptr %42, null
  br i1 %43, label %56, label %44

44:                                               ; preds = %35, %32
  %45 = load i8, ptr %7, align 1, !tbaa !11, !range !16, !noundef !17
  %46 = trunc i8 %45 to i1
  br i1 %46, label %70, label %47

47:                                               ; preds = %44
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %49
  %51 = load ptr, ptr %50, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.ImageCodec, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds %struct.ImageCodecSub, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !19
  %55 = icmp ne ptr %54, null
  br i1 %55, label %56, label %70

56:                                               ; preds = %47, %35
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %58
  %60 = load ptr, ptr %59, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.ImageCodec, ptr %60, i32 0, i32 0
  %62 = load ptr, ptr %61, align 8, !tbaa !13
  %63 = getelementptr inbounds [8 x i8], ptr %6, i64 0, i64 0
  %64 = call zeroext i1 %62(ptr noundef null, ptr noundef %63)
  br i1 %64, label %65, label %70

65:                                               ; preds = %56
  %66 = load i32, ptr %8, align 4, !tbaa !9
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %67
  %69 = load ptr, ptr %68, align 8, !tbaa !5
  store ptr %69, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %74

70:                                               ; preds = %56, %47, %44, %24
  br label %71

71:                                               ; preds = %70
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %8, align 4, !tbaa !9
  br label %17, !llvm.loop !20

74:                                               ; preds = %65, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %75 = load i32, ptr %9, align 4
  switch i32 %75, label %77 [
    i32 2, label %76
  ]

76:                                               ; preds = %74
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %77

77:                                               ; preds = %76, %74
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %78 = load ptr, ptr %3, align 8
  ret ptr %78
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @str_tolower(ptr noundef, i64 noundef, ptr noundef) #2

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 = { inlinehint 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 #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 = !{!"_Bool", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"", !6, i64 0, !15, i64 8, !15, i64 64, !6, i64 120, !6, i64 128}
!15 = !{!"", !6, i64 0, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = !{!14, !6, i64 64}
!19 = !{!14, !6, i64 8}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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

@imgio_codecs = external dso_local local_unnamed_addr global [32 x ptr], align 16

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

; Function Attrs: mustprogress 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 ptr @img_codec_detect_ext(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = alloca [8 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = call i64 @str_tolower(ptr noundef nonnull %3, i64 noundef 8, ptr noundef %0)
  %5 = load ptr, ptr @imgio_codecs, align 16, !tbaa !5
  %.not1520 = icmp eq ptr %5, null
  br i1 %.not1520, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %6 = and i32 %1, 2
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %13
  %indvars.iv28 = phi i64 [ %indvars.iv.next29, %13 ], [ 0, %.lr.ph ]
  %7 = phi ptr [ %15, %13 ], [ %5, %.lr.ph ]
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %.not16.us = icmp eq ptr %8, null
  br i1 %.not16.us, label %13, label %.critedge.us

.critedge.us:                                     ; preds = %.lr.ph.split.us
  %9 = getelementptr inbounds i8, ptr %7, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %.not18.us = icmp eq ptr %10, null
  br i1 %.not18.us, label %13, label %11

11:                                               ; preds = %.critedge.us
  %12 = call zeroext i1 %8(ptr noundef null, ptr noundef nonnull %3) #3
  br i1 %12, label %.split.us, label %13

13:                                               ; preds = %11, %.critedge.us, %.lr.ph.split.us
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %14 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %indvars.iv.next29
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %.not15.us = icmp eq ptr %15, null
  br i1 %.not15.us, label %.loopexit, label %.lr.ph.split.us, !llvm.loop !14

.lr.ph.split:                                     ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ %indvars.iv.next, %25 ], [ 0, %.lr.ph ]
  %16 = phi ptr [ %27, %25 ], [ %5, %.lr.ph ]
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %.not16 = icmp eq ptr %17, null
  br i1 %.not16, label %25, label %18

18:                                               ; preds = %.lr.ph.split
  %19 = getelementptr inbounds i8, ptr %16, i64 64
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  %.not17 = icmp eq ptr %20, null
  br i1 %.not17, label %25, label %21

21:                                               ; preds = %18
  %22 = call zeroext i1 %17(ptr noundef null, ptr noundef nonnull %3) #3
  br i1 %22, label %.split.us, label %25

.split.us:                                        ; preds = %21, %11
  %.us-phi = phi i64 [ %indvars.iv28, %11 ], [ %indvars.iv, %21 ]
  %23 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %.us-phi
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  br label %.loopexit

25:                                               ; preds = %18, %.lr.ph.split, %21
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = getelementptr inbounds [32 x ptr], ptr @imgio_codecs, i64 0, i64 %indvars.iv.next
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  %.not15 = icmp eq ptr %27, null
  br i1 %.not15, label %.loopexit, label %.lr.ph.split, !llvm.loop !14

.loopexit:                                        ; preds = %25, %13, %2, %.split.us
  %spec.select = phi ptr [ %24, %.split.us ], [ null, %2 ], [ null, %13 ], [ null, %25 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret ptr %spec.select
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @str_tolower(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { inlinehint 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 #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, !6, i64 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !11, i64 64, !6, i64 120, !6, i64 128}
!11 = !{!"", !6, i64 0, !12, i64 8, !12, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!10, !6, i64 64}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass