; LLVM IR from mncoppola/rpef :: utilities_src/squashfs2.1/lzma/C/7zip/Common (StreamUtils)

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

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z11WriteStreamP20ISequentialOutStreamPKvjPj(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, 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 %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  %14 = load ptr, ptr %9, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %18

16:                                               ; preds = %4
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 0, ptr %17, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %16, %4
  br label %19

19:                                               ; preds = %60, %18
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = icmp ne i32 %20, 0
  br i1 %21, label %22, label %61

22:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = load i32, ptr %8, align 4, !tbaa !9
  %26 = load ptr, ptr %23, align 8, !tbaa !11
  %27 = getelementptr inbounds ptr, ptr %26, i64 3
  %28 = load ptr, ptr %27, align 8
  %29 = call noundef i32 %28(ptr noundef nonnull align 8 dereferenceable(8) %23, ptr noundef %24, i32 noundef %25, ptr noundef %10)
  store i32 %29, ptr %11, align 4, !tbaa !9
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %37

32:                                               ; preds = %22
  %33 = load i32, ptr %10, align 4, !tbaa !9
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = add i32 %35, %33
  store i32 %36, ptr %34, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %32, %22
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %38, i64 %40
  store ptr %41, ptr %7, align 8, !tbaa !5
  %42 = load i32, ptr %10, align 4, !tbaa !9
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = sub i32 %43, %42
  store i32 %44, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %45 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %45, ptr %12, align 4, !tbaa !9
  %46 = load i32, ptr %12, align 4, !tbaa !9
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %48, label %50

48:                                               ; preds = %37
  %49 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %49, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %51

50:                                               ; preds = %37
  store i32 0, ptr %13, align 4
  br label %51

51:                                               ; preds = %50, %48
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  %52 = load i32, ptr %13, align 4
  switch i32 %52, label %58 [
    i32 0, label %53
  ]

53:                                               ; preds = %51
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %57

56:                                               ; preds = %53
  store i32 3, ptr %13, align 4
  br label %58

57:                                               ; preds = %53
  store i32 0, ptr %13, align 4
  br label %58

58:                                               ; preds = %57, %56, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  %59 = load i32, ptr %13, align 4
  switch i32 %59, label %64 [
    i32 0, label %60
    i32 1, label %62
    i32 3, label %61
  ]

60:                                               ; preds = %58
  br label %19, !llvm.loop !13

61:                                               ; preds = %58, %19
  store i32 0, ptr %5, align 4
  br label %62

62:                                               ; preds = %61, %58
  %63 = load i32, ptr %5, align 4
  ret i32 %63

64:                                               ; preds = %58
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z11WriteStreamP20ISequentialOutStreamPKvjPj(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = alloca i32, align 4
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.split.us, label %6

6:                                                ; preds = %4
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %.split

.split.us:                                        ; preds = %4, %7
  %.018.us = phi i32 [ %15, %7 ], [ %2, %4 ]
  %.017.us = phi ptr [ %14, %7 ], [ %1, %4 ]
  %.015.us = phi i32 [ %.015..us, %7 ], [ undef, %4 ]
  %.not22.us = icmp eq i32 %.018.us, 0
  br i1 %.not22.us, label %.split26.us, label %7

7:                                                ; preds = %.split.us
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #2
  %8 = load ptr, ptr %0, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %8, i64 24
  %10 = load ptr, ptr %9, align 8
  %11 = call noundef i32 %10(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %.017.us, i32 noundef %.018.us, ptr noundef nonnull %5)
  %12 = load i32, ptr %5, align 4
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %.017.us, i64 %13
  %15 = sub i32 %.018.us, %12
  %.not23.us = icmp eq i32 %11, 0
  %.015..us = select i1 %.not23.us, i32 %.015.us, i32 %11
  %16 = icmp eq i32 %12, 0
  %..us = select i1 %16, i32 3, i32 0
  %.1.us = select i1 %.not23.us, i32 %..us, i32 1
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #2
  switch i32 %.1.us, label %.unreachabledefault [
    i32 0, label %.split.us
    i32 1, label %.split26.us.loopexit41
    i32 3, label %.split26.us
  ], !llvm.loop !11

.unreachabledefault:                              ; preds = %7
  unreachable

.split:                                           ; preds = %6, %17
  %.018 = phi i32 [ %27, %17 ], [ %2, %6 ]
  %.017 = phi ptr [ %26, %17 ], [ %1, %6 ]
  %.015 = phi i32 [ %.015., %17 ], [ undef, %6 ]
  %.not22 = icmp eq i32 %.018, 0
  br i1 %.not22, label %.split26.us, label %17

17:                                               ; preds = %.split
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #2
  %18 = load ptr, ptr %0, align 8, !tbaa !9
  %19 = getelementptr inbounds i8, ptr %18, i64 24
  %20 = load ptr, ptr %19, align 8
  %21 = call noundef i32 %20(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %.017, i32 noundef %.018, ptr noundef nonnull %5)
  %22 = load i32, ptr %5, align 4
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = add i32 %23, %22
  store i32 %24, ptr %3, align 4, !tbaa !5
  %25 = zext i32 %22 to i64
  %26 = getelementptr inbounds i8, ptr %.017, i64 %25
  %27 = sub i32 %.018, %22
  %.not23 = icmp eq i32 %21, 0
  %.015. = select i1 %.not23, i32 %.015, i32 %21
  %28 = icmp eq i32 %22, 0
  %. = select i1 %28, i32 3, i32 0
  %.1 = select i1 %.not23, i32 %., i32 1
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #2
  switch i32 %.1, label %.unreachabledefault29 [
    i32 0, label %.split
    i32 1, label %.split26.us.loopexit43
    i32 3, label %.split26.us
  ], !llvm.loop !11

.split26.us.loopexit41:                           ; preds = %7
  br label %.split26.us

.split26.us.loopexit43:                           ; preds = %17
  br label %.split26.us

.split26.us:                                      ; preds = %17, %.split, %7, %.split.us, %.split26.us.loopexit43, %.split26.us.loopexit41
  %.2 = phi i32 [ %.015..us, %.split26.us.loopexit41 ], [ %.015., %.split26.us.loopexit43 ], [ 0, %.split.us ], [ 0, %7 ], [ 0, %.split ], [ 0, %17 ]
  ret i32 %.2

.unreachabledefault29:                            ; preds = %17
  unreachable
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"vtable pointer", !8, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass