; 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.1 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [72 x i8], align 1
@__PRETTY_FUNCTION__.stream_read_buffer_discard = external hidden unnamed_addr constant [41 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [34 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: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

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

12:                                               ; preds = %1
  br label %14

13:                                               ; preds = %1
  call void @__assert_fail(ptr noundef @.str.1, ptr noundef @.str.2, i32 noundef 232, ptr noundef @__PRETTY_FUNCTION__.stream_read_buffer_discard) #3
  unreachable

14:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.Stream, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Stream, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !15
  %21 = ptrtoint ptr %17 to i64
  %22 = ptrtoint ptr %20 to i64
  %23 = sub i64 %21, %22
  store i64 %23, ptr %4, align 8, !tbaa !16
  %24 = load i64, ptr %4, align 8, !tbaa !16
  %25 = icmp sgt i64 %24, 0
  br i1 %25, label %26, label %68

26:                                               ; preds = %14
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.Stream, ptr %27, i32 0, i32 4
  %29 = load ptr, ptr %28, align 8, !tbaa !17
  %30 = getelementptr inbounds %struct.StreamClass, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !18
  %32 = icmp ne ptr %31, null
  br i1 %32, label %34, label %33

33:                                               ; preds = %26
  store i32 -266, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %89

34:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Stream, ptr %35, i32 0, i32 4
  %37 = load ptr, ptr %36, align 8, !tbaa !17
  %38 = getelementptr inbounds %struct.StreamClass, ptr %37, i32 0, i32 3
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.Stream, ptr %40, i32 0, i32 5
  %42 = load i64, ptr %4, align 8, !tbaa !16
  %43 = sub nsw i64 0, %42
  %44 = call i64 %39(ptr noundef %41, i64 noundef %43, i32 noundef 1)
  store i64 %44, ptr %6, align 8, !tbaa !16
  %45 = load i64, ptr %6, align 8, !tbaa !16
  %46 = icmp slt i64 %45, 0
  br i1 %46, label %47, label %53

47:                                               ; preds = %34
  %48 = load i64, ptr %6, align 8, !tbaa !16
  %49 = icmp ne i64 %48, -267
  br i1 %49, label %50, label %53

50:                                               ; preds = %47
  %51 = load i64, ptr %6, align 8, !tbaa !16
  %52 = trunc i64 %51 to i32
  store i32 %52, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %65

53:                                               ; preds = %47, %34
  %54 = load i64, ptr %6, align 8, !tbaa !16
  %55 = icmp eq i64 %54, -267
  br i1 %55, label %62, label %56

56:                                               ; preds = %53
  %57 = load i64, ptr %6, align 8, !tbaa !16
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Stream, ptr %58, i32 0, i32 6
  %60 = load i64, ptr %59, align 8, !tbaa !20
  %61 = icmp eq i64 %57, %60
  br i1 %61, label %62, label %63

62:                                               ; preds = %56, %53
  br label %64

63:                                               ; preds = %56
  call void @__assert_fail(ptr noundef @.str.4, ptr noundef @.str.2, i32 noundef 239, ptr noundef @__PRETTY_FUNCTION__.stream_read_buffer_discard) #3
  unreachable

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

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

67:                                               ; preds = %65
  br label %68

68:                                               ; preds = %67, %14
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.Stream, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !15
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.Stream, ptr %72, i32 0, i32 2
  %74 = load ptr, ptr %73, align 8, !tbaa !21
  %75 = ptrtoint ptr %71 to i64
  %76 = ptrtoint ptr %74 to i64
  %77 = sub i64 %75, %76
  %78 = load ptr, ptr %3, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Stream, ptr %78, i32 0, i32 6
  %80 = load i64, ptr %79, align 8, !tbaa !20
  %81 = add i64 %80, %77
  store i64 %81, ptr %79, align 8, !tbaa !20
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.Stream, ptr %82, i32 0, i32 2
  %84 = load ptr, ptr %83, align 8, !tbaa !21
  %85 = load ptr, ptr %3, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.Stream, ptr %85, i32 0, i32 1
  store ptr %84, ptr %86, align 8, !tbaa !14
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.Stream, ptr %87, i32 0, i32 0
  store ptr %84, ptr %88, align 8, !tbaa !15
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %89

89:                                               ; preds = %68, %65, %33
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %90 = load i32, ptr %2, align 4
  ret i32 %90
}

attributes #0 = { nocallback nofree nosync 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !13, i64 72}
!10 = !{!"Stream", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !12, i64 64, !13, i64 72}
!11 = !{!"StreamInt", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !6, i64 8}
!15 = !{!10, !6, i64 0}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !6, i64 32}
!18 = !{!19, !6, i64 24}
!19 = !{!"StreamClass", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48}
!20 = !{!10, !12, i64 64}
!21 = !{!10, !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"

@.str.1 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [72 x i8], align 1
@__PRETTY_FUNCTION__.stream_read_buffer_discard = external hidden unnamed_addr constant [41 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [34 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @stream_read_buffer_discard(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 72
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = and i32 %3, 1
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %6, label %5

5:                                                ; preds = %1
  tail call void @__assert_fail(ptr noundef nonnull @.str.1, ptr noundef nonnull @.str.2, i32 noundef 232, ptr noundef nonnull @__PRETTY_FUNCTION__.stream_read_buffer_discard) #2
  unreachable

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %9 = load ptr, ptr %0, align 8, !tbaa !14
  %10 = ptrtoint ptr %8 to i64
  %11 = ptrtoint ptr %9 to i64
  %12 = sub i64 %10, %11
  %13 = icmp sgt i64 %12, 0
  br i1 %13, label %14, label %.thread

14:                                               ; preds = %6
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %17 = getelementptr inbounds i8, ptr %16, i64 24
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %.not26 = icmp eq ptr %18, null
  br i1 %.not26, label %42, label %19

19:                                               ; preds = %14
  %20 = getelementptr inbounds i8, ptr %0, i64 40
  %21 = sub nsw i64 0, %12
  %22 = tail call i64 %18(ptr noundef nonnull %20, i64 noundef %21, i32 noundef 1) #3
  %23 = icmp sgt i64 %22, -1
  %24 = icmp eq i64 %22, -267
  %or.cond.not = or i1 %23, %24
  br i1 %or.cond.not, label %25, label %31

25:                                               ; preds = %19
  br i1 %24, label %.thread, label %26

26:                                               ; preds = %25
  %27 = getelementptr inbounds i8, ptr %0, i64 64
  %28 = load i64, ptr %27, align 8, !tbaa !18
  %29 = icmp eq i64 %22, %28
  br i1 %29, label %.thread, label %30

30:                                               ; preds = %26
  tail call void @__assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.2, i32 noundef 239, ptr noundef nonnull @__PRETTY_FUNCTION__.stream_read_buffer_discard) #2
  unreachable

31:                                               ; preds = %19
  %32 = trunc i64 %22 to i32
  br label %42

.thread:                                          ; preds = %25, %26, %6
  %33 = load ptr, ptr %0, align 8, !tbaa !14
  %34 = getelementptr inbounds i8, ptr %0, i64 16
  %35 = load ptr, ptr %34, align 8, !tbaa !19
  %36 = ptrtoint ptr %33 to i64
  %37 = ptrtoint ptr %35 to i64
  %38 = sub i64 %36, %37
  %39 = getelementptr inbounds i8, ptr %0, i64 64
  %40 = load i64, ptr %39, align 8, !tbaa !18
  %41 = add i64 %38, %40
  store i64 %41, ptr %39, align 8, !tbaa !18
  store ptr %35, ptr %7, align 8, !tbaa !13
  store ptr %35, ptr %0, align 8, !tbaa !14
  br label %42

42:                                               ; preds = %31, %14, %.thread
  %.1 = phi i32 [ 0, %.thread ], [ %32, %31 ], [ -266, %14 ]
  ret i32 %.1
}

attributes #0 = { 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 #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 }
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, !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 8}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !7, i64 32}
!16 = !{!17, !7, i64 24}
!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}
!19 = !{!6, !7, i64 16}


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