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

@.str.2 = external hidden unnamed_addr constant [72 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [17 x i8], align 1
@__PRETTY_FUNCTION__.stream_write_i = external hidden unnamed_addr constant [68 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: 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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_write_buffer_flush(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_write_prep(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i64 @stream_write_i(ptr noalias noundef %0, i64 noundef %1, ptr noalias noundef %2) #3 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = icmp sge i64 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %3
  br label %16

15:                                               ; preds = %3
  call void @__assert_fail(ptr noundef @.str.7, ptr noundef @.str.2, i32 noundef 474, ptr noundef @__PRETTY_FUNCTION__.stream_write_i) #4
  unreachable

16:                                               ; preds = %14
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.Stream, ptr %17, i32 0, i32 7
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = and i32 %19, 1
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %25

22:                                               ; preds = %16
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = icmp ult i64 %23, 2048
  br i1 %24, label %25, label %39

25:                                               ; preds = %22, %16
  br label %26

26:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call i32 @stream_write_prep(ptr noundef %27, i64 noundef 0)
  store i32 %28, ptr %8, align 4, !tbaa !15
  %29 = load i32, ptr %8, align 4, !tbaa !15
  %30 = icmp slt i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %26
  %32 = load i32, ptr %8, align 4, !tbaa !15
  %33 = sext i32 %32 to i64
  store i64 %33, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %35

34:                                               ; preds = %26
  store i32 0, ptr %9, align 4
  br label %35

35:                                               ; preds = %34, %31
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %36 = load i32, ptr %9, align 4
  switch i32 %36, label %107 [
    i32 0, label %37
    i32 1, label %105
  ]

37:                                               ; preds = %35
  br label %38

38:                                               ; preds = %37
  br label %39

39:                                               ; preds = %38, %22
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Stream, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = load i64, ptr %6, align 8, !tbaa !9
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Stream, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %46, align 8, !tbaa !17
  %48 = icmp ule ptr %44, %47
  br i1 %48, label %49, label %61

49:                                               ; preds = %39
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.Stream, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !16
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = load i64, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %52, ptr align 1 %53, i64 %54, i1 false)
  %55 = load i64, ptr %6, align 8, !tbaa !9
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.Stream, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !16
  %59 = getelementptr inbounds i8, ptr %58, i64 %55
  store ptr %59, ptr %57, align 8, !tbaa !16
  %60 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %60, ptr %4, align 8
  br label %105

61:                                               ; preds = %39
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.Stream, ptr %62, i32 0, i32 4
  %64 = load ptr, ptr %63, align 8, !tbaa !18
  %65 = getelementptr inbounds %struct.StreamClass, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !19
  %67 = icmp ne ptr %66, null
  br i1 %67, label %68, label %104

68:                                               ; preds = %61
  br label %69

69:                                               ; preds = %68
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = call i32 @stream_write_buffer_flush(ptr noundef %70)
  store i32 %71, ptr %10, align 4, !tbaa !15
  %72 = load i32, ptr %10, align 4, !tbaa !15
  %73 = icmp slt i32 %72, 0
  br i1 %73, label %74, label %77

74:                                               ; preds = %69
  %75 = load i32, ptr %10, align 4, !tbaa !15
  %76 = sext i32 %75 to i64
  store i64 %76, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %78

77:                                               ; preds = %69
  store i32 0, ptr %9, align 4
  br label %78

78:                                               ; preds = %77, %74
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %79 = load i32, ptr %9, align 4
  switch i32 %79, label %107 [
    i32 0, label %80
    i32 1, label %105
  ]

80:                                               ; preds = %78
  br label %81

81:                                               ; preds = %80
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %82 = load ptr, ptr %5, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.Stream, ptr %82, i32 0, i32 4
  %84 = load ptr, ptr %83, align 8, !tbaa !18
  %85 = getelementptr inbounds %struct.StreamClass, ptr %84, i32 0, i32 1
  %86 = load ptr, ptr %85, align 8, !tbaa !19
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.Stream, ptr %87, i32 0, i32 5
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = load i64, ptr %6, align 8, !tbaa !9
  %91 = call i64 %86(ptr noundef %88, ptr noundef %89, i64 noundef %90)
  store i64 %91, ptr %11, align 8, !tbaa !9
  %92 = load i64, ptr %11, align 8, !tbaa !9
  %93 = icmp slt i64 %92, 0
  br i1 %93, label %94, label %96

94:                                               ; preds = %81
  %95 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %95, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %103

96:                                               ; preds = %81
  %97 = load i64, ptr %11, align 8, !tbaa !9
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.Stream, ptr %98, i32 0, i32 6
  %100 = load i64, ptr %99, align 8, !tbaa !21
  %101 = add i64 %100, %97
  store i64 %101, ptr %99, align 8, !tbaa !21
  %102 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %102, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %103

103:                                              ; preds = %96, %94
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %105

104:                                              ; preds = %61
  store i64 0, ptr %4, align 8
  br label %105

105:                                              ; preds = %104, %103, %78, %49, %35
  %106 = load i64, ptr %4, align 8
  ret i64 %106

107:                                              ; preds = %78, %35
  unreachable
}

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


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

@.str.2 = external hidden unnamed_addr constant [72 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [17 x i8], align 1
@__PRETTY_FUNCTION__.stream_write_i = external hidden unnamed_addr constant [68 x i8], align 1

; 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) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @stream_write_prep(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i64 @stream_write_i(ptr noalias noundef %0, i64 noundef %1, ptr noalias noundef %2) local_unnamed_addr #2 {
  %4 = icmp sgt i64 %1, -1
  br i1 %4, label %6, label %5

5:                                                ; preds = %3
  tail call void @__assert_fail(ptr noundef nonnull @.str.7, ptr noundef nonnull @.str.2, i32 noundef 474, ptr noundef nonnull @__PRETTY_FUNCTION__.stream_write_i) #3
  unreachable

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 72
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = and i32 %8, 1
  %10 = icmp eq i32 %9, 0
  %11 = icmp ult i64 %1, 2048
  %or.cond = or i1 %11, %10
  br i1 %or.cond, label %12, label %16

12:                                               ; preds = %6
  %13 = tail call i32 @stream_write_prep(ptr noundef nonnull %0, i64 noundef 0)
  %14 = icmp sgt i32 %13, -1
  %15 = sext i32 %13 to i64
  br i1 %14, label %16, label %44

16:                                               ; preds = %12, %6
  %17 = load ptr, ptr %0, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %17, i64 %1
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %.not = icmp ugt ptr %18, %20
  br i1 %.not, label %24, label %21

21:                                               ; preds = %16
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %17, ptr align 1 %2, i64 %1, i1 false)
  %22 = load ptr, ptr %0, align 8, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %22, i64 %1
  store ptr %23, ptr %0, align 8, !tbaa !13
  br label %44

24:                                               ; preds = %16
  %25 = getelementptr inbounds i8, ptr %0, i64 32
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !16
  %.not37 = icmp eq ptr %28, null
  br i1 %.not37, label %44, label %29

29:                                               ; preds = %24
  %30 = tail call i32 @stream_write_buffer_flush(ptr noundef nonnull %0)
  %31 = icmp sgt i32 %30, -1
  %32 = sext i32 %30 to i64
  br i1 %31, label %33, label %44

33:                                               ; preds = %29
  %34 = load ptr, ptr %25, align 8, !tbaa !15
  %35 = getelementptr inbounds i8, ptr %34, i64 8
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = getelementptr inbounds i8, ptr %0, i64 40
  %38 = tail call i64 %36(ptr noundef nonnull %37, ptr noundef %2, i64 noundef %1) #4
  %39 = icmp slt i64 %38, 0
  br i1 %39, label %44, label %40

40:                                               ; preds = %33
  %41 = getelementptr inbounds i8, ptr %0, i64 64
  %42 = load i64, ptr %41, align 8, !tbaa !18
  %43 = add i64 %42, %38
  store i64 %43, ptr %41, align 8, !tbaa !18
  br label %44

44:                                               ; preds = %29, %12, %24, %40, %33, %21
  %.4 = phi i64 [ %15, %12 ], [ %1, %21 ], [ %32, %29 ], [ %38, %33 ], [ %38, %40 ], [ 0, %24 ]
  ret i64 %.4
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { noreturn nounwind }
attributes #4 = { 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, !12, i64 72}
!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 8}
!15 = !{!6, !7, i64 32}
!16 = !{!17, !7, i64 8}
!17 = !{!"StreamClass", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48}
!18 = !{!6, !11, i64 64}


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