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

; 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.WalCursor = type { %struct.Buf, ptr, i64, ptr }
%struct.Buf = 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.LogWrite = type <{ i32, i64, i8, i32, i32 }>

; Function Attrs: nounwind uwtable
define dso_local void @wal_cursor_open(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.WalCursor, ptr %10, i32 0, i32 1
  store ptr null, ptr %11, align 8, !tbaa !11
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.WalCursor, ptr %12, i32 0, i32 2
  store i64 0, ptr %13, align 8, !tbaa !14
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.WalCursor, ptr %15, i32 0, i32 3
  store ptr %14, ptr %16, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %19, label %23

19:                                               ; preds = %3
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Wal, ptr %20, i32 0, i32 1
  %22 = call i64 @wal_id_min(ptr noundef %21)
  store i64 %22, ptr %7, align 8, !tbaa !9
  br label %28

23:                                               ; preds = %3
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Wal, ptr %24, i32 0, i32 1
  %26 = load i64, ptr %6, align 8, !tbaa !9
  %27 = call i64 @wal_id_find(ptr noundef %25, i64 noundef %26)
  store i64 %27, ptr %7, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %23, %19
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = icmp eq i64 %29, -1
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  store i32 1, ptr %8, align 4
  br label %66

32:                                               ; preds = %28
  %33 = load i64, ptr %7, align 8, !tbaa !9
  %34 = call ptr @wal_file_allocate(i64 noundef %33)
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.WalCursor, ptr %35, i32 0, i32 1
  store ptr %34, ptr %36, align 8, !tbaa !11
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.WalCursor, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  call void @wal_file_open(ptr noundef %39)
  br label %40

40:                                               ; preds = %64, %32
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = call zeroext i1 @wal_cursor_next(ptr noundef %41)
  br i1 %42, label %44, label %43

43:                                               ; preds = %40
  br label %65

44:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = call ptr @wal_cursor_at(ptr noundef %45)
  store ptr %46, ptr %9, align 8, !tbaa !5
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.LogWrite, ptr %47, i32 0, i32 1
  %49 = load i64, ptr %48, align 1, !tbaa !16
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = icmp uge i64 %49, %50
  br i1 %51, label %52, label %61

52:                                               ; preds = %44
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.LogWrite, ptr %53, i32 0, i32 4
  %55 = load i32, ptr %54, align 1, !tbaa !19
  %56 = zext i32 %55 to i64
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.WalCursor, ptr %57, i32 0, i32 2
  %59 = load i64, ptr %58, align 8, !tbaa !14
  %60 = sub i64 %59, %56
  store i64 %60, ptr %58, align 8, !tbaa !14
  store i32 2, ptr %8, align 4
  br label %62

61:                                               ; preds = %44
  store i32 0, ptr %8, align 4
  br label %62

62:                                               ; preds = %61, %52
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %63 = load i32, ptr %8, align 4
  switch i32 %63, label %69 [
    i32 0, label %64
    i32 2, label %65
  ]

64:                                               ; preds = %62
  br label %40

65:                                               ; preds = %62, %43
  store i32 0, ptr %8, align 4
  br label %66

66:                                               ; preds = %65, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %67 = load i32, ptr %8, align 4
  switch i32 %67, label %69 [
    i32 0, label %68
    i32 1, label %68
  ]

68:                                               ; preds = %66, %66
  ret void

69:                                               ; preds = %66, %62
  unreachable
}

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @wal_id_find(ptr noundef, i64 noundef) #2

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

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @wal_cursor_next(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @wal_cursor_at(ptr noundef) #0

; 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 = { 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 #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, !6, i64 24}
!12 = !{!"WalCursor", !13, i64 0, !6, i64 24, !10, i64 32, !6, i64 40}
!13 = !{!"Buf", !6, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!12, !10, i64 32}
!15 = !{!12, !6, i64 40}
!16 = !{!17, !10, i64 4}
!17 = !{!"LogWrite", !18, i64 0, !10, i64 4, !7, i64 12, !18, i64 13, !18, i64 17}
!18 = !{!"int", !7, i64 0}
!19 = !{!17, !18, i64 17}


; 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 void @wal_cursor_open(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = getelementptr inbounds i8, ptr %0, i64 40
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %4, i8 0, i64 16, i1 false)
  store ptr %1, ptr %6, align 8, !tbaa !5
  %7 = icmp eq i64 %2, 0
  %8 = getelementptr inbounds i8, ptr %1, i64 40
  br i1 %7, label %9, label %11

9:                                                ; preds = %3
  %10 = tail call i64 @wal_id_min(ptr noundef nonnull %8)
  br label %13

11:                                               ; preds = %3
  %12 = tail call i64 @wal_id_find(ptr noundef nonnull %8, i64 noundef %2)
  br label %13

13:                                               ; preds = %11, %9
  %.0 = phi i64 [ %10, %9 ], [ %12, %11 ]
  %14 = icmp eq i64 %.0, -1
  br i1 %14, label %.loopexit, label %15

15:                                               ; preds = %13
  %16 = tail call ptr @wal_file_allocate(i64 noundef %.0)
  store ptr %16, ptr %4, align 8, !tbaa !12
  tail call void @wal_file_open(ptr noundef %16)
  br label %17

17:                                               ; preds = %19, %15
  %18 = tail call zeroext i1 @wal_cursor_next(ptr noundef %0)
  br i1 %18, label %19, label %.loopexit

19:                                               ; preds = %17
  %20 = tail call ptr @wal_cursor_at(ptr noundef %0)
  %21 = getelementptr inbounds i8, ptr %20, i64 4
  %22 = load i64, ptr %21, align 1, !tbaa !13
  %.not = icmp ult i64 %22, %2
  br i1 %.not, label %17, label %.thread

.thread:                                          ; preds = %19
  %23 = getelementptr inbounds i8, ptr %20, i64 17
  %24 = load i32, ptr %23, align 1, !tbaa !16
  %25 = zext i32 %24 to i64
  %26 = load i64, ptr %5, align 8, !tbaa !17
  %27 = sub i64 %26, %25
  store i64 %27, ptr %5, align 8, !tbaa !17
  br label %.loopexit

.loopexit:                                        ; preds = %17, %.thread, %13
  ret void
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @wal_id_find(ptr noundef, i64 noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @wal_cursor_next(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @wal_cursor_at(ptr noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }

!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 40}
!6 = !{!"WalCursor", !7, i64 0, !8, i64 24, !11, i64 32, !8, i64 40}
!7 = !{!"Buf", !8, i64 0, !8, i64 8, !8, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!6, !8, i64 24}
!13 = !{!14, !11, i64 4}
!14 = !{!"LogWrite", !15, i64 0, !11, i64 4, !9, i64 12, !15, i64 13, !15, i64 17}
!15 = !{!"int", !9, i64 0}
!16 = !{!14, !15, i64 17}
!17 = !{!6, !11, i64 32}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass