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

; 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.StreamClass = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.Stream = type { ptr, ptr, ptr, ptr, ptr, %struct.StreamInt, i64, i32 }
%struct.StreamInt = type { [3 x ptr] }

@stream_class_memory = external dso_local constant %struct.StreamClass, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_open_flags_normalize(i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_ibuffer_set(ptr noalias noundef, i64 noundef, ptr noalias noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @stream_open_begin(ptr noundef, i32 noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stream_open_memory(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !11
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = load i32, ptr %9, align 4, !tbaa !11
  call void @stream_open_begin(ptr noundef %13, i32 noundef %14)
  %15 = load i32, ptr %9, align 4, !tbaa !11
  %16 = call i32 @stream_open_flags_normalize(i32 noundef %15)
  store i32 %16, ptr %9, align 4, !tbaa !11
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %4
  store i32 -257, ptr %5, align 4
  br label %98

20:                                               ; preds = %4
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %38, label %23

23:                                               ; preds = %20
  br label %24

24:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = add i64 %26, 1
  %28 = call i32 @stream_ibuffer_set(ptr noundef %25, i64 noundef %27, ptr noundef null)
  store i32 %28, ptr %10, align 4, !tbaa !11
  %29 = load i32, ptr %10, align 4, !tbaa !11
  %30 = icmp slt i32 %29, 0
  br i1 %30, label %31, label %33

31:                                               ; preds = %24
  %32 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %32, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %34

33:                                               ; preds = %24
  store i32 0, ptr %11, align 4
  br label %34

34:                                               ; preds = %33, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %35 = load i32, ptr %11, align 4
  switch i32 %35, label %100 [
    i32 0, label %36
    i32 1, label %98
  ]

36:                                               ; preds = %34
  br label %37

37:                                               ; preds = %36
  br label %92

38:                                               ; preds = %20
  %39 = load i32, ptr %9, align 4, !tbaa !11
  %40 = and i32 %39, 32
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %76

42:                                               ; preds = %38
  %43 = load i32, ptr %9, align 4, !tbaa !11
  %44 = and i32 %43, -33
  store i32 %44, ptr %9, align 4, !tbaa !11
  br label %45

45:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = add i64 %47, 1
  %49 = call i32 @stream_ibuffer_set(ptr noundef %46, i64 noundef %48, ptr noundef null)
  store i32 %49, ptr %12, align 4, !tbaa !11
  %50 = load i32, ptr %12, align 4, !tbaa !11
  %51 = icmp slt i32 %50, 0
  br i1 %51, label %52, label %54

52:                                               ; preds = %45
  %53 = load i32, ptr %12, align 4, !tbaa !11
  store i32 %53, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %55

54:                                               ; preds = %45
  store i32 0, ptr %11, align 4
  br label %55

55:                                               ; preds = %54, %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %56 = load i32, ptr %11, align 4
  switch i32 %56, label %100 [
    i32 0, label %57
    i32 1, label %98
  ]

57:                                               ; preds = %55
  br label %58

58:                                               ; preds = %57
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.Stream, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %61, ptr align 1 %62, i64 %63, i1 false)
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.Stream, ptr %64, i32 0, i32 2
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Stream, ptr %67, i32 0, i32 0
  store ptr %66, ptr %68, align 8, !tbaa !16
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.Stream, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !16
  %72 = load i64, ptr %8, align 8, !tbaa !9
  %73 = getelementptr inbounds i8, ptr %71, i64 %72
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.Stream, ptr %74, i32 0, i32 1
  store ptr %73, ptr %75, align 8, !tbaa !17
  br label %91

76:                                               ; preds = %38
  %77 = load ptr, ptr %7, align 8, !tbaa !5
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Stream, ptr %78, i32 0, i32 2
  store ptr %77, ptr %79, align 8, !tbaa !13
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Stream, ptr %80, i32 0, i32 0
  store ptr %77, ptr %81, align 8, !tbaa !16
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.Stream, ptr %82, i32 0, i32 2
  %84 = load ptr, ptr %83, align 8, !tbaa !13
  %85 = load i64, ptr %8, align 8, !tbaa !9
  %86 = getelementptr inbounds i8, ptr %84, i64 %85
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.Stream, ptr %87, i32 0, i32 3
  store ptr %86, ptr %88, align 8, !tbaa !18
  %89 = load ptr, ptr %6, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.Stream, ptr %89, i32 0, i32 1
  store ptr %86, ptr %90, align 8, !tbaa !17
  br label %91

91:                                               ; preds = %76, %58
  br label %92

92:                                               ; preds = %91, %37
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.Stream, ptr %93, i32 0, i32 4
  store ptr @stream_class_memory, ptr %94, align 8, !tbaa !19
  %95 = load ptr, ptr %6, align 8, !tbaa !5
  %96 = load i32, ptr %9, align 4, !tbaa !11
  %97 = call i32 @stream_open_end(ptr noundef %95, i32 noundef %96)
  store i32 %97, ptr %5, align 4
  br label %98

98:                                               ; preds = %92, %55, %34, %19
  %99 = load i32, ptr %5, align 4
  ret i32 %99

100:                                              ; preds = %55, %34
  unreachable
}

; 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) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"Stream", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !15, i64 40, !10, i64 64, !12, i64 72}
!15 = !{!"StreamInt", !7, i64 0}
!16 = !{!14, !6, i64 0}
!17 = !{!14, !6, i64 8}
!18 = !{!14, !6, i64 24}
!19 = !{!14, !6, i64 32}


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

%struct.StreamClass = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@stream_class_memory = external dso_local constant %struct.StreamClass, align 8

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_ibuffer_set(ptr noalias noundef, i64 noundef, ptr noalias noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stream_open_memory(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  tail call void @stream_open_begin(ptr noundef %0, i32 noundef %3)
  %5 = tail call i32 @stream_open_flags_normalize(i32 noundef %3)
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %31, label %6

6:                                                ; preds = %4
  %.not39 = icmp eq ptr %1, null
  br i1 %.not39, label %7, label %11

7:                                                ; preds = %6
  %8 = add i64 %2, 1
  %9 = tail call i32 @stream_ibuffer_set(ptr noundef %0, i64 noundef %8, ptr noundef null)
  %10 = icmp sgt i32 %9, -1
  br i1 %10, label %28, label %31

11:                                               ; preds = %6
  %12 = and i32 %5, 32
  %.not40 = icmp eq i32 %12, 0
  br i1 %.not40, label %23, label %13

13:                                               ; preds = %11
  %14 = add i64 %2, 1
  %15 = tail call i32 @stream_ibuffer_set(ptr noundef %0, i64 noundef %14, ptr noundef null)
  %16 = icmp sgt i32 %15, -1
  br i1 %16, label %17, label %31

17:                                               ; preds = %13
  %18 = and i32 %5, -33
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %20, ptr nonnull align 1 %1, i64 %2, i1 false)
  %21 = load ptr, ptr %19, align 8, !tbaa !5
  store ptr %21, ptr %0, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %21, i64 %2
  br label %.sink.split

23:                                               ; preds = %11
  %24 = getelementptr inbounds i8, ptr %0, i64 16
  store ptr %1, ptr %24, align 8, !tbaa !5
  store ptr %1, ptr %0, align 8, !tbaa !13
  %25 = getelementptr inbounds i8, ptr %1, i64 %2
  %26 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr %25, ptr %26, align 8, !tbaa !14
  br label %.sink.split

.sink.split:                                      ; preds = %23, %17
  %.sink = phi ptr [ %22, %17 ], [ %25, %23 ]
  %.037.ph = phi i32 [ %18, %17 ], [ %5, %23 ]
  %27 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %.sink, ptr %27, align 8, !tbaa !15
  br label %28

28:                                               ; preds = %.sink.split, %7
  %.037 = phi i32 [ %5, %7 ], [ %.037.ph, %.sink.split ]
  %29 = getelementptr inbounds i8, ptr %0, i64 32
  store ptr @stream_class_memory, ptr %29, align 8, !tbaa !16
  %30 = tail call i32 @stream_open_end(ptr noundef %0, i32 noundef %.037)
  br label %31

31:                                               ; preds = %13, %7, %4, %28
  %.2 = phi i32 [ %15, %13 ], [ %30, %28 ], [ %9, %7 ], [ -257, %4 ]
  ret i32 %.2
}

; Function Attrs: mustprogress 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) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 16}
!6 = !{!"Stream", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !11, i64 64, !12, i64 72}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"StreamInt", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !7, i64 0}
!14 = !{!6, !7, i64 24}
!15 = !{!6, !7, i64 8}
!16 = !{!6, !7, i64 32}


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