; LLVM IR from seppo0010/rlite :: src (scripting)

; 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"

@lua = external hidden global ptr, align 8

; 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 willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

declare ptr @lua_tolstring(ptr noundef, i32 noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
define dso_local ptr @luaReplyToStringReply(i32 noundef %0) #5 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %10 = load ptr, ptr @lua, align 8, !tbaa !9
  %11 = call ptr @lua_tolstring(ptr noundef %10, i32 noundef -1, ptr noundef null)
  store ptr %11, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = call i64 @strlen(ptr noundef %12) #7
  store i64 %13, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %14 = load i64, ptr %7, align 8, !tbaa !11
  %15 = add i64 %14, 1
  %16 = mul i64 1, %15
  %17 = call noalias ptr @malloc(i64 noundef %16) #8
  store ptr %17, ptr %8, align 8, !tbaa !9
  %18 = load ptr, ptr %8, align 8, !tbaa !9
  %19 = icmp ne ptr %18, null
  br i1 %19, label %21, label %20

20:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %67

21:                                               ; preds = %1
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %53, %21
  %23 = load i64, ptr %6, align 8, !tbaa !11
  %24 = load i64, ptr %7, align 8, !tbaa !11
  %25 = icmp ult i64 %23, %24
  br i1 %25, label %26, label %56

26:                                               ; preds = %22
  %27 = load ptr, ptr %5, align 8, !tbaa !9
  %28 = load i64, ptr %6, align 8, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = sext i8 %30 to i32
  %32 = icmp eq i32 %31, 13
  br i1 %32, label %40, label %33

33:                                               ; preds = %26
  %34 = load ptr, ptr %5, align 8, !tbaa !9
  %35 = load i64, ptr %6, align 8, !tbaa !11
  %36 = getelementptr inbounds i8, ptr %34, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !13
  %38 = sext i8 %37 to i32
  %39 = icmp eq i32 %38, 10
  br i1 %39, label %40, label %44

40:                                               ; preds = %33, %26
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = load i64, ptr %6, align 8, !tbaa !11
  %43 = getelementptr inbounds i8, ptr %41, i64 %42
  store i8 32, ptr %43, align 1, !tbaa !13
  br label %52

44:                                               ; preds = %33
  %45 = load ptr, ptr %5, align 8, !tbaa !9
  %46 = load i64, ptr %6, align 8, !tbaa !11
  %47 = getelementptr inbounds i8, ptr %45, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !13
  %49 = load ptr, ptr %8, align 8, !tbaa !9
  %50 = load i64, ptr %6, align 8, !tbaa !11
  %51 = getelementptr inbounds i8, ptr %49, i64 %50
  store i8 %48, ptr %51, align 1, !tbaa !13
  br label %52

52:                                               ; preds = %44, %40
  br label %53

53:                                               ; preds = %52
  %54 = load i64, ptr %6, align 8, !tbaa !11
  %55 = add i64 %54, 1
  store i64 %55, ptr %6, align 8, !tbaa !11
  br label %22, !llvm.loop !14

56:                                               ; preds = %22
  %57 = load ptr, ptr %8, align 8, !tbaa !9
  %58 = load i64, ptr %7, align 8, !tbaa !11
  %59 = getelementptr inbounds i8, ptr %57, i64 %58
  store i8 0, ptr %59, align 1, !tbaa !13
  %60 = load i32, ptr %3, align 4, !tbaa !5
  %61 = load ptr, ptr %8, align 8, !tbaa !9
  %62 = load i64, ptr %7, align 8, !tbaa !11
  %63 = trunc i64 %62 to i32
  %64 = call ptr @createStringTypeObject(i32 noundef %60, ptr noundef %61, i32 noundef %63)
  store ptr %64, ptr %4, align 8, !tbaa !9
  %65 = load ptr, ptr %8, align 8, !tbaa !9
  call void @free(ptr noundef %65) #6
  %66 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %66, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %67

67:                                               ; preds = %56, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  %68 = load ptr, ptr %2, align 8
  ret ptr %68
}

declare ptr @createStringTypeObject(i32 noundef, ptr noundef, i32 noundef) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind allocsize(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 "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 #4 = { "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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }
attributes #8 = { nounwind allocsize(0) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; 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"

@lua = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

declare ptr @lua_tolstring(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local ptr @luaReplyToStringReply(i32 noundef %0) local_unnamed_addr #4 {
  %2 = load ptr, ptr @lua, align 8, !tbaa !5
  %3 = tail call ptr @lua_tolstring(ptr noundef %2, i32 noundef -1, ptr noundef null) #5
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %3) #6
  %5 = add i64 %4, 1
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #7
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %38, label %.preheader

.preheader:                                       ; preds = %1
  %.not28 = icmp eq i64 %4, 0
  br i1 %.not28, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %xtraiter = and i64 %4, 3
  %7 = icmp ult i64 %4, 4
  br i1 %7, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %4, -4
  br label %.lr.ph

.lr.ph:                                           ; preds = %26, %.lr.ph.preheader.new
  %.02327 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %28, %26 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %26 ]
  %8 = getelementptr inbounds i8, ptr %3, i64 %.02327
  %9 = load i8, ptr %8, align 1, !tbaa !9
  switch i8 %9, label %10 [
    i8 13, label %.lr.ph.1
    i8 10, label %.lr.ph.1
  ]

10:                                               ; preds = %.lr.ph
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %.lr.ph, %10
  %.sink = phi i8 [ %9, %10 ], [ 32, %.lr.ph ], [ 32, %.lr.ph ]
  %11 = getelementptr inbounds i8, ptr %6, i64 %.02327
  store i8 %.sink, ptr %11, align 1, !tbaa !9
  %12 = or disjoint i64 %.02327, 1
  %13 = getelementptr inbounds i8, ptr %3, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !9
  switch i8 %14, label %15 [
    i8 13, label %.lr.ph.2
    i8 10, label %.lr.ph.2
  ]

15:                                               ; preds = %.lr.ph.1
  br label %.lr.ph.2

.lr.ph.2:                                         ; preds = %15, %.lr.ph.1, %.lr.ph.1
  %.sink.1 = phi i8 [ %14, %15 ], [ 32, %.lr.ph.1 ], [ 32, %.lr.ph.1 ]
  %16 = getelementptr inbounds i8, ptr %6, i64 %12
  store i8 %.sink.1, ptr %16, align 1, !tbaa !9
  %17 = or disjoint i64 %.02327, 2
  %18 = getelementptr inbounds i8, ptr %3, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !9
  switch i8 %19, label %20 [
    i8 13, label %.lr.ph.3
    i8 10, label %.lr.ph.3
  ]

20:                                               ; preds = %.lr.ph.2
  br label %.lr.ph.3

.lr.ph.3:                                         ; preds = %20, %.lr.ph.2, %.lr.ph.2
  %.sink.2 = phi i8 [ %19, %20 ], [ 32, %.lr.ph.2 ], [ 32, %.lr.ph.2 ]
  %21 = getelementptr inbounds i8, ptr %6, i64 %17
  store i8 %.sink.2, ptr %21, align 1, !tbaa !9
  %22 = or disjoint i64 %.02327, 3
  %23 = getelementptr inbounds i8, ptr %3, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  switch i8 %24, label %25 [
    i8 13, label %26
    i8 10, label %26
  ]

25:                                               ; preds = %.lr.ph.3
  br label %26

26:                                               ; preds = %25, %.lr.ph.3, %.lr.ph.3
  %.sink.3 = phi i8 [ %24, %25 ], [ 32, %.lr.ph.3 ], [ 32, %.lr.ph.3 ]
  %27 = getelementptr inbounds i8, ptr %6, i64 %22
  store i8 %.sink.3, ptr %27, align 1, !tbaa !9
  %28 = add nuw i64 %.02327, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10

._crit_edge.loopexit.unr-lcssa:                   ; preds = %26, %.lr.ph.preheader
  %.02327.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %28, %26 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %32
  %.02327.epil = phi i64 [ %34, %32 ], [ %.02327.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %32 ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %29 = getelementptr inbounds i8, ptr %3, i64 %.02327.epil
  %30 = load i8, ptr %29, align 1, !tbaa !9
  switch i8 %30, label %31 [
    i8 13, label %32
    i8 10, label %32
  ]

31:                                               ; preds = %.lr.ph.epil
  br label %32

32:                                               ; preds = %31, %.lr.ph.epil, %.lr.ph.epil
  %.sink.epil = phi i8 [ %30, %31 ], [ 32, %.lr.ph.epil ], [ 32, %.lr.ph.epil ]
  %33 = getelementptr inbounds i8, ptr %6, i64 %.02327.epil
  store i8 %.sink.epil, ptr %33, align 1, !tbaa !9
  %34 = add nuw i64 %.02327.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !12

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %32, %.preheader
  %35 = getelementptr inbounds i8, ptr %6, i64 %4
  store i8 0, ptr %35, align 1, !tbaa !9
  %36 = trunc i64 %4 to i32
  %37 = tail call ptr @createStringTypeObject(i32 noundef %0, ptr noundef nonnull %6, i32 noundef %36) #5
  tail call void @free(ptr noundef nonnull %6) #5
  br label %38

38:                                               ; preds = %1, %._crit_edge
  %.0 = phi ptr [ %37, %._crit_edge ], [ null, %1 ]
  ret ptr %.0
}

declare ptr @createStringTypeObject(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nofree nounwind willreturn memory(argmem: 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 #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { mustprogress nounwind willreturn allockind("free") 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 #3 = { "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 #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind allocsize(0) }

!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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}


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