; LLVM IR from tezc/sc :: buffer (sc_buf)

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

; 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
define dso_local zeroext i1 @sc_buf_reserve(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.sc_buf, ptr %9, i32 0, i32 4
  %11 = load i64, ptr %10, align 8, !tbaa !11
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = add i64 %11, %12
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.sc_buf, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !15
  %17 = icmp ugt i64 %13, %16
  br i1 %17, label %18, label %60

18:                                               ; preds = %2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.sc_buf, ptr %19, i32 0, i32 6
  %21 = load i8, ptr %20, align 4, !tbaa !16, !range !17, !noundef !18
  %22 = trunc i8 %21 to i1
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  br label %61

24:                                               ; preds = %18
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.sc_buf, ptr %25, i32 0, i32 1
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = load i64, ptr %5, align 8, !tbaa !9
  %29 = add i64 %27, %28
  %30 = add i64 %29, 4095
  %31 = udiv i64 %30, 4096
  %32 = mul i64 %31, 4096
  store i64 %32, ptr %6, align 8, !tbaa !9
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.sc_buf, ptr %34, i32 0, i32 2
  %36 = load i64, ptr %35, align 8, !tbaa !19
  %37 = icmp ugt i64 %33, %36
  br i1 %37, label %43, label %38

38:                                               ; preds = %24
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.sc_buf, ptr %39, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !15
  %42 = icmp uge i64 %41, -8193
  br i1 %42, label %43, label %44

43:                                               ; preds = %38, %24
  br label %61

44:                                               ; preds = %38
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.sc_buf, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !20
  %48 = load i64, ptr %6, align 8, !tbaa !9
  %49 = call ptr @realloc(ptr noundef %47, i64 noundef %48) #4
  store ptr %49, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = icmp eq ptr %50, null
  br i1 %51, label %52, label %53

52:                                               ; preds = %44
  br label %61

53:                                               ; preds = %44
  %54 = load i64, ptr %6, align 8, !tbaa !9
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.sc_buf, ptr %55, i32 0, i32 1
  store i64 %54, ptr %56, align 8, !tbaa !15
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.sc_buf, ptr %58, i32 0, i32 0
  store ptr %57, ptr %59, align 8, !tbaa !20
  br label %60

60:                                               ; preds = %53, %2
  store i1 true, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %66

61:                                               ; preds = %52, %43, %23
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.sc_buf, ptr %62, i32 0, i32 5
  %64 = load i32, ptr %63, align 8, !tbaa !21
  %65 = or i32 %64, 3
  store i32 %65, ptr %63, align 8, !tbaa !21
  store i1 false, ptr %3, align 1
  store i32 1, ptr %8, align 4
  br label %66

66:                                               ; preds = %61, %60
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %67 = load i1, ptr %3, align 1
  ret i1 %67
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #2

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 = { nounwind allocsize(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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(1) }

!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, !10, i64 32}
!12 = !{!"sc_buf", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !13, i64 40, !14, i64 44}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !14, i64 44}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = !{!12, !10, i64 16}
!20 = !{!12, !6, i64 0}
!21 = !{!12, !13, i64 40}


; 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: mustprogress nounwind willreturn uwtable
define dso_local noundef zeroext i1 @sc_buf_reserve(ptr nocapture noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 32
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = add i64 %4, %1
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !13
  %8 = icmp ugt i64 %5, %7
  br i1 %8, label %9, label %30

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 44
  %11 = load i8, ptr %10, align 4, !tbaa !14, !range !15, !noundef !16
  %12 = trunc nuw i8 %11 to i1
  br i1 %12, label %26, label %13

13:                                               ; preds = %9
  %14 = add i64 %1, 4095
  %15 = add i64 %14, %7
  %16 = and i64 %15, -4096
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = icmp ugt i64 %16, %18
  %20 = icmp ugt i64 %7, -8194
  %or.cond = or i1 %20, %19
  br i1 %or.cond, label %26, label %21

21:                                               ; preds = %13
  %22 = load ptr, ptr %0, align 8, !tbaa !18
  %23 = tail call ptr @realloc(ptr noundef %22, i64 noundef %16) #2
  %24 = icmp eq ptr %23, null
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i64 %16, ptr %6, align 8, !tbaa !13
  store ptr %23, ptr %0, align 8, !tbaa !18
  br label %30

26:                                               ; preds = %21, %13, %9
  %27 = getelementptr inbounds i8, ptr %0, i64 40
  %28 = load i32, ptr %27, align 8, !tbaa !19
  %29 = or i32 %28, 3
  store i32 %29, ptr %27, align 8, !tbaa !19
  br label %30

30:                                               ; preds = %2, %25, %26
  %.0 = phi i1 [ false, %26 ], [ true, %25 ], [ true, %2 ]
  ret i1 %.0
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nounwind willreturn 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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(1) }

!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, !10, i64 32}
!6 = !{!"sc_buf", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !11, i64 40, !12, i64 44}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!6, !12, i64 44}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!6, !10, i64 16}
!18 = !{!6, !7, i64 0}
!19 = !{!6, !11, i64 40}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass