; LLVM IR from DanEngelbrecht/longtail :: test (test)

; 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.TestAsyncBlockStore = type { %struct.Longtail_BlockStoreAPI, ptr, ptr, i32, ptr, ptr, ptr, [8 x ptr], i32, ptr, i64, ptr, i64, ptr, i64, ptr, ptr, ptr }
%struct.Longtail_BlockStoreAPI = type { %struct.Longtail_API, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.Longtail_API = type { ptr }
%struct.stbds_array_header = type { i64, i64, ptr, i64 }
%struct.Longtail_AsyncFlushAPI = type { %struct.Longtail_API, ptr }

; 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

declare void @Longtail_LockSpinLock(ptr noundef) #1

declare void @Longtail_UnlockSpinLock(ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN19TestAsyncBlockStore5FlushEP22Longtail_BlockStoreAPIP22Longtail_AsyncFlushAPI(ptr noundef %0, ptr noundef %1) #2 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %8, ptr %6, align 8, !tbaa !5
  %9 = load ptr, ptr %6, align 8, !tbaa !5
  %10 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %9, i32 0, i32 4
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  call void @Longtail_LockSpinLock(ptr noundef %11)
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %12, i32 0, i32 8
  %14 = load volatile i32, ptr %13, align 8, !tbaa !15
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %16, label %61

16:                                               ; preds = %2
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %17, i32 0, i32 9
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %36

21:                                               ; preds = %16
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %22, i32 0, i32 9
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = getelementptr inbounds %struct.stbds_array_header, ptr %24, i64 -1
  %26 = getelementptr inbounds %struct.stbds_array_header, ptr %25, i32 0, i32 0
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = add i64 %27, 1
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %29, i32 0, i32 9
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  %32 = getelementptr inbounds %struct.stbds_array_header, ptr %31, i64 -1
  %33 = getelementptr inbounds %struct.stbds_array_header, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !19
  %35 = icmp ugt i64 %28, %34
  br i1 %35, label %36, label %43

36:                                               ; preds = %21, %16
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %37, i32 0, i32 9
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = call noundef ptr @_ZL22stbds_arrgrowf_wrapperIP22Longtail_AsyncFlushAPIEPT_S3_mmm(ptr noundef %39, i64 noundef 8, i64 noundef 1, i64 noundef 0)
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %41, i32 0, i32 9
  store ptr %40, ptr %42, align 8, !tbaa !16
  br label %44

43:                                               ; preds = %21
  br label %44

44:                                               ; preds = %43, %36
  %45 = phi i32 [ 0, %36 ], [ 0, %43 ]
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %47, i32 0, i32 9
  %49 = load ptr, ptr %48, align 8, !tbaa !16
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %50, i32 0, i32 9
  %52 = load ptr, ptr %51, align 8, !tbaa !16
  %53 = getelementptr inbounds %struct.stbds_array_header, ptr %52, i64 -1
  %54 = getelementptr inbounds %struct.stbds_array_header, ptr %53, i32 0, i32 0
  %55 = load i64, ptr %54, align 8, !tbaa !17
  %56 = add i64 %55, 1
  store i64 %56, ptr %54, align 8, !tbaa !17
  %57 = getelementptr inbounds ptr, ptr %49, i64 %55
  store ptr %46, ptr %57, align 8, !tbaa !5
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %58, i32 0, i32 4
  %60 = load ptr, ptr %59, align 8, !tbaa !9
  call void @Longtail_UnlockSpinLock(ptr noundef %60)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

61:                                               ; preds = %2
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %class.TestAsyncBlockStore, ptr %62, i32 0, i32 4
  %64 = load ptr, ptr %63, align 8, !tbaa !9
  call void @Longtail_UnlockSpinLock(ptr noundef %64)
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Longtail_AsyncFlushAPI, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !20
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  call void %67(ptr noundef %68, i32 noundef 0)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %69

69:                                               ; preds = %61, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %70 = load i32, ptr %3, align 4
  ret i32 %70
}

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL22stbds_arrgrowf_wrapperIP22Longtail_AsyncFlushAPIEPT_S3_mmm(ptr noundef, i64 noundef, i64 noundef, i64 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 = { 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, !6, i64 88}
!10 = !{!"_ZTS19TestAsyncBlockStore", !11, i64 0, !6, i64 64, !6, i64 72, !13, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !7, i64 112, !13, i64 176, !6, i64 184, !14, i64 192, !6, i64 200, !14, i64 208, !6, i64 216, !14, i64 224, !6, i64 232, !6, i64 240, !6, i64 248}
!11 = !{!"_ZTS22Longtail_BlockStoreAPI", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56}
!12 = !{!"_ZTS12Longtail_API", !6, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!10, !13, i64 176}
!16 = !{!10, !6, i64 184}
!17 = !{!18, !14, i64 0}
!18 = !{!"_ZTS18stbds_array_header", !14, i64 0, !14, i64 8, !6, i64 16, !14, i64 24}
!19 = !{!18, !14, i64 8}
!20 = !{!21, !6, i64 8}
!21 = !{!"_ZTS22Longtail_AsyncFlushAPI", !12, i64 0, !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"

declare void @Longtail_LockSpinLock(ptr noundef) local_unnamed_addr #0

declare void @Longtail_UnlockSpinLock(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN19TestAsyncBlockStore5FlushEP22Longtail_BlockStoreAPIP22Longtail_AsyncFlushAPI(ptr noundef %0, ptr noundef %1) local_unnamed_addr #1 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 88
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  tail call void @Longtail_LockSpinLock(ptr noundef %4)
  %5 = getelementptr inbounds i8, ptr %0, i64 176
  %6 = load volatile i32, ptr %5, align 8, !tbaa !14
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %8, label %26

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 184
  %10 = load ptr, ptr %9, align 8, !tbaa !15
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %18, label %11

11:                                               ; preds = %8
  %12 = getelementptr inbounds i8, ptr %10, i64 -32
  %13 = load i64, ptr %12, align 8, !tbaa !16
  %14 = add i64 %13, 1
  %15 = getelementptr inbounds i8, ptr %10, i64 -24
  %16 = load i64, ptr %15, align 8, !tbaa !18
  %17 = icmp ugt i64 %14, %16
  br i1 %17, label %18, label %20

18:                                               ; preds = %11, %8
  %19 = tail call noundef ptr @_ZL22stbds_arrgrowf_wrapperIP22Longtail_AsyncFlushAPIEPT_S3_mmm(ptr noundef %10, i64 noundef 8, i64 noundef 1, i64 noundef 0)
  store ptr %19, ptr %9, align 8, !tbaa !15
  %.phi.trans.insert = getelementptr inbounds i8, ptr %19, i64 -32
  %.pre = load i64, ptr %.phi.trans.insert, align 8, !tbaa !16
  %.pre15 = add i64 %.pre, 1
  br label %20

20:                                               ; preds = %11, %18
  %.pre-phi = phi i64 [ %14, %11 ], [ %.pre15, %18 ]
  %21 = phi i64 [ %13, %11 ], [ %.pre, %18 ]
  %22 = phi ptr [ %10, %11 ], [ %19, %18 ]
  %23 = getelementptr inbounds i8, ptr %22, i64 -32
  store i64 %.pre-phi, ptr %23, align 8, !tbaa !16
  %24 = getelementptr inbounds ptr, ptr %22, i64 %21
  store ptr %1, ptr %24, align 8, !tbaa !19
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  tail call void @Longtail_UnlockSpinLock(ptr noundef %25)
  br label %30

26:                                               ; preds = %2
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  tail call void @Longtail_UnlockSpinLock(ptr noundef %27)
  %28 = getelementptr inbounds i8, ptr %1, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !20
  tail call void %29(ptr noundef %1, i32 noundef 0)
  br label %30

30:                                               ; preds = %26, %20
  ret i32 0
}

; Function Attrs: mustprogress uwtable
declare hidden noundef ptr @_ZL22stbds_arrgrowf_wrapperIP22Longtail_AsyncFlushAPIEPT_S3_mmm(ptr noundef, i64 noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

attributes #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 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" }

!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, !9, i64 88}
!6 = !{!"_ZTS19TestAsyncBlockStore", !7, i64 0, !9, i64 64, !9, i64 72, !12, i64 80, !9, i64 88, !9, i64 96, !9, i64 104, !10, i64 112, !12, i64 176, !9, i64 184, !13, i64 192, !9, i64 200, !13, i64 208, !9, i64 216, !13, i64 224, !9, i64 232, !9, i64 240, !9, i64 248}
!7 = !{!"_ZTS22Longtail_BlockStoreAPI", !8, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56}
!8 = !{!"_ZTS12Longtail_API", !9, i64 0}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!"long", !10, i64 0}
!14 = !{!6, !12, i64 176}
!15 = !{!6, !9, i64 184}
!16 = !{!17, !13, i64 0}
!17 = !{!"_ZTS18stbds_array_header", !13, i64 0, !13, i64 8, !9, i64 16, !13, i64 24}
!18 = !{!17, !13, i64 8}
!19 = !{!9, !9, i64 0}
!20 = !{!21, !9, i64 8}
!21 = !{!"_ZTS22Longtail_AsyncFlushAPI", !8, i64 0, !9, i64 8}


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