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

; 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 }
%struct.anon.0 = type { ptr, i8, i8, i8, i32, i64 }
%union.TString = type { %struct.anon.0 }
%struct.GCheader = type { ptr, i8, 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 hidden ptr @luaS_newlstr(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %14 = load i64, ptr %7, align 8, !tbaa !9
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = lshr i64 %16, 5
  %18 = add i64 %17, 1
  store i64 %18, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %19 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %19, ptr %11, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %39, %3
  %21 = load i64, ptr %11, align 8, !tbaa !9
  %22 = load i64, ptr %10, align 8, !tbaa !9
  %23 = icmp uge i64 %21, %22
  br i1 %23, label %24, label %43

24:                                               ; preds = %20
  %25 = load i32, ptr %9, align 4, !tbaa !11
  %26 = load i32, ptr %9, align 4, !tbaa !11
  %27 = shl i32 %26, 5
  %28 = load i32, ptr %9, align 4, !tbaa !11
  %29 = lshr i32 %28, 2
  %30 = add i32 %27, %29
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load i64, ptr %11, align 8, !tbaa !9
  %33 = sub i64 %32, 1
  %34 = getelementptr inbounds i8, ptr %31, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !13
  %36 = zext i8 %35 to i32
  %37 = add i32 %30, %36
  %38 = xor i32 %25, %37
  store i32 %38, ptr %9, align 4, !tbaa !11
  br label %39

39:                                               ; preds = %24
  %40 = load i64, ptr %10, align 8, !tbaa !9
  %41 = load i64, ptr %11, align 8, !tbaa !9
  %42 = sub i64 %41, %40
  store i64 %42, ptr %11, align 8, !tbaa !9
  br label %20, !llvm.loop !14

43:                                               ; preds = %20
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.lua_State, ptr %44, i32 0, i32 6
  %46 = load ptr, ptr %45, align 8, !tbaa !16
  %47 = getelementptr inbounds %struct.global_State, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds %struct.stringtable, ptr %47, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !20
  %50 = load i32, ptr %9, align 4, !tbaa !11
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.lua_State, ptr %51, i32 0, i32 6
  %53 = load ptr, ptr %52, align 8, !tbaa !16
  %54 = getelementptr inbounds %struct.global_State, ptr %53, i32 0, i32 0
  %55 = getelementptr inbounds %struct.stringtable, ptr %54, i32 0, i32 2
  %56 = load i32, ptr %55, align 4, !tbaa !25
  %57 = sub nsw i32 %56, 1
  %58 = and i32 %50, %57
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds ptr, ptr %49, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  store ptr %61, ptr %8, align 8, !tbaa !5
  br label %62

62:                                               ; preds = %107, %43
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = icmp ne ptr %63, null
  br i1 %64, label %65, label %111

65:                                               ; preds = %62
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %66, ptr %12, align 8, !tbaa !5
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.anon.0, ptr %67, i32 0, i32 5
  %69 = load i64, ptr %68, align 8, !tbaa !13
  %70 = load i64, ptr %7, align 8, !tbaa !9
  %71 = icmp eq i64 %69, %70
  br i1 %71, label %72, label %103

72:                                               ; preds = %65
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = load ptr, ptr %12, align 8, !tbaa !5
  %75 = getelementptr inbounds %union.TString, ptr %74, i64 1
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = call i32 @memcmp(ptr noundef %73, ptr noundef %75, i64 noundef %76) #4
  %78 = icmp eq i32 %77, 0
  br i1 %78, label %79, label %103

79:                                               ; preds = %72
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.GCheader, ptr %80, i32 0, i32 2
  %82 = load i8, ptr %81, align 1, !tbaa !13
  %83 = zext i8 %82 to i32
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.lua_State, ptr %84, i32 0, i32 6
  %86 = load ptr, ptr %85, align 8, !tbaa !16
  %87 = getelementptr inbounds %struct.global_State, ptr %86, i32 0, i32 3
  %88 = load i8, ptr %87, align 8, !tbaa !26
  %89 = zext i8 %88 to i32
  %90 = xor i32 %89, 3
  %91 = and i32 %83, %90
  %92 = and i32 %91, 3
  %93 = icmp ne i32 %92, 0
  br i1 %93, label %94, label %101

94:                                               ; preds = %79
  %95 = load ptr, ptr %8, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.GCheader, ptr %95, i32 0, i32 2
  %97 = load i8, ptr %96, align 1, !tbaa !13
  %98 = zext i8 %97 to i32
  %99 = xor i32 %98, 3
  %100 = trunc i32 %99 to i8
  store i8 %100, ptr %96, align 1, !tbaa !13
  br label %101

101:                                              ; preds = %94, %79
  %102 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %102, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %104

103:                                              ; preds = %72, %65
  store i32 0, ptr %13, align 4
  br label %104

104:                                              ; preds = %103, %101
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %105 = load i32, ptr %13, align 4
  switch i32 %105, label %117 [
    i32 0, label %106
  ]

106:                                              ; preds = %104
  br label %107

107:                                              ; preds = %106
  %108 = load ptr, ptr %8, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.GCheader, ptr %108, i32 0, i32 0
  %110 = load ptr, ptr %109, align 8, !tbaa !13
  store ptr %110, ptr %8, align 8, !tbaa !5
  br label %62, !llvm.loop !27

111:                                              ; preds = %62
  %112 = load ptr, ptr %5, align 8, !tbaa !5
  %113 = load ptr, ptr %6, align 8, !tbaa !5
  %114 = load i64, ptr %7, align 8, !tbaa !9
  %115 = load i32, ptr %9, align 4, !tbaa !11
  %116 = call ptr @newlstr(ptr noundef %112, ptr noundef %113, i64 noundef %114, i32 noundef %115)
  store ptr %116, ptr %4, align 8
  store i32 1, ptr %13, align 4
  br label %117

117:                                              ; preds = %111, %104
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %118 = load ptr, ptr %4, align 8
  ret ptr %118
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #2

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

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 willreturn memory(read) "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 willreturn memory(read) }

!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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !6, i64 32}
!17 = !{!"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, !12, i64 88, !12, i64 92, !18, i64 96, !18, i64 98, !7, i64 100, !7, i64 101, !12, i64 104, !12, i64 108, !6, i64 112, !19, i64 120, !19, i64 136, !6, i64 152, !6, i64 160, !6, i64 168, !10, i64 176}
!18 = !{!"short", !7, i64 0}
!19 = !{!"lua_TValue", !7, i64 0, !12, i64 8}
!20 = !{!21, !6, i64 0}
!21 = !{!"global_State", !22, i64 0, !6, i64 16, !6, i64 24, !7, i64 32, !7, i64 33, !12, i64 36, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !23, i64 88, !10, i64 112, !10, i64 120, !10, i64 128, !10, i64 136, !12, i64 144, !12, i64 148, !6, i64 152, !19, i64 160, !6, i64 176, !24, i64 184, !7, i64 224, !7, i64 296}
!22 = !{!"stringtable", !6, i64 0, !12, i64 8, !12, i64 12}
!23 = !{!"Mbuffer", !6, i64 0, !10, i64 8, !10, i64 16}
!24 = !{!"UpVal", !6, i64 0, !7, i64 8, !7, i64 9, !6, i64 16, !7, i64 24}
!25 = !{!21, !12, i64 12}
!26 = !{!21, !7, i64 32}
!27 = distinct !{!27, !15}


; 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 hidden ptr @luaS_newlstr(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = lshr i64 %2, 5
  %.neg = xor i64 %4, -1
  %invariant.gep = getelementptr i8, ptr %1, i64 -1
  %.not.not42.not = icmp eq i64 %2, 0
  br i1 %.not.not42.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %5 = trunc i64 %2 to i32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.03344 = phi i64 [ %13, %.lr.ph ], [ %2, %.lr.ph.preheader ]
  %.03543 = phi i32 [ %12, %.lr.ph ], [ %5, %.lr.ph.preheader ]
  %6 = shl i32 %.03543, 5
  %7 = lshr i32 %.03543, 2
  %8 = add i32 %6, %7
  %gep = getelementptr i8, ptr %invariant.gep, i64 %.03344
  %9 = load i8, ptr %gep, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = add i32 %8, %10
  %12 = xor i32 %11, %.03543
  %13 = add i64 %.03344, %.neg
  %.not.not = icmp ugt i64 %13, %4
  br i1 %.not.not, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %3
  %.035.lcssa = phi i32 [ 0, %3 ], [ %12, %.lr.ph ]
  %14 = getelementptr inbounds i8, ptr %0, i64 32
  %15 = load ptr, ptr %14, align 8, !tbaa !10
  %16 = load ptr, ptr %15, align 8, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %15, i64 12
  %18 = load i32, ptr %17, align 4, !tbaa !22
  %19 = add nsw i32 %18, -1
  %20 = and i32 %19, %.035.lcssa
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds ptr, ptr %16, i64 %21
  %.03445 = load ptr, ptr %22, align 8, !tbaa !5
  %.not46 = icmp eq ptr %.03445, null
  br i1 %.not46, label %._crit_edge50, label %.lr.ph49

.lr.ph49:                                         ; preds = %._crit_edge, %39
  %.03447 = phi ptr [ %.034, %39 ], [ %.03445, %._crit_edge ]
  %23 = getelementptr inbounds i8, ptr %.03447, i64 16
  %24 = load i64, ptr %23, align 8, !tbaa !5
  %25 = icmp eq i64 %24, %2
  br i1 %25, label %26, label %39

26:                                               ; preds = %.lr.ph49
  %27 = getelementptr inbounds i8, ptr %.03447, i64 24
  %bcmp = tail call i32 @bcmp(ptr %1, ptr nonnull %27, i64 %2)
  %28 = icmp eq i32 %bcmp, 0
  br i1 %28, label %29, label %39

29:                                               ; preds = %26
  %30 = getelementptr inbounds i8, ptr %.03447, i64 9
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %15, i64 32
  %33 = load i8, ptr %32, align 8, !tbaa !23
  %34 = xor i8 %33, -1
  %35 = and i8 %31, 3
  %36 = and i8 %35, %34
  %.not38 = icmp eq i8 %36, 0
  br i1 %.not38, label %.thread, label %37

37:                                               ; preds = %29
  %38 = xor i8 %31, 3
  store i8 %38, ptr %30, align 1, !tbaa !5
  br label %.thread

39:                                               ; preds = %.lr.ph49, %26
  %.034 = load ptr, ptr %.03447, align 8, !tbaa !5
  %.not = icmp eq ptr %.034, null
  br i1 %.not, label %._crit_edge50, label %.lr.ph49, !llvm.loop !24

._crit_edge50:                                    ; preds = %39, %._crit_edge
  %40 = tail call ptr @newlstr(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %.035.lcssa)
  br label %.thread

.thread:                                          ; preds = %29, %37, %._crit_edge50
  %.2 = phi ptr [ %40, %._crit_edge50 ], [ %.03447, %37 ], [ %.03447, %29 ]
  ret ptr %.2
}

; Function Attrs: nounwind uwtable
declare hidden ptr @newlstr(ptr noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) 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 = { nofree nounwind willreturn memory(argmem: read) }

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


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