; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_ctype)

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

; 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 ptr @lj_str_new(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define hidden ptr @lj_ctype_repr_int64(ptr noundef %0, i64 noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca [24 x i8], align 16
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = getelementptr inbounds [24 x i8], ptr %7, i64 0, i64 0
  %11 = getelementptr inbounds i8, ptr %10, i64 24
  store ptr %11, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !11
  %12 = load ptr, ptr %8, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i32 -1
  store ptr %13, ptr %8, align 8, !tbaa !5
  store i8 76, ptr %13, align 1, !tbaa !13
  %14 = load ptr, ptr %8, align 8, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %14, i32 -1
  store ptr %15, ptr %8, align 8, !tbaa !5
  store i8 76, ptr %15, align 1, !tbaa !13
  %16 = load i32, ptr %6, align 4, !tbaa !11
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %3
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i32 -1
  store ptr %20, ptr %8, align 8, !tbaa !5
  store i8 85, ptr %20, align 1, !tbaa !13
  br label %28

21:                                               ; preds = %3
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = icmp slt i64 %22, 0
  br i1 %23, label %24, label %27

24:                                               ; preds = %21
  %25 = load i64, ptr %5, align 8, !tbaa !9
  %26 = sub nsw i64 0, %25
  store i64 %26, ptr %5, align 8, !tbaa !9
  store i32 1, ptr %9, align 4, !tbaa !11
  br label %27

27:                                               ; preds = %24, %21
  br label %28

28:                                               ; preds = %27, %18
  br label %29

29:                                               ; preds = %36, %28
  %30 = load i64, ptr %5, align 8, !tbaa !9
  %31 = urem i64 %30, 10
  %32 = add i64 48, %31
  %33 = trunc i64 %32 to i8
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %34, i32 -1
  store ptr %35, ptr %8, align 8, !tbaa !5
  store i8 %33, ptr %35, align 1, !tbaa !13
  br label %36

36:                                               ; preds = %29
  %37 = load i64, ptr %5, align 8, !tbaa !9
  %38 = udiv i64 %37, 10
  store i64 %38, ptr %5, align 8, !tbaa !9
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %29, label %40, !llvm.loop !14

40:                                               ; preds = %36
  %41 = load i32, ptr %9, align 4, !tbaa !11
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %46

43:                                               ; preds = %40
  %44 = load ptr, ptr %8, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i32 -1
  store ptr %45, ptr %8, align 8, !tbaa !5
  store i8 45, ptr %45, align 1, !tbaa !13
  br label %46

46:                                               ; preds = %43, %40
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds [24 x i8], ptr %7, i64 0, i64 0
  %50 = getelementptr inbounds i8, ptr %49, i64 24
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = ptrtoint ptr %50 to i64
  %53 = ptrtoint ptr %51 to i64
  %54 = sub i64 %52, %53
  %55 = call ptr @lj_str_new(ptr noundef %47, ptr noundef %48, i64 noundef %54)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #3
  ret ptr %55
}

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 = !{!"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"}


; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare hidden ptr @lj_str_new(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define hidden ptr @lj_ctype_repr_int64(ptr noundef %0, i64 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = alloca [24 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #4
  %5 = getelementptr inbounds i8, ptr %4, i64 23
  store i8 76, ptr %5, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %4, i64 22
  store i8 76, ptr %6, align 2, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %9, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %4, i64 21
  store i8 85, ptr %8, align 1, !tbaa !5
  br label %11

9:                                                ; preds = %3
  %10 = icmp sgt i64 %1, -1
  %spec.select = tail call i64 @llvm.abs.i64(i64 %1, i1 true)
  br label %11

11:                                               ; preds = %9, %7
  %.014 = phi i64 [ %1, %7 ], [ %spec.select, %9 ]
  %.013 = phi ptr [ %8, %7 ], [ %6, %9 ]
  %.not19 = phi i1 [ true, %7 ], [ %10, %9 ]
  br label %12

12:                                               ; preds = %12, %11
  %.115 = phi i64 [ %.014, %11 ], [ %17, %12 ]
  %.1 = phi ptr [ %.013, %11 ], [ %16, %12 ]
  %13 = urem i64 %.115, 10
  %14 = trunc nuw nsw i64 %13 to i8
  %15 = or disjoint i8 %14, 48
  %16 = getelementptr inbounds i8, ptr %.1, i64 -1
  store i8 %15, ptr %16, align 1, !tbaa !5
  %17 = udiv i64 %.115, 10
  %.not18 = icmp ult i64 %.115, 10
  br i1 %.not18, label %18, label %12, !llvm.loop !8

18:                                               ; preds = %12
  br i1 %.not19, label %21, label %19

19:                                               ; preds = %18
  %20 = getelementptr inbounds i8, ptr %.1, i64 -2
  store i8 45, ptr %20, align 1, !tbaa !5
  br label %21

21:                                               ; preds = %19, %18
  %.2 = phi ptr [ %20, %19 ], [ %16, %18 ]
  %22 = getelementptr inbounds i8, ptr %4, i64 24
  %23 = ptrtoint ptr %22 to i64
  %24 = ptrtoint ptr %.2 to i64
  %25 = sub i64 %23, %24
  %26 = call ptr @lj_str_new(ptr noundef %0, ptr noundef nonnull %.2, i64 noundef %25) #4
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #4
  ret ptr %26
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.abs.i64(i64, i1 immarg) #3

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass