; LLVM IR from pmwkaa/monotone :: monotone/engine/wal (wal)

; 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.Runtime = type { %struct.ExceptionMgr, %struct.Error, ptr }
%struct.ExceptionMgr = type { ptr }
%struct.Error = type { [256 x i8], i32, ptr, ptr, i32, ptr, ptr }
%struct.Buf = type { ptr, ptr, ptr }
%struct.Guard = type { ptr, ptr, ptr }
%struct.Exception = type { ptr, i8, ptr, [1 x %struct.__jmp_buf_tag] }
%struct.__jmp_buf_tag = type { [8 x i64], i32, %struct.__sigset_t }
%struct.__sigset_t = type { [16 x i64] }
%struct.Wal = type { %struct.Mutex, %struct.WalId, ptr }
%struct.Mutex = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.WalId = type { %struct.Spinlock, i32, %struct.Buf }
%struct.Spinlock = type { i32 }

@mn_runtime = external thread_local global %struct.Runtime, align 8
@.str = external hidden unnamed_addr constant [18 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 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: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

; Function Attrs: nounwind uwtable
define dso_local void @wal_gc(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca %struct.Buf, align 8
  %7 = alloca %struct.Guard, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca [4096 x i8], align 16
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %15 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %15, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #6
  call void @buf_init(ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #6
  %16 = getelementptr inbounds %struct.Guard, ptr %7, i32 0, i32 0
  store ptr @buf_free, ptr %16, align 8, !tbaa !11
  %17 = getelementptr inbounds %struct.Guard, ptr %7, i32 0, i32 1
  store ptr %6, ptr %17, align 8, !tbaa !13
  %18 = getelementptr inbounds %struct.Guard, ptr %7, i32 0, i32 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %19 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @mn_runtime)
  %20 = getelementptr inbounds %struct.Runtime, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %struct.ExceptionMgr, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  store ptr %22, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Exception, ptr %23, i32 0, i32 2
  %25 = load ptr, ptr %24, align 8, !tbaa !19
  store ptr %25, ptr %9, align 8, !tbaa !5
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.Exception, ptr %26, i32 0, i32 2
  store ptr %7, ptr %27, align 8, !tbaa !19
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %28, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %29, ptr %18, align 8, !tbaa !22
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #6
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Wal, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = call i32 @wal_id_gc_between(ptr noundef %31, ptr noundef %6, i64 noundef %32)
  store i32 %33, ptr %11, align 4, !tbaa !23
  %34 = load i32, ptr %11, align 4, !tbaa !23
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %57

36:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4096, ptr %12) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %37 = getelementptr inbounds %struct.Buf, ptr %6, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !24
  store ptr %38, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #6
  store i32 0, ptr %14, align 4, !tbaa !23
  br label %39

39:                                               ; preds = %53, %36
  %40 = load i32, ptr %14, align 4, !tbaa !23
  %41 = load i32, ptr %11, align 4, !tbaa !23
  %42 = icmp slt i32 %40, %41
  br i1 %42, label %43, label %56

43:                                               ; preds = %39
  %44 = getelementptr inbounds [4096 x i8], ptr %12, i64 0, i64 0
  %45 = call ptr @config_directory()
  %46 = load ptr, ptr %13, align 8, !tbaa !5
  %47 = load i32, ptr %14, align 4, !tbaa !23
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i64, ptr %46, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %44, i64 noundef 4096, ptr noundef @.str, ptr noundef %45, i64 noundef %50) #6
  %52 = getelementptr inbounds [4096 x i8], ptr %12, i64 0, i64 0
  call void (ptr, ...) @fs_unlink(ptr noundef @.str.1, ptr noundef %52)
  br label %53

53:                                               ; preds = %43
  %54 = load i32, ptr %14, align 4, !tbaa !23
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %14, align 4, !tbaa !23
  br label %39, !llvm.loop !26

56:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 4096, ptr %12) #6
  br label %57

57:                                               ; preds = %56, %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #6
  call void @guard_runner(ptr noundef %7)
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @buf_init(ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @buf_free(ptr noundef) #3

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @guard_runner(ptr noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @wal_id_gc_between(ptr noundef, ptr noundef, i64 noundef) #3

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @config_directory() #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fs_unlink(ptr noundef, ...) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { inlinehint 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 #4 = { alwaysinline 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 #5 = { 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 #6 = { 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, !6, i64 0}
!12 = !{!"Guard", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!15, !6, i64 0}
!15 = !{!"Runtime", !16, i64 0, !17, i64 8, !6, i64 312}
!16 = !{!"ExceptionMgr", !6, i64 0}
!17 = !{!"Error", !7, i64 0, !18, i64 256, !6, i64 264, !6, i64 272, !18, i64 280, !6, i64 288, !6, i64 296}
!18 = !{!"int", !7, i64 0}
!19 = !{!20, !6, i64 16}
!20 = !{!"Exception", !6, i64 0, !21, i64 8, !6, i64 16, !7, i64 24}
!21 = !{!"_Bool", !7, i64 0}
!22 = !{!12, !6, i64 16}
!23 = !{!18, !18, i64 0}
!24 = !{!25, !6, i64 0}
!25 = !{!"Buf", !6, i64 0, !6, i64 8, !6, i64 16}
!26 = distinct !{!26, !27}
!27 = !{!"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"

%struct.Runtime = type { %struct.ExceptionMgr, %struct.Error, ptr }
%struct.ExceptionMgr = type { ptr }
%struct.Error = type { [256 x i8], i32, ptr, ptr, i32, ptr, ptr }
%struct.Buf = type { ptr, ptr, ptr }
%struct.Guard = type { ptr, ptr, ptr }

@mn_runtime = external thread_local local_unnamed_addr global %struct.Runtime, align 8
@.str = external hidden unnamed_addr constant [18 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; 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 nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

; Function Attrs: nounwind uwtable
define dso_local void @wal_gc(ptr noundef %0, i64 noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.Buf, align 8
  %4 = alloca %struct.Guard, align 8
  %5 = alloca [4096 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #6
  call void @buf_init(ptr noundef nonnull %3)
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #6
  store ptr @buf_free, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %4, i64 8
  store ptr %3, ptr %6, align 8, !tbaa !10
  %7 = getelementptr inbounds i8, ptr %4, i64 16
  %8 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @mn_runtime)
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %9, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !16
  store ptr %4, ptr %10, align 8, !tbaa !16
  store ptr %11, ptr %7, align 8, !tbaa !19
  %12 = getelementptr inbounds i8, ptr %0, i64 40
  %13 = call i32 @wal_id_gc_between(ptr noundef nonnull %12, ptr noundef nonnull %3, i64 noundef %1)
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %15, label %23

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %5) #6
  %16 = load ptr, ptr %3, align 8, !tbaa !20
  %wide.trip.count = zext nneg i32 %13 to i64
  br label %17

17:                                               ; preds = %15, %17
  %indvars.iv = phi i64 [ 0, %15 ], [ %indvars.iv.next, %17 ]
  %18 = call ptr @config_directory()
  %19 = getelementptr inbounds i64, ptr %16, i64 %indvars.iv
  %20 = load i64, ptr %19, align 8, !tbaa !22
  %21 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %5, i64 noundef 4096, ptr noundef nonnull @.str, ptr noundef %18, i64 noundef %20) #6
  call void (ptr, ...) @fs_unlink(ptr noundef nonnull @.str.1, ptr noundef nonnull %5)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %22, label %17, !llvm.loop !24

22:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %5) #6
  br label %23

23:                                               ; preds = %22, %2
  call void @guard_runner(ptr noundef nonnull %4)
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #6
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @buf_init(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @buf_free(ptr noundef) #3

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @guard_runner(ptr noundef) local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @wal_id_gc_between(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @config_directory() local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @fs_unlink(ptr noundef, ...) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { inlinehint 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 #4 = { alwaysinline 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 #5 = { nofree 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 #6 = { 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, !7, i64 0}
!6 = !{!"Guard", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!12, !7, i64 0}
!12 = !{!"Runtime", !13, i64 0, !14, i64 8, !7, i64 312}
!13 = !{!"ExceptionMgr", !7, i64 0}
!14 = !{!"Error", !8, i64 0, !15, i64 256, !7, i64 264, !7, i64 272, !15, i64 280, !7, i64 288, !7, i64 296}
!15 = !{!"int", !8, i64 0}
!16 = !{!17, !7, i64 16}
!17 = !{!"Exception", !7, i64 0, !18, i64 8, !7, i64 16, !8, i64 24}
!18 = !{!"_Bool", !8, i64 0}
!19 = !{!6, !7, i64 16}
!20 = !{!21, !7, i64 0}
!21 = !{!"Buf", !7, i64 0, !7, i64 8, !7, i64 16}
!22 = !{!23, !23, i64 0}
!23 = !{!"long", !8, i64 0}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass