; 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 }
%struct.StioStream = type { ptr, ptr, ptr, ptr, ptr, i32, i32 }
%struct.StioClass = 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: inlinehint nounwind uwtable
declare hidden i32 @stio__ctx_new(ptr noundef, ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stio_read(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.StioItem, align 8
  %9 = alloca %struct.StioItem, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = and i32 %14, 2
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %41, label %17

17:                                               ; preds = %3
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = and i32 %18, 1
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %26

21:                                               ; preds = %17
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %8, i8 0, i64 24, i1 false)
  %23 = getelementptr inbounds %struct.StioItem, ptr %8, i32 0, i32 0
  %24 = getelementptr inbounds %struct.StioItem, ptr %8, i32 0, i32 1
  %25 = getelementptr inbounds %struct.StioItem, ptr %8, i32 0, i32 2
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %22, ptr align 8 %8, i64 24, i1 false), !tbaa.struct !11
  br label %40

26:                                               ; preds = %17
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.StioItem, ptr %9, i32 0, i32 0
  store i32 0, ptr %28, align 8, !tbaa !15
  %29 = getelementptr inbounds %struct.StioItem, ptr %9, i32 0, i32 1
  store i32 0, ptr %29, align 4, !tbaa !18
  %30 = getelementptr inbounds %struct.StioItem, ptr %9, i32 0, i32 2
  %31 = getelementptr inbounds %struct.Any, ptr %30, i32 0, i32 0
  store i16 15, ptr %31, align 8, !tbaa !19
  %32 = getelementptr inbounds %struct.Any, ptr %30, i32 0, i32 1
  store i16 0, ptr %32, align 2, !tbaa !20
  %33 = getelementptr inbounds %struct.Any, ptr %30, i32 0, i32 2
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = call i64 @stio__buf_sz(ptr noundef %34)
  %36 = trunc i64 %35 to i32
  store i32 %36, ptr %33, align 4, !tbaa !21
  %37 = getelementptr inbounds %struct.Any, ptr %30, i32 0, i32 3
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = call ptr @stio__buf_ptr(ptr noundef %38)
  store ptr %39, ptr %37, align 8, !tbaa !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %27, ptr align 8 %9, i64 24, i1 false), !tbaa.struct !11
  br label %40

40:                                               ; preds = %26, %21
  br label %41

41:                                               ; preds = %40, %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.StioStream, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !22
  %45 = getelementptr inbounds %struct.StioClass, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !24
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.StioStream, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !26
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = call i32 %46(ptr noundef %47, ptr noundef %50, ptr noundef %51)
  store i32 %52, ptr %10, align 4, !tbaa !9
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = icmp eq i32 %53, 1
  br i1 %54, label %55, label %70

55:                                               ; preds = %41
  br label %56

56:                                               ; preds = %55
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = call i32 @stio__ctx_new(ptr noundef %57, ptr noundef %58)
  store i32 %59, ptr %11, align 4, !tbaa !9
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = icmp slt i32 %60, 0
  br i1 %61, label %62, label %64

62:                                               ; preds = %56
  %63 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %63, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %65

64:                                               ; preds = %56
  store i32 0, ptr %12, align 4
  br label %65

65:                                               ; preds = %64, %62
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  %66 = load i32, ptr %12, align 4
  switch i32 %66, label %90 [
    i32 0, label %67
  ]

67:                                               ; preds = %65
  br label %68

68:                                               ; preds = %67
  br label %69

69:                                               ; preds = %68
  br label %88

70:                                               ; preds = %41
  %71 = load i32, ptr %10, align 4, !tbaa !9
  %72 = icmp eq i32 %71, 0
  br i1 %72, label %73, label %87

73:                                               ; preds = %70
  br label %74

74:                                               ; preds = %73
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = call i32 @stio__ctx_delete(ptr noundef %75)
  store i32 %76, ptr %13, align 4, !tbaa !9
  %77 = load i32, ptr %13, align 4, !tbaa !9
  %78 = icmp slt i32 %77, 0
  br i1 %78, label %79, label %81

79:                                               ; preds = %74
  %80 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %80, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %82

81:                                               ; preds = %74
  store i32 0, ptr %12, align 4
  br label %82

82:                                               ; preds = %81, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  %83 = load i32, ptr %12, align 4
  switch i32 %83, label %90 [
    i32 0, label %84
  ]

84:                                               ; preds = %82
  br label %85

85:                                               ; preds = %84
  br label %86

86:                                               ; preds = %85
  br label %87

87:                                               ; preds = %86, %70
  br label %88

88:                                               ; preds = %87, %69
  %89 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %89, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %90

90:                                               ; preds = %88, %82, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %91 = load i32, ptr %4, align 4
  ret i32 %91
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @stio__buf_sz(ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @stio__buf_ptr(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{i64 0, i64 4, !9, i64 4, i64 4, !9, i64 8, i64 2, !12, i64 10, i64 2, !12, i64 12, i64 4, !9, i64 16, i64 8, !14}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !10, i64 0}
!16 = !{!"StioItem", !10, i64 0, !10, i64 4, !17, i64 8}
!17 = !{!"Any", !13, i64 0, !13, i64 2, !10, i64 4, !7, i64 8}
!18 = !{!16, !10, i64 4}
!19 = !{!17, !13, i64 0}
!20 = !{!17, !13, i64 2}
!21 = !{!17, !10, i64 4}
!22 = !{!23, !6, i64 0}
!23 = !{!"StioStream", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !10, i64 44}
!24 = !{!25, !6, i64 0}
!25 = !{!"StioClass", !6, i64 0, !6, i64 8, !6, i64 16}
!26 = !{!23, !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"

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stio_read(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %4 = and i32 %2, 2
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %5, label %12

5:                                                ; preds = %3
  %6 = and i32 %2, 1
  %.not25 = icmp eq i32 %6, 0
  br i1 %.not25, label %8, label %7

7:                                                ; preds = %5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %1, i8 0, i64 24, i1 false)
  br label %12

8:                                                ; preds = %5
  %9 = tail call i64 @stio__buf_sz(ptr noundef %0)
  %10 = trunc i64 %9 to i32
  %11 = tail call ptr @stio__buf_ptr(ptr noundef %0)
  store i32 0, ptr %1, align 8, !tbaa !5
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %1, i64 4
  store i32 0, ptr %.sroa.2.0..sroa_idx, align 4, !tbaa !5
  %.sroa.3.0..sroa_idx = getelementptr inbounds i8, ptr %1, i64 8
  store i16 15, ptr %.sroa.3.0..sroa_idx, align 8, !tbaa !9
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %1, i64 10
  store i16 0, ptr %.sroa.4.0..sroa_idx, align 2, !tbaa !9
  %.sroa.5.0..sroa_idx = getelementptr inbounds i8, ptr %1, i64 12
  store i32 %10, ptr %.sroa.5.0..sroa_idx, align 4, !tbaa !5
  %.sroa.6.0..sroa_idx = getelementptr inbounds i8, ptr %1, i64 16
  store ptr %11, ptr %.sroa.6.0..sroa_idx, align 8, !tbaa !11
  br label %12

12:                                               ; preds = %7, %8, %3
  %13 = load ptr, ptr %0, align 8, !tbaa !12
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !17
  %17 = tail call i32 %14(ptr noundef nonnull %0, ptr noundef %16, ptr noundef %1) #3
  switch i32 %17, label %24 [
    i32 1, label %18
    i32 0, label %21
  ]

18:                                               ; preds = %12
  %19 = tail call i32 @stio__ctx_new(ptr noundef nonnull %0, ptr noundef %1)
  %20 = icmp sgt i32 %19, -1
  br i1 %20, label %24, label %25

21:                                               ; preds = %12
  %22 = tail call i32 @stio__ctx_delete(ptr noundef nonnull %0)
  %23 = icmp sgt i32 %22, -1
  br i1 %23, label %24, label %25

24:                                               ; preds = %12, %21, %18
  br label %25

25:                                               ; preds = %18, %21, %24
  %.2 = phi i32 [ %17, %24 ], [ %19, %18 ], [ %22, %21 ]
  ret i32 %.2
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @stio__buf_sz(ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @stio__buf_ptr(ptr noundef) local_unnamed_addr #0

attributes #0 = { 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 #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"StioStream", !14, i64 0, !14, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !6, i64 40, !6, i64 44}
!14 = !{!"any pointer", !7, i64 0}
!15 = !{!16, !14, i64 0}
!16 = !{!"StioClass", !14, i64 0, !14, i64 8, !14, i64 16}
!17 = !{!13, !14, i64 16}


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