; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/7zip/Common/OutBuffer.ll (ZN10COutBuffer9FlushPartEv)

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

%class.COutBuffer = type <{ ptr, i32, i32, i32, i32, ptr, i64, ptr, i8, [7 x i8] }>

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(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: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN10COutBuffer9FlushPartEv(ptr noundef nonnull align 8 dereferenceable(49) %0) #2 align 2 personality ptr @__gxx_personality_v0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %7 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 1
  %10 = load i32, ptr %9, align 8, !tbaa !14
  %11 = icmp uge i32 %8, %10
  br i1 %11, label %12, label %18

12:                                               ; preds = %1
  %13 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 4
  %14 = load i32, ptr %13, align 4, !tbaa !15
  %15 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !9
  %17 = sub i32 %14, %16
  br label %24

18:                                               ; preds = %1
  %19 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 1
  %20 = load i32, ptr %19, align 8, !tbaa !14
  %21 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %22 = load i32, ptr %21, align 8, !tbaa !9
  %23 = sub i32 %20, %22
  br label %24

24:                                               ; preds = %18, %12
  %25 = phi i32 [ %17, %12 ], [ %23, %18 ]
  store i32 %25, ptr %3, align 4, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !16
  %26 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 7
  %27 = load ptr, ptr %26, align 8, !tbaa !17
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %45

29:                                               ; preds = %24
  %30 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 7
  %31 = load ptr, ptr %30, align 8, !tbaa !17
  %32 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !18
  %34 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %35 = load i32, ptr %34, align 8, !tbaa !9
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %33, i64 %36
  %38 = load i32, ptr %3, align 4, !tbaa !16
  %39 = zext i32 %38 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %31, ptr align 1 %37, i64 %39, i1 false)
  %40 = load i32, ptr %3, align 4, !tbaa !16
  %41 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 7
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  %43 = zext i32 %40 to i64
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  store ptr %44, ptr %41, align 8, !tbaa !17
  br label %45

45:                                               ; preds = %29, %24
  %46 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 5
  %47 = load ptr, ptr %46, align 8, !tbaa !19
  %48 = icmp ne ptr %47, null
  br i1 %48, label %49, label %65

49:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !16
  %50 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 5
  %51 = load ptr, ptr %50, align 8, !tbaa !19
  %52 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !18
  %54 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %55 = load i32, ptr %54, align 8, !tbaa !9
  %56 = zext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %53, i64 %56
  %58 = load i32, ptr %3, align 4, !tbaa !16
  %59 = load ptr, ptr %51, align 8, !tbaa !20
  %60 = getelementptr inbounds ptr, ptr %59, i64 5
  %61 = load ptr, ptr %60, align 8
  %62 = invoke noundef i32 %61(ptr noundef nonnull align 8 dereferenceable(8) %51, ptr noundef %57, i32 noundef %58, ptr noundef %5)
          to label %63 unwind label %107

63:                                               ; preds = %49
  store i32 %62, ptr %4, align 4, !tbaa !16
  %64 = load i32, ptr %5, align 4, !tbaa !16
  store i32 %64, ptr %3, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %65

65:                                               ; preds = %63, %45
  %66 = load i32, ptr %3, align 4, !tbaa !16
  %67 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %68 = load i32, ptr %67, align 8, !tbaa !9
  %69 = add i32 %68, %66
  store i32 %69, ptr %67, align 8, !tbaa !9
  %70 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %71 = load i32, ptr %70, align 8, !tbaa !9
  %72 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 4
  %73 = load i32, ptr %72, align 4, !tbaa !15
  %74 = icmp eq i32 %71, %73
  br i1 %74, label %75, label %77

75:                                               ; preds = %65
  %76 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  store i32 0, ptr %76, align 8, !tbaa !9
  br label %77

77:                                               ; preds = %75, %65
  %78 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 1
  %79 = load i32, ptr %78, align 8, !tbaa !14
  %80 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 4
  %81 = load i32, ptr %80, align 4, !tbaa !15
  %82 = icmp eq i32 %79, %81
  br i1 %82, label %83, label %86

83:                                               ; preds = %77
  %84 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 8
  store i8 1, ptr %84, align 8, !tbaa !22
  %85 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 1
  store i32 0, ptr %85, align 8, !tbaa !14
  br label %86

86:                                               ; preds = %83, %77
  %87 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %88 = load i32, ptr %87, align 8, !tbaa !9
  %89 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 1
  %90 = load i32, ptr %89, align 8, !tbaa !14
  %91 = icmp ugt i32 %88, %90
  br i1 %91, label %92, label %95

92:                                               ; preds = %86
  %93 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 3
  %94 = load i32, ptr %93, align 8, !tbaa !9
  br label %98

95:                                               ; preds = %86
  %96 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 4
  %97 = load i32, ptr %96, align 4, !tbaa !15
  br label %98

98:                                               ; preds = %95, %92
  %99 = phi i32 [ %94, %92 ], [ %97, %95 ]
  %100 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 2
  store i32 %99, ptr %100, align 4, !tbaa !23
  %101 = load i32, ptr %3, align 4, !tbaa !16
  %102 = zext i32 %101 to i64
  %103 = getelementptr inbounds %class.COutBuffer, ptr %6, i32 0, i32 6
  %104 = load i64, ptr %103, align 8, !tbaa !24
  %105 = add i64 %104, %102
  store i64 %105, ptr %103, align 8, !tbaa !24
  %106 = load i32, ptr %4, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret i32 %106

107:                                              ; preds = %49
  %108 = landingpad { ptr, i32 }
          catch ptr null
  %109 = extractvalue { ptr, i32 } %108, 0
  call void @__clang_call_terminate(ptr %109) #5
  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) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noinline noreturn nounwind uwtable "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 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: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { noreturn 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, !11, i64 16}
!10 = !{!"_ZTS10COutBuffer", !6, i64 0, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !6, i64 24, !12, i64 32, !6, i64 40, !13, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"bool", !7, i64 0}
!14 = !{!10, !11, i64 8}
!15 = !{!10, !11, i64 20}
!16 = !{!11, !11, i64 0}
!17 = !{!10, !6, i64 40}
!18 = !{!10, !6, i64 0}
!19 = !{!10, !6, i64 24}
!20 = !{!21, !21, i64 0}
!21 = !{!"vtable pointer", !8, i64 0}
!22 = !{!10, !13, i64 48}
!23 = !{!10, !11, i64 12}
!24 = !{!10, !12, 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"

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

declare i32 @__gxx_personality_v0(...)

; Function Attrs: noinline noreturn nounwind uwtable
declare hidden void @__clang_call_terminate(ptr noundef) local_unnamed_addr #1

; 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 nounwind uwtable
define dso_local noundef i32 @_ZN10COutBuffer9FlushPartEv(ptr nocapture noundef nonnull align 8 dereferenceable(49) %0) local_unnamed_addr #2 align 2 personality ptr @__gxx_personality_v0 {
  %2 = alloca i32, align 4
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !13
  %.not = icmp ult i32 %4, %6
  %7 = getelementptr inbounds i8, ptr %0, i64 20
  %8 = load i32, ptr %7, align 4
  %.pn = select i1 %.not, i32 %6, i32 %8
  %9 = sub i32 %.pn, %4
  %10 = getelementptr inbounds i8, ptr %0, i64 40
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %.not11 = icmp eq ptr %11, null
  br i1 %.not11, label %19, label %12

12:                                               ; preds = %1
  %13 = load ptr, ptr %0, align 8, !tbaa !15
  %14 = zext i32 %4 to i64
  %15 = getelementptr inbounds i8, ptr %13, i64 %14
  %16 = zext i32 %9 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %11, ptr align 1 %15, i64 %16, i1 false)
  %17 = load ptr, ptr %10, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %17, i64 %16
  store ptr %18, ptr %10, align 8, !tbaa !14
  %.pre13.pre = load i32, ptr %3, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %12, %1
  %.pre13 = phi i32 [ %.pre13.pre, %12 ], [ %4, %1 ]
  %20 = getelementptr inbounds i8, ptr %0, i64 24
  %21 = load ptr, ptr %20, align 8, !tbaa !16
  %.not12 = icmp eq ptr %21, null
  br i1 %.not12, label %32, label %22

22:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  store i32 0, ptr %2, align 4, !tbaa !17
  %23 = load ptr, ptr %0, align 8, !tbaa !15
  %24 = zext i32 %.pre13 to i64
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  %26 = load ptr, ptr %21, align 8, !tbaa !18
  %27 = getelementptr inbounds i8, ptr %26, i64 40
  %28 = load ptr, ptr %27, align 8
  %29 = invoke noundef i32 %28(ptr noundef nonnull align 8 dereferenceable(8) %21, ptr noundef %25, i32 noundef %9, ptr noundef nonnull %2)
          to label %30 unwind label %50

30:                                               ; preds = %22
  %31 = load i32, ptr %2, align 4, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  %.pre = load i32, ptr %3, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %30, %19
  %33 = phi i32 [ %.pre, %30 ], [ %.pre13, %19 ]
  %.06 = phi i32 [ %31, %30 ], [ %9, %19 ]
  %.0 = phi i32 [ %29, %30 ], [ 0, %19 ]
  %34 = add i32 %33, %.06
  %35 = load i32, ptr %7, align 4
  %36 = icmp eq i32 %34, %35
  %spec.store.select = select i1 %36, i32 0, i32 %34
  store i32 %spec.store.select, ptr %3, align 8
  %37 = load i32, ptr %5, align 8, !tbaa !13
  %38 = icmp eq i32 %37, %35
  br i1 %38, label %39, label %41

39:                                               ; preds = %32
  %40 = getelementptr inbounds i8, ptr %0, i64 48
  store i8 1, ptr %40, align 8, !tbaa !20
  store i32 0, ptr %5, align 8, !tbaa !13
  br label %41

41:                                               ; preds = %39, %32
  %42 = phi i32 [ 0, %39 ], [ %37, %32 ]
  %43 = icmp ugt i32 %spec.store.select, %42
  %44 = select i1 %43, i32 %spec.store.select, i32 %35
  %45 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %44, ptr %45, align 4, !tbaa !21
  %46 = zext i32 %.06 to i64
  %47 = getelementptr inbounds i8, ptr %0, i64 32
  %48 = load i64, ptr %47, align 8, !tbaa !22
  %49 = add i64 %48, %46
  store i64 %49, ptr %47, align 8, !tbaa !22
  ret i32 %.0

50:                                               ; preds = %22
  %51 = landingpad { ptr, i32 }
          catch ptr null
  %52 = extractvalue { ptr, i32 } %51, 0
  call void @__clang_call_terminate(ptr %52) #5
  unreachable
}

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { noinline noreturn nounwind uwtable "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 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { noreturn 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, !10, i64 16}
!6 = !{!"_ZTS10COutBuffer", !7, i64 0, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !7, i64 24, !11, i64 32, !7, i64 40, !12, i64 48}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long long", !8, i64 0}
!12 = !{!"bool", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !7, i64 40}
!15 = !{!6, !7, i64 0}
!16 = !{!6, !7, i64 24}
!17 = !{!10, !10, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"vtable pointer", !9, i64 0}
!20 = !{!6, !12, i64 48}
!21 = !{!6, !10, i64 12}
!22 = !{!6, !11, i64 32}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass