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

@g_allocator = external global ptr, align 8
@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [74 x i8], align 1
@__PRETTY_FUNCTION__.imgio_open_inner = 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @imgio_stream_alloc(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @imgio_open_inner(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.ImageIO, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  br label %14

13:                                               ; preds = %1
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 136, ptr noundef @__PRETTY_FUNCTION__.imgio_open_inner) #4
  unreachable

14:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.ImageIO, ptr %15, i32 0, i32 4
  %17 = load i32, ptr %16, align 8, !tbaa !12
  %18 = and i32 %17, 2
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %25

20:                                               ; preds = %14
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.ImageIO, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.ImageCodec, ptr %23, i32 0, i32 2
  br label %30

25:                                               ; preds = %14
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.ImageIO, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.ImageCodec, ptr %28, i32 0, i32 1
  br label %30

30:                                               ; preds = %25, %20
  %31 = phi ptr [ %24, %20 ], [ %29, %25 ]
  store ptr %31, ptr %4, align 8, !tbaa !5
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ImageIO, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  %35 = icmp ne ptr %34, null
  br i1 %35, label %36, label %43

36:                                               ; preds = %30
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ImageCodecSub, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 8, !tbaa !14
  %40 = and i32 %39, 1
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %43

42:                                               ; preds = %36
  br label %80

43:                                               ; preds = %36, %30
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.ImageIO, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  %47 = icmp ne ptr %46, null
  br i1 %47, label %48, label %55

48:                                               ; preds = %43
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ImageCodecSub, ptr %49, i32 0, i32 1
  %51 = load i32, ptr %50, align 8, !tbaa !14
  %52 = and i32 %51, 2
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %55

54:                                               ; preds = %48
  br label %79

55:                                               ; preds = %48, %43
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.ImageIO, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !16
  %59 = icmp ne ptr %58, null
  br i1 %59, label %60, label %77

60:                                               ; preds = %55
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.ImageCodecSub, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %62, align 8, !tbaa !14
  %64 = and i32 %63, 1
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %77

66:                                               ; preds = %60
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = call i32 @imgio_stream_alloc(ptr noundef %67)
  store i32 %68, ptr %5, align 4, !tbaa !17
  %69 = load i32, ptr %5, align 4, !tbaa !17
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %71, label %73

71:                                               ; preds = %66
  %72 = load i32, ptr %5, align 4, !tbaa !17
  store i32 %72, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %74

73:                                               ; preds = %66
  store i32 0, ptr %6, align 4
  br label %74

74:                                               ; preds = %73, %71
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  %75 = load i32, ptr %6, align 4
  switch i32 %75, label %124 [
    i32 0, label %76
  ]

76:                                               ; preds = %74
  br label %78

77:                                               ; preds = %60, %55
  store i32 -778, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %124

78:                                               ; preds = %76
  br label %79

79:                                               ; preds = %78, %54
  br label %80

80:                                               ; preds = %79, %42
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.ImageCodecSub, ptr %81, i32 0, i32 2
  %83 = load i32, ptr %82, align 4, !tbaa !18
  %84 = icmp ne i32 %83, 0
  br i1 %84, label %85, label %101

85:                                               ; preds = %80
  %86 = load ptr, ptr @g_allocator, align 8, !tbaa !5
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.ImageIO, ptr %87, i32 0, i32 3
  %89 = load ptr, ptr %88, align 8, !tbaa !19
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.ImageCodecSub, ptr %90, i32 0, i32 2
  %92 = load i32, ptr %91, align 4, !tbaa !18
  %93 = zext i32 %92 to i64
  %94 = call noalias ptr @alloc_realloc(ptr noundef %86, ptr noundef %89, i64 noundef %93) #6
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.ImageIO, ptr %95, i32 0, i32 3
  store ptr %94, ptr %96, align 8, !tbaa !19
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.ImageIO, ptr %97, i32 0, i32 5
  %99 = load i32, ptr %98, align 4, !tbaa !20
  %100 = or i32 %99, 2
  store i32 %100, ptr %98, align 4, !tbaa !20
  br label %101

101:                                              ; preds = %85, %80
  %102 = load ptr, ptr %4, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.ImageCodecSub, ptr %102, i32 0, i32 3
  %104 = load ptr, ptr %103, align 8, !tbaa !21
  %105 = icmp ne ptr %104, null
  br i1 %105, label %106, label %123

106:                                              ; preds = %101
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %107 = load ptr, ptr %4, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.ImageCodecSub, ptr %107, i32 0, i32 3
  %109 = load ptr, ptr %108, align 8, !tbaa !21
  %110 = load ptr, ptr %3, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.ImageIO, ptr %110, i32 0, i32 3
  %112 = load ptr, ptr %111, align 8, !tbaa !19
  %113 = load ptr, ptr %3, align 8, !tbaa !5
  %114 = call i32 %109(ptr noundef %112, ptr noundef %113)
  store i32 %114, ptr %7, align 4, !tbaa !17
  %115 = load i32, ptr %7, align 4, !tbaa !17
  %116 = icmp ne i32 %115, 0
  br i1 %116, label %117, label %119

117:                                              ; preds = %106
  %118 = load i32, ptr %7, align 4, !tbaa !17
  store i32 %118, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %120

119:                                              ; preds = %106
  store i32 0, ptr %6, align 4
  br label %120

120:                                              ; preds = %119, %117
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  %121 = load i32, ptr %6, align 4
  switch i32 %121, label %124 [
    i32 0, label %122
  ]

122:                                              ; preds = %120
  br label %123

123:                                              ; preds = %122, %101
  store i32 0, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %124

124:                                              ; preds = %123, %120, %77, %74
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %125 = load i32, ptr %2, align 4
  ret i32 %125
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind allocsize(2) uwtable
declare hidden noalias ptr @alloc_realloc(ptr noundef, ptr noundef, i64 noundef) #3

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 = { noreturn nounwind "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 = { inlinehint nounwind allocsize(2) 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 #4 = { noreturn nounwind }
attributes #5 = { nounwind }
attributes #6 = { allocsize(2) }

!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 = !{!"ImageIO", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !11, i64 36}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 32}
!13 = !{!10, !6, i64 8}
!14 = !{!15, !11, i64 8}
!15 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!16 = !{!10, !6, i64 16}
!17 = !{!11, !11, i64 0}
!18 = !{!15, !11, i64 12}
!19 = !{!10, !6, i64 24}
!20 = !{!10, !11, i64 36}
!21 = !{!15, !6, i64 16}


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

@g_allocator = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [11 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [74 x i8], align 1
@__PRETTY_FUNCTION__.imgio_open_inner = external hidden unnamed_addr constant [32 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @imgio_stream_alloc(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @imgio_open_inner(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %3, label %4

3:                                                ; preds = %1
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 136, ptr noundef nonnull @__PRETTY_FUNCTION__.imgio_open_inner) #3
  unreachable

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %7 = and i32 %6, 2
  %.not32 = icmp eq i32 %7, 0
  %.v = select i1 %.not32, i64 8, i64 64
  %8 = getelementptr inbounds i8, ptr %2, i64 %.v
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %.not33 = icmp eq ptr %10, null
  br i1 %.not33, label %15, label %11

11:                                               ; preds = %4
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %14 = and i32 %13, 1
  %.not34 = icmp eq i32 %14, 0
  br i1 %.not34, label %15, label %26

15:                                               ; preds = %11, %4
  %16 = getelementptr inbounds i8, ptr %0, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %.not35 = icmp eq ptr %17, null
  br i1 %.not35, label %.thread, label %18

18:                                               ; preds = %15
  %19 = getelementptr inbounds i8, ptr %8, i64 8
  %20 = load i32, ptr %19, align 8, !tbaa !13
  %21 = and i32 %20, 2
  %.not36 = icmp eq i32 %21, 0
  br i1 %.not36, label %22, label %26

22:                                               ; preds = %18
  %23 = and i32 %20, 1
  %.not38 = icmp eq i32 %23, 0
  br i1 %.not38, label %.thread, label %24

24:                                               ; preds = %22
  %25 = tail call i32 @imgio_stream_alloc(ptr noundef nonnull %0)
  %.not39 = icmp eq i32 %25, 0
  br i1 %.not39, label %26, label %.thread

26:                                               ; preds = %18, %24, %11
  %27 = getelementptr inbounds i8, ptr %8, i64 12
  %28 = load i32, ptr %27, align 4, !tbaa !16
  %.not40 = icmp eq i32 %28, 0
  br i1 %.not40, label %38, label %29

29:                                               ; preds = %26
  %30 = load ptr, ptr @g_allocator, align 8, !tbaa !17
  %31 = getelementptr inbounds i8, ptr %0, i64 24
  %32 = load ptr, ptr %31, align 8, !tbaa !18
  %33 = zext i32 %28 to i64
  %34 = tail call noalias ptr @alloc_realloc(ptr noundef %30, ptr noundef %32, i64 noundef %33) #4
  store ptr %34, ptr %31, align 8, !tbaa !18
  %35 = getelementptr inbounds i8, ptr %0, i64 36
  %36 = load i32, ptr %35, align 4, !tbaa !19
  %37 = or i32 %36, 2
  store i32 %37, ptr %35, align 4, !tbaa !19
  br label %38

38:                                               ; preds = %29, %26
  %39 = getelementptr inbounds i8, ptr %8, i64 16
  %40 = load ptr, ptr %39, align 8, !tbaa !20
  %.not41 = icmp eq ptr %40, null
  br i1 %.not41, label %45, label %41

41:                                               ; preds = %38
  %42 = getelementptr inbounds i8, ptr %0, i64 24
  %43 = load ptr, ptr %42, align 8, !tbaa !18
  %44 = tail call i32 %40(ptr noundef %43, ptr noundef nonnull %0) #5
  %.not42 = icmp eq i32 %44, 0
  br i1 %.not42, label %45, label %.thread

45:                                               ; preds = %41, %38
  br label %.thread

.thread:                                          ; preds = %15, %22, %41, %24, %45
  %.3 = phi i32 [ 0, %45 ], [ %44, %41 ], [ %25, %24 ], [ -778, %22 ], [ -778, %15 ]
  ret i32 %.3
}

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind allocsize(2) uwtable
declare hidden noalias ptr @alloc_realloc(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #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 = { noreturn nounwind "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 allocsize(2) 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 = { noreturn nounwind }
attributes #4 = { allocsize(2) }
attributes #5 = { 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, !7, i64 0}
!6 = !{!"ImageIO", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 36}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 32}
!12 = !{!6, !7, i64 8}
!13 = !{!14, !10, i64 8}
!14 = !{!"", !7, i64 0, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!15 = !{!6, !7, i64 16}
!16 = !{!14, !10, i64 12}
!17 = !{!7, !7, i64 0}
!18 = !{!6, !7, i64 24}
!19 = !{!6, !10, i64 36}
!20 = !{!14, !7, i64 16}


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