; LLVM IR from ejoy/ejoy2d :: lua (lauxlib)

; 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.luaL_Buffer = type { ptr, i64, i64, ptr, [8192 x i8] }

@.str.24 = external hidden unnamed_addr constant [17 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 i32 @luaL_error(ptr noundef, ptr noundef, ...) #1

declare void @lua_settop(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @luaL_prepbuffsize(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  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) #4
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.luaL_Buffer, ptr %8, i32 0, i32 3
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  store ptr %10, ptr %5, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.luaL_Buffer, ptr %11, i32 0, i32 1
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.luaL_Buffer, ptr %14, i32 0, i32 2
  %16 = load i64, ptr %15, align 8, !tbaa !14
  %17 = sub i64 %13, %16
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = icmp ult i64 %17, %18
  br i1 %19, label %20, label %85

20:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.luaL_Buffer, ptr %21, i32 0, i32 1
  %23 = load i64, ptr %22, align 8, !tbaa !13
  %24 = mul i64 %23, 2
  store i64 %24, ptr %7, align 8, !tbaa !9
  %25 = load i64, ptr %7, align 8, !tbaa !9
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.luaL_Buffer, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = sub i64 %25, %28
  %30 = load i64, ptr %4, align 8, !tbaa !9
  %31 = icmp ult i64 %29, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %20
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.luaL_Buffer, ptr %33, i32 0, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !14
  %36 = load i64, ptr %4, align 8, !tbaa !9
  %37 = add i64 %35, %36
  store i64 %37, ptr %7, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %32, %20
  %39 = load i64, ptr %7, align 8, !tbaa !9
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.luaL_Buffer, ptr %40, i32 0, i32 2
  %42 = load i64, ptr %41, align 8, !tbaa !14
  %43 = icmp ult i64 %39, %42
  br i1 %43, label %52, label %44

44:                                               ; preds = %38
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.luaL_Buffer, ptr %46, i32 0, i32 2
  %48 = load i64, ptr %47, align 8, !tbaa !14
  %49 = sub i64 %45, %48
  %50 = load i64, ptr %4, align 8, !tbaa !9
  %51 = icmp ult i64 %49, %50
  br i1 %51, label %52, label %55

52:                                               ; preds = %44, %38
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = call i32 (ptr, ptr, ...) @luaL_error(ptr noundef %53, ptr noundef @.str.24)
  br label %55

55:                                               ; preds = %52, %44
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = load i64, ptr %7, align 8, !tbaa !9
  %58 = mul i64 %57, 1
  %59 = call ptr @lua_newuserdata(ptr noundef %56, i64 noundef %58)
  store ptr %59, ptr %6, align 8, !tbaa !5
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.luaL_Buffer, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !15
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.luaL_Buffer, ptr %64, i32 0, i32 2
  %66 = load i64, ptr %65, align 8, !tbaa !14
  %67 = mul i64 %66, 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %60, ptr align 1 %63, i64 %67, i1 false)
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.luaL_Buffer, ptr %68, i32 0, i32 0
  %70 = load ptr, ptr %69, align 8, !tbaa !15
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.luaL_Buffer, ptr %71, i32 0, i32 4
  %73 = getelementptr inbounds [8192 x i8], ptr %72, i64 0, i64 0
  %74 = icmp ne ptr %70, %73
  br i1 %74, label %75, label %78

75:                                               ; preds = %55
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lua_rotate(ptr noundef %76, i32 noundef -2, i32 noundef -1)
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  call void @lua_settop(ptr noundef %77, i32 noundef -2)
  br label %78

78:                                               ; preds = %75, %55
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.luaL_Buffer, ptr %80, i32 0, i32 0
  store ptr %79, ptr %81, align 8, !tbaa !15
  %82 = load i64, ptr %7, align 8, !tbaa !9
  %83 = load ptr, ptr %3, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.luaL_Buffer, ptr %83, i32 0, i32 1
  store i64 %82, ptr %84, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %85

85:                                               ; preds = %78, %2
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.luaL_Buffer, ptr %86, i32 0, i32 0
  %88 = load ptr, ptr %87, align 8, !tbaa !15
  %89 = load ptr, ptr %3, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.luaL_Buffer, ptr %89, i32 0, i32 2
  %91 = load i64, ptr %90, align 8, !tbaa !14
  %92 = getelementptr inbounds i8, ptr %88, i64 %91
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret ptr %92
}

declare ptr @lua_newuserdata(ptr noundef, i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

declare void @lua_rotate(ptr noundef, i32 noundef, i32 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 = { "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 nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"luaL_Buffer", !6, i64 0, !10, i64 8, !10, i64 16, !6, i64 24, !7, i64 32}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = !{!12, !6, 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"

@.str.24 = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @luaL_error(ptr noundef, ptr noundef, ...) local_unnamed_addr #0

declare void @lua_settop(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @luaL_prepbuffsize(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load i64, ptr %7, align 8, !tbaa !12
  %9 = sub i64 %6, %8
  %10 = icmp ult i64 %9, %1
  br i1 %10, label %11, label %._crit_edge

._crit_edge:                                      ; preds = %2
  %.pre = load ptr, ptr %0, align 8, !tbaa !13
  br label %29

11:                                               ; preds = %2
  %12 = shl i64 %6, 1
  %13 = sub i64 %12, %8
  %14 = icmp ult i64 %13, %1
  %15 = add i64 %8, %1
  %spec.select = select i1 %14, i64 %15, i64 %12
  %16 = icmp ult i64 %spec.select, %8
  %17 = sub i64 %spec.select, %8
  %18 = icmp ult i64 %17, %1
  %or.cond = or i1 %16, %18
  br i1 %or.cond, label %19, label %21

19:                                               ; preds = %11
  %20 = tail call i32 (ptr, ptr, ...) @luaL_error(ptr noundef %4, ptr noundef nonnull @.str.24)
  br label %21

21:                                               ; preds = %11, %19
  %22 = tail call ptr @lua_newuserdata(ptr noundef %4, i64 noundef %spec.select) #3
  %23 = load ptr, ptr %0, align 8, !tbaa !13
  %24 = load i64, ptr %7, align 8, !tbaa !12
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %22, ptr align 1 %23, i64 %24, i1 false)
  %25 = load ptr, ptr %0, align 8, !tbaa !13
  %26 = getelementptr inbounds i8, ptr %0, i64 32
  %.not = icmp eq ptr %25, %26
  br i1 %.not, label %28, label %27

27:                                               ; preds = %21
  tail call void @lua_rotate(ptr noundef %4, i32 noundef -2, i32 noundef -1) #3
  tail call void @lua_settop(ptr noundef %4, i32 noundef -2) #3
  br label %28

28:                                               ; preds = %27, %21
  store ptr %22, ptr %0, align 8, !tbaa !13
  store i64 %spec.select, ptr %5, align 8, !tbaa !11
  %.pre33 = load i64, ptr %7, align 8, !tbaa !12
  br label %29

29:                                               ; preds = %._crit_edge, %28
  %30 = phi i64 [ %8, %._crit_edge ], [ %.pre33, %28 ]
  %31 = phi ptr [ %.pre, %._crit_edge ], [ %22, %28 ]
  %32 = getelementptr inbounds i8, ptr %31, i64 %30
  ret ptr %32
}

declare ptr @lua_newuserdata(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare void @lua_rotate(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #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 = { "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 nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 24}
!6 = !{!"luaL_Buffer", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !8, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 16}
!13 = !{!6, !7, i64 0}


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