; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/C (7zBuf2)

; 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.CDynBuf = type { ptr, i64, i64 }
%struct.ISzAlloc = type { ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local i32 @DynBuf_Write(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr 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 ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %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 ptr %3, ptr %9, align 8, !tbaa !5
  %13 = load i64, ptr %8, align 8, !tbaa !9
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.CDynBuf, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.CDynBuf, ptr %17, i32 0, i32 2
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = sub i64 %16, %19
  %21 = icmp ugt i64 %13, %20
  br i1 %21, label %22, label %71

22:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.CDynBuf, ptr %23, i32 0, i32 2
  %25 = load i64, ptr %24, align 8, !tbaa !13
  %26 = load i64, ptr %8, align 8, !tbaa !9
  %27 = add i64 %25, %26
  store i64 %27, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %28 = load i64, ptr %10, align 8, !tbaa !9
  %29 = udiv i64 %28, 4
  %30 = load i64, ptr %10, align 8, !tbaa !9
  %31 = add i64 %30, %29
  store i64 %31, ptr %10, align 8, !tbaa !9
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ISzAlloc, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = load ptr, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr %10, align 8, !tbaa !9
  %37 = call ptr %34(ptr noundef %35, i64 noundef %36)
  store ptr %37, ptr %11, align 8, !tbaa !5
  %38 = load ptr, ptr %11, align 8, !tbaa !5
  %39 = icmp ne ptr %38, null
  br i1 %39, label %41, label %40

40:                                               ; preds = %22
  store i32 0, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %68

41:                                               ; preds = %22
  %42 = load i64, ptr %10, align 8, !tbaa !9
  %43 = load ptr, ptr %6, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.CDynBuf, ptr %43, i32 0, i32 1
  store i64 %42, ptr %44, align 8, !tbaa !11
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.CDynBuf, ptr %45, i32 0, i32 2
  %47 = load i64, ptr %46, align 8, !tbaa !13
  %48 = icmp ne i64 %47, 0
  br i1 %48, label %49, label %57

49:                                               ; preds = %41
  %50 = load ptr, ptr %11, align 8, !tbaa !5
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.CDynBuf, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.CDynBuf, ptr %54, i32 0, i32 2
  %56 = load i64, ptr %55, align 8, !tbaa !13
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %50, ptr align 1 %53, i64 %56, i1 false)
  br label %57

57:                                               ; preds = %49, %41
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.ISzAlloc, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !17
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.CDynBuf, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !16
  call void %60(ptr noundef %61, ptr noundef %64)
  %65 = load ptr, ptr %11, align 8, !tbaa !5
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.CDynBuf, ptr %66, i32 0, i32 0
  store ptr %65, ptr %67, align 8, !tbaa !16
  store i32 0, ptr %12, align 4
  br label %68

68:                                               ; preds = %57, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %69 = load i32, ptr %12, align 4
  switch i32 %69, label %92 [
    i32 0, label %70
    i32 1, label %90
  ]

70:                                               ; preds = %68
  br label %71

71:                                               ; preds = %70, %4
  %72 = load i64, ptr %8, align 8, !tbaa !9
  %73 = icmp ne i64 %72, 0
  br i1 %73, label %74, label %89

74:                                               ; preds = %71
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.CDynBuf, ptr %75, i32 0, i32 0
  %77 = load ptr, ptr %76, align 8, !tbaa !16
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.CDynBuf, ptr %78, i32 0, i32 2
  %80 = load i64, ptr %79, align 8, !tbaa !13
  %81 = getelementptr inbounds i8, ptr %77, i64 %80
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %81, ptr align 1 %82, i64 %83, i1 false)
  %84 = load i64, ptr %8, align 8, !tbaa !9
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.CDynBuf, ptr %85, i32 0, i32 2
  %87 = load i64, ptr %86, align 8, !tbaa !13
  %88 = add i64 %87, %84
  store i64 %88, ptr %86, align 8, !tbaa !13
  br label %89

89:                                               ; preds = %74, %71
  store i32 1, ptr %5, align 4
  br label %90

90:                                               ; preds = %89, %68
  %91 = load i32, ptr %5, align 4
  ret i32 %91

92:                                               ; preds = %68
  unreachable
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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, !10, i64 8}
!12 = !{!"", !6, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 16}
!14 = !{!15, !6, i64 0}
!15 = !{!"ISzAlloc", !6, i64 0, !6, i64 8}
!16 = !{!12, !6, i64 0}
!17 = !{!15, !6, i64 8}


; 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: nounwind uwtable
define dso_local range(i32 0, 2) i32 @DynBuf_Write(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load i64, ptr %7, align 8, !tbaa !11
  %9 = sub i64 %6, %8
  %10 = icmp ult i64 %9, %2
  br i1 %10, label %11, label %24

11:                                               ; preds = %4
  %12 = add i64 %8, %2
  %13 = lshr i64 %12, 2
  %14 = add i64 %13, %12
  %15 = load ptr, ptr %3, align 8, !tbaa !12
  %16 = tail call ptr %15(ptr noundef nonnull %3, i64 noundef %14) #2
  %.not = icmp eq ptr %16, null
  br i1 %.not, label %31, label %17

17:                                               ; preds = %11
  store i64 %14, ptr %5, align 8, !tbaa !5
  %18 = load i64, ptr %7, align 8, !tbaa !11
  %.not35 = icmp eq i64 %18, 0
  br i1 %.not35, label %.thread38, label %19

19:                                               ; preds = %17
  %20 = load ptr, ptr %0, align 8, !tbaa !14
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %16, ptr align 1 %20, i64 %18, i1 false)
  br label %.thread38

.thread38:                                        ; preds = %19, %17
  %21 = getelementptr inbounds i8, ptr %3, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = load ptr, ptr %0, align 8, !tbaa !14
  tail call void %22(ptr noundef nonnull %3, ptr noundef %23) #2
  store ptr %16, ptr %0, align 8, !tbaa !14
  %.pre40 = load i64, ptr %7, align 8, !tbaa !11
  br label %25

24:                                               ; preds = %4
  %.not36 = icmp eq i64 %2, 0
  br i1 %.not36, label %31, label %._crit_edge

._crit_edge:                                      ; preds = %24
  %.pre = load ptr, ptr %0, align 8, !tbaa !14
  br label %25

25:                                               ; preds = %._crit_edge, %.thread38
  %26 = phi i64 [ %8, %._crit_edge ], [ %.pre40, %.thread38 ]
  %27 = phi ptr [ %.pre, %._crit_edge ], [ %16, %.thread38 ]
  %28 = getelementptr inbounds i8, ptr %27, i64 %26
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %28, ptr align 1 %1, i64 %2, i1 false)
  %29 = load i64, ptr %7, align 8, !tbaa !11
  %30 = add i64 %29, %2
  store i64 %30, ptr %7, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %11, %24, %25
  %.1 = phi i32 [ 1, %25 ], [ 1, %24 ], [ 0, %11 ]
  ret i32 %.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) #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) }
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, !10, i64 8}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!13, !7, i64 0}
!13 = !{!"ISzAlloc", !7, i64 0, !7, i64 8}
!14 = !{!6, !7, i64 0}
!15 = !{!13, !7, i64 8}


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