; LLVM IR from ramsdell/lineqpp :: lua (lapi)

; 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.lua_State = type { ptr, i8, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i16, i16, i8, i8, i32, i32, ptr, %struct.lua_TValue, %struct.lua_TValue, ptr, ptr, ptr, i64 }
%struct.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }
%struct.global_State = type { %struct.stringtable, ptr, ptr, i8, i8, i32, ptr, ptr, ptr, ptr, ptr, ptr, %struct.Mbuffer, i64, i64, i64, i64, i32, i32, ptr, %struct.lua_TValue, ptr, %struct.UpVal, [9 x ptr], [17 x ptr] }
%struct.stringtable = type { ptr, i32, i32 }
%struct.Mbuffer = type { ptr, i64, i64 }
%struct.UpVal = type { ptr, i8, i8, ptr, %union.anon }
%union.anon = type { %struct.lua_TValue }

@.str.3 = external hidden unnamed_addr constant [1 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

declare hidden void @luaC_step(ptr noundef) #1

declare hidden ptr @luaS_newlstr(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @lua_concat(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = icmp sge i32 %7, 2
  br i1 %8, label %9, label %46

9:                                                ; preds = %2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.lua_State, ptr %10, i32 0, i32 6
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  %13 = getelementptr inbounds %struct.global_State, ptr %12, i32 0, i32 14
  %14 = load i64, ptr %13, align 8, !tbaa !16
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.lua_State, ptr %15, i32 0, i32 6
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.global_State, ptr %17, i32 0, i32 13
  %19 = load i64, ptr %18, align 8, !tbaa !21
  %20 = icmp uge i64 %14, %19
  br i1 %20, label %21, label %23

21:                                               ; preds = %9
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  call void @luaC_step(ptr noundef %22)
  br label %23

23:                                               ; preds = %21, %9
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = load i32, ptr %4, align 4, !tbaa !9
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.lua_State, ptr %26, i32 0, i32 4
  %28 = load ptr, ptr %27, align 8, !tbaa !22
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.lua_State, ptr %29, i32 0, i32 5
  %31 = load ptr, ptr %30, align 8, !tbaa !23
  %32 = ptrtoint ptr %28 to i64
  %33 = ptrtoint ptr %31 to i64
  %34 = sub i64 %32, %33
  %35 = sdiv exact i64 %34, 16
  %36 = trunc i64 %35 to i32
  %37 = sub nsw i32 %36, 1
  call void @luaV_concat(ptr noundef %24, i32 noundef %25, i32 noundef %37)
  %38 = load i32, ptr %4, align 4, !tbaa !9
  %39 = sub nsw i32 %38, 1
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.lua_State, ptr %40, i32 0, i32 4
  %42 = load ptr, ptr %41, align 8, !tbaa !22
  %43 = sext i32 %39 to i64
  %44 = sub i64 0, %43
  %45 = getelementptr inbounds %struct.lua_TValue, ptr %42, i64 %44
  store ptr %45, ptr %41, align 8, !tbaa !22
  br label %65

46:                                               ; preds = %2
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = icmp eq i32 %47, 0
  br i1 %48, label %49, label %64

49:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.lua_State, ptr %50, i32 0, i32 4
  %52 = load ptr, ptr %51, align 8, !tbaa !22
  store ptr %52, ptr %5, align 8, !tbaa !5
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = call ptr @luaS_newlstr(ptr noundef %53, ptr noundef @.str.3, i64 noundef 0)
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.lua_TValue, ptr %55, i32 0, i32 0
  store ptr %54, ptr %56, align 8, !tbaa !24
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.lua_TValue, ptr %57, i32 0, i32 1
  store i32 4, ptr %58, align 8, !tbaa !25
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.lua_State, ptr %60, i32 0, i32 4
  %62 = load ptr, ptr %61, align 8, !tbaa !22
  %63 = getelementptr inbounds %struct.lua_TValue, ptr %62, i32 1
  store ptr %63, ptr %61, align 8, !tbaa !22
  br label %64

64:                                               ; preds = %49, %46
  br label %65

65:                                               ; preds = %64, %23
  ret void
}

declare hidden void @luaV_concat(ptr noundef, i32 noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 32}
!12 = !{!"lua_State", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !10, i64 92, !13, i64 96, !13, i64 98, !7, i64 100, !7, i64 101, !10, i64 104, !10, i64 108, !6, i64 112, !14, i64 120, !14, i64 136, !6, i64 152, !6, i64 160, !6, i64 168, !15, i64 176}
!13 = !{!"short", !7, i64 0}
!14 = !{!"lua_TValue", !7, i64 0, !10, i64 8}
!15 = !{!"long", !7, i64 0}
!16 = !{!17, !15, i64 120}
!17 = !{!"global_State", !18, i64 0, !6, i64 16, !6, i64 24, !7, i64 32, !7, i64 33, !10, i64 36, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !19, i64 88, !15, i64 112, !15, i64 120, !15, i64 128, !15, i64 136, !10, i64 144, !10, i64 148, !6, i64 152, !14, i64 160, !6, i64 176, !20, i64 184, !7, i64 224, !7, i64 296}
!18 = !{!"stringtable", !6, i64 0, !10, i64 8, !10, i64 12}
!19 = !{!"Mbuffer", !6, i64 0, !15, i64 8, !15, i64 16}
!20 = !{!"UpVal", !6, i64 0, !7, i64 8, !7, i64 9, !6, i64 16, !7, i64 24}
!21 = !{!17, !15, i64 112}
!22 = !{!12, !6, i64 16}
!23 = !{!12, !6, i64 24}
!24 = !{!7, !7, i64 0}
!25 = !{!14, !10, i64 8}


; 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.lua_TValue = type { %union.Value, i32 }
%union.Value = type { ptr }

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

declare hidden void @luaC_step(ptr noundef) local_unnamed_addr #0

declare hidden ptr @luaS_newlstr(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @lua_concat(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp sgt i32 %1, 1
  br i1 %3, label %4, label %28

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 120
  %8 = load i64, ptr %7, align 8, !tbaa !14
  %9 = getelementptr inbounds i8, ptr %6, i64 112
  %10 = load i64, ptr %9, align 8, !tbaa !19
  %.not = icmp ult i64 %8, %10
  br i1 %.not, label %12, label %11

11:                                               ; preds = %4
  tail call void @luaC_step(ptr noundef nonnull %0) #2
  br label %12

12:                                               ; preds = %11, %4
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !20
  %15 = getelementptr inbounds i8, ptr %0, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !21
  %17 = ptrtoint ptr %14 to i64
  %18 = ptrtoint ptr %16 to i64
  %19 = sub i64 %17, %18
  %20 = lshr exact i64 %19, 4
  %21 = trunc i64 %20 to i32
  %22 = add nsw i32 %21, -1
  tail call void @luaV_concat(ptr noundef nonnull %0, i32 noundef %1, i32 noundef %22) #2
  %23 = add nsw i32 %1, -1
  %24 = load ptr, ptr %13, align 8, !tbaa !20
  %25 = zext nneg i32 %23 to i64
  %26 = sub nsw i64 0, %25
  %27 = getelementptr inbounds %struct.lua_TValue, ptr %24, i64 %26
  store ptr %27, ptr %13, align 8, !tbaa !20
  br label %37

28:                                               ; preds = %2
  %29 = icmp eq i32 %1, 0
  br i1 %29, label %30, label %37

30:                                               ; preds = %28
  %31 = getelementptr inbounds i8, ptr %0, i64 16
  %32 = load ptr, ptr %31, align 8, !tbaa !20
  %33 = tail call ptr @luaS_newlstr(ptr noundef %0, ptr noundef nonnull @.str.3, i64 noundef 0) #2
  store ptr %33, ptr %32, align 8, !tbaa !22
  %34 = getelementptr inbounds i8, ptr %32, i64 8
  store i32 4, ptr %34, align 8, !tbaa !23
  %35 = load ptr, ptr %31, align 8, !tbaa !20
  %36 = getelementptr inbounds i8, ptr %35, i64 16
  store ptr %36, ptr %31, align 8, !tbaa !20
  br label %37

37:                                               ; preds = %28, %30, %12
  ret void
}

declare hidden void @luaV_concat(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

attributes #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 = { 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 32}
!6 = !{!"lua_State", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !10, i64 92, !11, i64 96, !11, i64 98, !8, i64 100, !8, i64 101, !10, i64 104, !10, i64 108, !7, i64 112, !12, i64 120, !12, i64 136, !7, i64 152, !7, i64 160, !7, i64 168, !13, i64 176}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"lua_TValue", !8, i64 0, !10, i64 8}
!13 = !{!"long", !8, i64 0}
!14 = !{!15, !13, i64 120}
!15 = !{!"global_State", !16, i64 0, !7, i64 16, !7, i64 24, !8, i64 32, !8, i64 33, !10, i64 36, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !17, i64 88, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136, !10, i64 144, !10, i64 148, !7, i64 152, !12, i64 160, !7, i64 176, !18, i64 184, !8, i64 224, !8, i64 296}
!16 = !{!"stringtable", !7, i64 0, !10, i64 8, !10, i64 12}
!17 = !{!"Mbuffer", !7, i64 0, !13, i64 8, !13, i64 16}
!18 = !{!"UpVal", !7, i64 0, !8, i64 8, !8, i64 9, !7, i64 16, !8, i64 24}
!19 = !{!15, !13, i64 112}
!20 = !{!6, !7, i64 16}
!21 = !{!6, !7, i64 24}
!22 = !{!8, !8, i64 0}
!23 = !{!12, !10, i64 8}


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