; 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.Context = type { ptr, ptr, ptr }
%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 }
%struct.Buf = type { ptr, ptr, ptr }
%struct.WalFile = type { i64, %struct.File }
%struct.File = type { i32, i64, %struct.Str, %struct.List }
%struct.Str = type { ptr, ptr, i8 }
%struct.List = type { ptr, ptr }

@mn_runtime = external thread_local global %struct.Runtime, align 8
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [75 x i8], align 1
@__func__.wal_open = external hidden unnamed_addr constant [9 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [24 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: nounwind uwtable
declare dso_local void @wal_rotate(ptr noundef, i64 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #2

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @wal_open(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca [4096 x i8], align 16
  %4 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4096, ptr %3) #5
  %5 = getelementptr inbounds [4096 x i8], ptr %3, i64 0, i64 0
  %6 = call ptr @config_directory()
  %7 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %5, i64 noundef 4096, ptr noundef @.str.2, ptr noundef %6) #5
  %8 = getelementptr inbounds [4096 x i8], ptr %3, i64 0, i64 0
  %9 = call zeroext i1 (ptr, ...) @fs_exists(ptr noundef @.str.1, ptr noundef %8)
  br i1 %9, label %23, label %10

10:                                               ; preds = %1
  %11 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @mn_runtime)
  %12 = getelementptr inbounds %struct.Runtime, ptr %11, i32 0, i32 2
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.Context, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !14
  %16 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @mn_runtime)
  %17 = getelementptr inbounds %struct.Runtime, ptr %16, i32 0, i32 2
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.Context, ptr %18, i32 0, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  %21 = getelementptr inbounds [4096 x i8], ptr %3, i64 0, i64 0
  call void (ptr, ptr, ptr, ptr, i32, ptr, ...) @log_at(ptr noundef %15, ptr noundef %20, ptr noundef @.str.3, ptr noundef @__func__.wal_open, i32 noundef 176, ptr noundef @.str.4, ptr noundef %21)
  %22 = getelementptr inbounds [4096 x i8], ptr %3, i64 0, i64 0
  call void (i32, ptr, ...) @fs_mkdir(i32 noundef 493, ptr noundef @.str.1, ptr noundef %22)
  br label %23

23:                                               ; preds = %10, %1
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds [4096 x i8], ptr %3, i64 0, i64 0
  call void @wal_recover(ptr noundef %24, ptr noundef %25)
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.Wal, ptr %26, i32 0, i32 1
  %28 = getelementptr inbounds %struct.WalId, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !17
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %31, label %46

31:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Wal, ptr %32, i32 0, i32 1
  %34 = call i64 @wal_id_max(ptr noundef %33)
  store i64 %34, ptr %4, align 8, !tbaa !23
  %35 = load i64, ptr %4, align 8, !tbaa !23
  %36 = call ptr @wal_file_allocate(i64 noundef %35)
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Wal, ptr %37, i32 0, i32 2
  store ptr %36, ptr %38, align 8, !tbaa !25
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Wal, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !25
  call void @wal_file_open(ptr noundef %41)
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.Wal, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !25
  %45 = getelementptr inbounds %struct.WalFile, ptr %44, i32 0, i32 1
  call void @file_seek_to_end(ptr noundef %45)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %48

46:                                               ; preds = %23
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  call void @wal_rotate(ptr noundef %47, i64 noundef 0)
  br label %48

48:                                               ; preds = %46, %31
  call void @llvm.lifetime.end.p0(i64 4096, ptr %3) #5
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @fs_exists(ptr noundef, ...) #3

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

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

; Function Attrs: nounwind uwtable
declare hidden void @wal_recover(ptr noundef, ptr noundef) #1

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

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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 #5 = { 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, !6, i64 312}
!10 = !{!"Runtime", !11, i64 0, !12, i64 8, !6, i64 312}
!11 = !{!"ExceptionMgr", !6, i64 0}
!12 = !{!"Error", !7, i64 0, !13, i64 256, !6, i64 264, !6, i64 272, !13, i64 280, !6, i64 288, !6, i64 296}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"Context", !6, i64 0, !6, i64 8, !6, i64 16}
!16 = !{!15, !6, i64 8}
!17 = !{!18, !13, i64 44}
!18 = !{!"Wal", !19, i64 0, !20, i64 40, !6, i64 72}
!19 = !{!"Mutex", !7, i64 0}
!20 = !{!"WalId", !21, i64 0, !13, i64 4, !22, i64 8}
!21 = !{!"Spinlock", !13, i64 0}
!22 = !{!"Buf", !6, i64 0, !6, i64 8, !6, i64 16}
!23 = !{!24, !24, i64 0}
!24 = !{!"long", !7, i64 0}
!25 = !{!18, !6, i64 72}


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

@mn_runtime = external thread_local local_unnamed_addr global %struct.Runtime, align 8
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [75 x i8], align 1
@__func__.wal_open = external hidden unnamed_addr constant [9 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [24 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: nounwind uwtable
declare dso_local void @wal_rotate(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @wal_file_allocate(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 #4

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

; Function Attrs: nounwind uwtable
define dso_local void @wal_open(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca [4096 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 4096, ptr nonnull %2) #5
  %3 = tail call ptr @config_directory()
  %4 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %2, i64 noundef 4096, ptr noundef nonnull @.str.2, ptr noundef %3) #5
  %5 = call zeroext i1 (ptr, ...) @fs_exists(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  br i1 %5, label %13, label %6

6:                                                ; preds = %1
  %7 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @mn_runtime)
  %8 = getelementptr inbounds i8, ptr %7, i64 312
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  call void (ptr, ptr, ptr, ptr, i32, ptr, ...) @log_at(ptr noundef %10, ptr noundef %12, ptr noundef nonnull @.str.3, ptr noundef nonnull @__func__.wal_open, i32 noundef 176, ptr noundef nonnull @.str.4, ptr noundef nonnull %2)
  call void (i32, ptr, ...) @fs_mkdir(i32 noundef 493, ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  br label %13

13:                                               ; preds = %6, %1
  call void @wal_recover(ptr noundef %0, ptr noundef nonnull %2)
  %14 = getelementptr inbounds i8, ptr %0, i64 44
  %15 = load i32, ptr %14, align 4, !tbaa !16
  %16 = icmp sgt i32 %15, 0
  br i1 %16, label %17, label %24

17:                                               ; preds = %13
  %18 = getelementptr inbounds i8, ptr %0, i64 40
  %19 = call i64 @wal_id_max(ptr noundef nonnull %18)
  %20 = call ptr @wal_file_allocate(i64 noundef %19)
  %21 = getelementptr inbounds i8, ptr %0, i64 72
  store ptr %20, ptr %21, align 8, !tbaa !22
  call void @wal_file_open(ptr noundef %20)
  %22 = load ptr, ptr %21, align 8, !tbaa !22
  %23 = getelementptr inbounds i8, ptr %22, i64 8
  call void @file_seek_to_end(ptr noundef nonnull %23)
  br label %25

24:                                               ; preds = %13
  call void @wal_rotate(ptr noundef nonnull %0, i64 noundef 0)
  br label %25

25:                                               ; preds = %24, %17
  call void @llvm.lifetime.end.p0(i64 4096, ptr nonnull %2) #5
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @fs_exists(ptr noundef, ...) local_unnamed_addr #3

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

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

; Function Attrs: nounwind uwtable
declare hidden void @wal_recover(ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = { 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 #5 = { 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, !8, i64 312}
!6 = !{!"Runtime", !7, i64 0, !11, i64 8, !8, i64 312}
!7 = !{!"ExceptionMgr", !8, i64 0}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"Error", !9, i64 0, !12, i64 256, !8, i64 264, !8, i64 272, !12, i64 280, !8, i64 288, !8, i64 296}
!12 = !{!"int", !9, i64 0}
!13 = !{!14, !8, i64 0}
!14 = !{!"Context", !8, i64 0, !8, i64 8, !8, i64 16}
!15 = !{!14, !8, i64 8}
!16 = !{!17, !12, i64 44}
!17 = !{!"Wal", !18, i64 0, !19, i64 40, !8, i64 72}
!18 = !{!"Mutex", !9, i64 0}
!19 = !{!"WalId", !20, i64 0, !12, i64 4, !21, i64 8}
!20 = !{!"Spinlock", !12, i64 0}
!21 = !{!"Buf", !8, i64 0, !8, i64 8, !8, i64 16}
!22 = !{!17, !8, i64 72}


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