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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @wal_cursor_next(ptr noundef %0) #2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.WalCursor, ptr %9, i32 0, i32 1
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  %12 = icmp eq ptr %11, null
  %13 = xor i1 %12, true
  %14 = xor i1 %13, true
  %15 = zext i1 %14 to i32
  %16 = sext i32 %15 to i64
  %17 = call i64 @llvm.expect.i64(i64 %16, i64 0)
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %1
  store i1 false, ptr %2, align 1
  br label %76

20:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.WalCursor, ptr %21, i32 0, i32 3
  %23 = load ptr, ptr %22, align 8, !tbaa !13
  store ptr %23, ptr %4, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %73, %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.WalCursor, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !9
  store ptr %27, ptr %5, align 8, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.WalCursor, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !14
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.WalCursor, ptr %32, i32 0, i32 0
  %34 = call zeroext i1 @wal_file_pread(ptr noundef %28, i64 noundef %31, ptr noundef %33)
  br i1 %34, label %35, label %46

35:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = call ptr @wal_cursor_at(ptr noundef %36)
  store ptr %37, ptr %6, align 8, !tbaa !5
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.LogWrite, ptr %38, i32 0, i32 4
  %40 = load i32, ptr %39, align 1, !tbaa !15
  %41 = zext i32 %40 to i64
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.WalCursor, ptr %42, i32 0, i32 2
  %44 = load i64, ptr %43, align 8, !tbaa !14
  %45 = add i64 %44, %41
  store i64 %45, ptr %43, align 8, !tbaa !14
  store i1 true, ptr %2, align 1
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %71

46:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.Wal, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.WalFile, ptr %49, i32 0, i32 0
  %51 = load i64, ptr %50, align 8, !tbaa !18
  %52 = call i64 @wal_id_next(ptr noundef %48, i64 noundef %51)
  store i64 %52, ptr %8, align 8, !tbaa !24
  %53 = load i64, ptr %8, align 8, !tbaa !24
  %54 = icmp eq i64 %53, -1
  br i1 %54, label %55, label %56

55:                                               ; preds = %46
  store i32 2, ptr %7, align 4
  br label %70

56:                                               ; preds = %46
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  call void @wal_file_close(ptr noundef %57)
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  call void @wal_file_free(ptr noundef %58)
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.WalCursor, ptr %59, i32 0, i32 2
  store i64 0, ptr %60, align 8, !tbaa !14
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.WalCursor, ptr %61, i32 0, i32 1
  store ptr null, ptr %62, align 8, !tbaa !9
  %63 = load i64, ptr %8, align 8, !tbaa !24
  %64 = call ptr @wal_file_allocate(i64 noundef %63)
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.WalCursor, ptr %65, i32 0, i32 1
  store ptr %64, ptr %66, align 8, !tbaa !9
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.WalCursor, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !9
  call void @wal_file_open(ptr noundef %69)
  store i32 0, ptr %7, align 4
  br label %70

70:                                               ; preds = %56, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %71

71:                                               ; preds = %70, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  %72 = load i32, ptr %7, align 4
  switch i32 %72, label %75 [
    i32 0, label %73
    i32 2, label %74
  ]

73:                                               ; preds = %71
  br label %24

74:                                               ; preds = %71
  store i1 false, ptr %2, align 1
  store i32 1, ptr %7, align 4
  br label %75

75:                                               ; preds = %74, %71
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %76

76:                                               ; preds = %75, %19
  %77 = load i1, ptr %2, align 1
  ret i1 %77
}

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @wal_file_pread(ptr noundef, i64 noundef, ptr noundef) #1

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #4 = { 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 24}
!10 = !{!"WalCursor", !11, i64 0, !6, i64 24, !12, i64 32, !6, i64 40}
!11 = !{!"Buf", !6, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !6, i64 40}
!14 = !{!10, !12, i64 32}
!15 = !{!16, !17, i64 17}
!16 = !{!"LogWrite", !17, i64 0, !12, i64 4, !7, i64 12, !17, i64 13, !17, i64 17}
!17 = !{!"int", !7, i64 0}
!18 = !{!19, !12, i64 0}
!19 = !{!"WalFile", !12, i64 0, !20, i64 8}
!20 = !{!"File", !17, i64 0, !12, i64 8, !21, i64 16, !23, i64 40}
!21 = !{!"Str", !6, i64 0, !6, i64 8, !22, i64 16}
!22 = !{!"_Bool", !7, i64 0}
!23 = !{!"List", !6, i64 0, !6, i64 8}
!24 = !{!12, !12, i64 0}


; 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: inlinehint nounwind uwtable
declare hidden ptr @wal_file_allocate(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @wal_cursor_next(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %.loopexit, label %5, !prof !12

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %0, i64 40
  %7 = load ptr, ptr %6, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %0, i64 32
  %9 = load i64, ptr %8, align 8, !tbaa !14
  %10 = tail call zeroext i1 @wal_file_pread(ptr noundef nonnull %3, i64 noundef %9, ptr noundef nonnull %0)
  br i1 %10, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %11 = getelementptr inbounds i8, ptr %7, i64 40
  br label %18

.thread:                                          ; preds = %23, %5
  %12 = tail call ptr @wal_cursor_at(ptr noundef nonnull %0)
  %13 = getelementptr inbounds i8, ptr %12, i64 17
  %14 = load i32, ptr %13, align 1, !tbaa !15
  %15 = zext i32 %14 to i64
  %16 = load i64, ptr %8, align 8, !tbaa !14
  %17 = add i64 %16, %15
  store i64 %17, ptr %8, align 8, !tbaa !14
  br label %.loopexit

18:                                               ; preds = %.lr.ph, %23
  %19 = phi ptr [ %3, %.lr.ph ], [ %25, %23 ]
  %20 = load i64, ptr %19, align 8, !tbaa !18
  %21 = tail call i64 @wal_id_next(ptr noundef nonnull %11, i64 noundef %20)
  %22 = icmp eq i64 %21, -1
  br i1 %22, label %.loopexit, label %23

23:                                               ; preds = %18
  tail call void @wal_file_close(ptr noundef nonnull %19)
  tail call void @wal_file_free(ptr noundef nonnull %19)
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %2, i8 0, i64 16, i1 false)
  %24 = tail call ptr @wal_file_allocate(i64 noundef %21)
  store ptr %24, ptr %2, align 8, !tbaa !5
  tail call void @wal_file_open(ptr noundef %24)
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = load i64, ptr %8, align 8, !tbaa !14
  %27 = tail call zeroext i1 @wal_file_pread(ptr noundef %25, i64 noundef %26, ptr noundef nonnull %0)
  br i1 %27, label %.thread, label %18

.loopexit:                                        ; preds = %18, %.thread, %1
  %.3 = phi i1 [ false, %1 ], [ true, %.thread ], [ false, %18 ]
  ret i1 %.3
}

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

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

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @wal_file_pread(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @wal_id_next(ptr noundef, i64 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 = { 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 #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 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 24}
!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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!13 = !{!6, !8, i64 40}
!14 = !{!6, !11, i64 32}
!15 = !{!16, !17, i64 17}
!16 = !{!"LogWrite", !17, i64 0, !11, i64 4, !9, i64 12, !17, i64 13, !17, i64 17}
!17 = !{!"int", !9, i64 0}
!18 = !{!19, !11, i64 0}
!19 = !{!"WalFile", !11, i64 0, !20, i64 8}
!20 = !{!"File", !17, i64 0, !11, i64 8, !21, i64 16, !23, i64 40}
!21 = !{!"Str", !8, i64 0, !8, i64 8, !22, i64 16}
!22 = !{!"_Bool", !9, i64 0}
!23 = !{!"List", !8, i64 0, !8, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LowerExpectIntrinsicPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass