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

; 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.StioItem = type { i32, i32, %struct.Any }
%struct.Any = type { i16, i16, i32, %union.AnyPayload }
%union.AnyPayload = type { i64 }

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

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

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

27:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = call i64 @anyb_size(i32 noundef %28)
  %30 = trunc i64 %29 to i32
  store i32 %30, ptr %14, align 4, !tbaa !9
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.StioItem, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %32, align 4, !tbaa !15
  %34 = icmp ne i32 %33, -1
  br i1 %34, label %35, label %47

35:                                               ; preds = %27
  %36 = load ptr, ptr %11, align 8, !tbaa !5
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.StioItem, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !15
  %42 = load i32, ptr %14, align 4, !tbaa !9
  %43 = mul i32 %41, %42
  %44 = zext i32 %43 to i64
  %45 = call noalias ptr @alloc_realloc(ptr noundef %36, ptr noundef %38, i64 noundef %44) #6
  %46 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %45, ptr %46, align 8, !tbaa !5
  br label %56

47:                                               ; preds = %27
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = load ptr, ptr %48, align 8, !tbaa !5
  %50 = icmp ne ptr %49, null
  br i1 %50, label %55, label %51

51:                                               ; preds = %47
  %52 = load ptr, ptr %11, align 8, !tbaa !5
  %53 = call noalias ptr @alloc_alloc(ptr noundef %52, i64 noundef 1024) #7
  %54 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %53, ptr %54, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %51, %47
  br label %56

56:                                               ; preds = %55, %35
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %57 = load ptr, ptr %11, align 8, !tbaa !5
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = load ptr, ptr %58, align 8, !tbaa !5
  %60 = call i64 @alloc_size(ptr noundef %57, ptr noundef %59)
  %61 = load i32, ptr %14, align 4, !tbaa !9
  %62 = zext i32 %61 to i64
  %63 = udiv i64 %60, %62
  %64 = trunc i64 %63 to i32
  store i32 %64, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  store i32 0, ptr %16, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %101, %56
  br label %66

66:                                               ; preds = %65
  br label %67

67:                                               ; preds = %66
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #5
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = load i32, ptr %9, align 4, !tbaa !9
  %71 = load i32, ptr %15, align 4, !tbaa !9
  %72 = load i32, ptr %16, align 4, !tbaa !9
  %73 = sub i32 %71, %72
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  %75 = load ptr, ptr %74, align 8, !tbaa !5
  %76 = load i32, ptr %14, align 4, !tbaa !9
  %77 = load i32, ptr %16, align 4, !tbaa !9
  %78 = mul i32 %76, %77
  %79 = zext i32 %78 to i64
  %80 = getelementptr inbounds i8, ptr %75, i64 %79
  %81 = call i32 @stio_read_chunk_buf(ptr noundef %68, ptr noundef %69, i32 noundef %70, i32 noundef %73, ptr noundef %80)
  store i32 %81, ptr %12, align 4, !tbaa !9
  store i32 %81, ptr %17, align 4, !tbaa !9
  %82 = load i32, ptr %17, align 4, !tbaa !9
  %83 = icmp slt i32 %82, 0
  br i1 %83, label %84, label %86

84:                                               ; preds = %67
  %85 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %85, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %87

86:                                               ; preds = %67
  store i32 0, ptr %18, align 4
  br label %87

87:                                               ; preds = %86, %84
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #5
  %88 = load i32, ptr %18, align 4
  switch i32 %88, label %126 [
    i32 0, label %89
  ]

89:                                               ; preds = %87
  br label %90

90:                                               ; preds = %89
  br label %91

91:                                               ; preds = %90
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.StioItem, ptr %92, i32 0, i32 2
  %94 = getelementptr inbounds %struct.Any, ptr %93, i32 0, i32 2
  %95 = load i32, ptr %94, align 4, !tbaa !16
  %96 = load i32, ptr %16, align 4, !tbaa !9
  %97 = add i32 %96, %95
  store i32 %97, ptr %16, align 4, !tbaa !9
  %98 = load i32, ptr %12, align 4, !tbaa !9
  %99 = icmp eq i32 %98, 0
  br i1 %99, label %100, label %101

100:                                              ; preds = %91
  br label %113

101:                                              ; preds = %91
  %102 = load i32, ptr %15, align 4, !tbaa !9
  %103 = mul i32 %102, 2
  store i32 %103, ptr %15, align 4, !tbaa !9
  %104 = load ptr, ptr %11, align 8, !tbaa !5
  %105 = load ptr, ptr %10, align 8, !tbaa !5
  %106 = load ptr, ptr %105, align 8, !tbaa !5
  %107 = load i32, ptr %14, align 4, !tbaa !9
  %108 = load i32, ptr %15, align 4, !tbaa !9
  %109 = mul i32 %107, %108
  %110 = zext i32 %109 to i64
  %111 = call noalias ptr @alloc_realloc(ptr noundef %104, ptr noundef %106, i64 noundef %110) #6
  %112 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %111, ptr %112, align 8, !tbaa !5
  br label %65

113:                                              ; preds = %100
  store i32 2, ptr %12, align 4, !tbaa !9
  %114 = load i32, ptr %13, align 4, !tbaa !9
  %115 = load ptr, ptr %8, align 8, !tbaa !5
  %116 = getelementptr inbounds %struct.StioItem, ptr %115, i32 0, i32 0
  store i32 %114, ptr %116, align 8, !tbaa !11
  %117 = load i32, ptr %16, align 4, !tbaa !9
  %118 = load ptr, ptr %8, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.StioItem, ptr %118, i32 0, i32 2
  %120 = getelementptr inbounds %struct.Any, ptr %119, i32 0, i32 2
  store i32 %117, ptr %120, align 4, !tbaa !16
  %121 = load ptr, ptr %10, align 8, !tbaa !5
  %122 = load ptr, ptr %121, align 8, !tbaa !5
  %123 = load ptr, ptr %8, align 8, !tbaa !5
  %124 = getelementptr inbounds %struct.StioItem, ptr %123, i32 0, i32 2
  %125 = getelementptr inbounds %struct.Any, ptr %124, i32 0, i32 3
  store ptr %122, ptr %125, align 8, !tbaa !17
  store i32 0, ptr %18, align 4
  br label %126

126:                                              ; preds = %113, %87
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  %127 = load i32, ptr %18, align 4
  switch i32 %127, label %131 [
    i32 0, label %128
  ]

128:                                              ; preds = %126
  br label %129

129:                                              ; preds = %128, %5
  %130 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %130, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %131

131:                                              ; preds = %129, %126
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  %132 = load i32, ptr %6, align 4
  ret i32 %132
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @anyb_size(i32 noundef) #2

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

; Function Attrs: inlinehint nounwind allocsize(1) uwtable
declare hidden noalias ptr @alloc_alloc(ptr noundef, i64 noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @alloc_size(ptr 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 = { 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 = { inlinehint nounwind allocsize(1) 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 #5 = { nounwind }
attributes #6 = { allocsize(2) }
attributes #7 = { allocsize(1) }

!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 0}
!12 = !{!"StioItem", !10, i64 0, !10, i64 4, !13, i64 8}
!13 = !{!"Any", !14, i64 0, !14, i64 2, !10, i64 4, !7, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !10, i64 4}
!16 = !{!12, !10, i64 12}
!17 = !{!7, !7, i64 0}


; 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: nounwind uwtable
declare dso_local i32 @stio_read(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stio_read_dyn(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr nocapture noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = tail call i32 @stio_read(ptr noundef %0, ptr noundef %1, i32 noundef 1)
  %7 = load i32, ptr %1, align 8, !tbaa !5
  %8 = icmp eq i32 %6, 1
  br i1 %8, label %9, label %.loopexit

9:                                                ; preds = %5
  %10 = tail call i64 @anyb_size(i32 noundef %2)
  %11 = trunc i64 %10 to i32
  %12 = getelementptr inbounds i8, ptr %1, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !12
  %.not = icmp eq i32 %13, -1
  %14 = load ptr, ptr %3, align 8, !tbaa !13
  br i1 %.not, label %19, label %15

15:                                               ; preds = %9
  %16 = mul i32 %13, %11
  %17 = zext i32 %16 to i64
  %18 = tail call noalias ptr @alloc_realloc(ptr noundef %4, ptr noundef %14, i64 noundef %17) #4
  br label %.sink.split

19:                                               ; preds = %9
  %.not55 = icmp eq ptr %14, null
  br i1 %.not55, label %20, label %22

20:                                               ; preds = %19
  %21 = tail call noalias dereferenceable_or_null(1024) ptr @alloc_alloc(ptr noundef %4, i64 noundef 1024) #5
  br label %.sink.split

.sink.split:                                      ; preds = %15, %20
  %.sink = phi ptr [ %21, %20 ], [ %18, %15 ]
  store ptr %.sink, ptr %3, align 8, !tbaa !13
  br label %22

22:                                               ; preds = %.sink.split, %19
  %23 = phi ptr [ %14, %19 ], [ %.sink, %.sink.split ]
  %24 = tail call i64 @alloc_size(ptr noundef %4, ptr noundef %23)
  %25 = and i64 %10, 4294967295
  %26 = udiv i64 %24, %25
  %27 = trunc i64 %26 to i32
  %28 = load ptr, ptr %3, align 8, !tbaa !13
  %29 = tail call i32 @stio_read_chunk_buf(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %2, i32 noundef %27, ptr noundef %28)
  %30 = icmp sgt i32 %29, -1
  br i1 %30, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %22
  %31 = getelementptr inbounds i8, ptr %1, i64 12
  br label %32

32:                                               ; preds = %.lr.ph, %37
  %33 = phi i32 [ %29, %.lr.ph ], [ %47, %37 ]
  %.04860 = phi i32 [ 0, %.lr.ph ], [ %35, %37 ]
  %.04959 = phi i32 [ %27, %.lr.ph ], [ %38, %37 ]
  %34 = load i32, ptr %31, align 4, !tbaa !15
  %35 = add i32 %34, %.04860
  %36 = icmp eq i32 %33, 0
  br i1 %36, label %49, label %37

37:                                               ; preds = %32
  %38 = shl i32 %.04959, 1
  %39 = load ptr, ptr %3, align 8, !tbaa !13
  %40 = mul i32 %38, %11
  %41 = zext i32 %40 to i64
  %42 = tail call noalias ptr @alloc_realloc(ptr noundef %4, ptr noundef %39, i64 noundef %41) #4
  store ptr %42, ptr %3, align 8, !tbaa !13
  %43 = sub i32 %38, %35
  %44 = mul i32 %35, %11
  %45 = zext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %42, i64 %45
  %47 = tail call i32 @stio_read_chunk_buf(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %2, i32 noundef %43, ptr noundef %46)
  %48 = icmp sgt i32 %47, -1
  br i1 %48, label %32, label %.loopexit

49:                                               ; preds = %32
  store i32 %7, ptr %1, align 8, !tbaa !5
  store i32 %35, ptr %31, align 4, !tbaa !15
  %50 = load ptr, ptr %3, align 8, !tbaa !13
  %51 = getelementptr inbounds i8, ptr %1, i64 16
  store ptr %50, ptr %51, align 8, !tbaa !16
  br label %.loopexit

.loopexit:                                        ; preds = %37, %22, %49, %5
  %.2 = phi i32 [ %6, %5 ], [ 2, %49 ], [ %29, %22 ], [ %47, %37 ]
  ret i32 %.2
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @anyb_size(i32 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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @alloc_size(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 = { 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 #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 = { inlinehint nounwind allocsize(1) 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 = { allocsize(2) }
attributes #5 = { allocsize(1) }

!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 = !{!"StioItem", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"Any", !11, i64 0, !11, i64 2, !7, i64 4, !8, i64 8}
!11 = !{!"short", !8, i64 0}
!12 = !{!6, !7, i64 4}
!13 = !{!14, !14, i64 0}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!6, !7, i64 12}
!16 = !{!8, !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass