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

@.str.16 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.66 = external hidden unnamed_addr constant [18 x i8], align 1

declare void @lua_pushnumber(ptr noundef, double noundef) #0

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

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

declare i32 @lua_gettop(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @rlite_math_random(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %9 = call i32 (...) @rliteLrand48()
  %10 = srem i32 %9, 2147483647
  %11 = sitofp i32 %10 to double
  %12 = fdiv double %11, 0x41DFFFFFFFC00000
  store double %12, ptr %4, align 8, !tbaa !9
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call i32 @lua_gettop(ptr noundef %13)
  switch i32 %14, label %67 [
    i32 0, label %15
    i32 1, label %18
    i32 2, label %38
  ]

15:                                               ; preds = %1
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load double, ptr %4, align 8, !tbaa !9
  call void @lua_pushnumber(ptr noundef %16, double noundef %17)
  br label %70

18:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = call i64 @luaL_checkinteger(ptr noundef %19, i32 noundef 1)
  %21 = trunc i64 %20 to i32
  store i32 %21, ptr %5, align 4, !tbaa !11
  %22 = load i32, ptr %5, align 4, !tbaa !11
  %23 = icmp sle i32 1, %22
  br i1 %23, label %28, label %24

24:                                               ; preds = %18
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = call i32 @luaL_argerror(ptr noundef %25, i32 noundef 1, ptr noundef @.str.66)
  %27 = icmp ne i32 %26, 0
  br label %28

28:                                               ; preds = %24, %18
  %29 = phi i1 [ true, %18 ], [ %27, %24 ]
  %30 = zext i1 %29 to i32
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = load double, ptr %4, align 8, !tbaa !9
  %33 = load i32, ptr %5, align 4, !tbaa !11
  %34 = sitofp i32 %33 to double
  %35 = fmul double %32, %34
  %36 = call double @llvm.floor.f64(double %35)
  %37 = fadd double %36, 1.000000e+00
  call void @lua_pushnumber(ptr noundef %31, double noundef %37)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %70

38:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = call i64 @luaL_checkinteger(ptr noundef %39, i32 noundef 1)
  %41 = trunc i64 %40 to i32
  store i32 %41, ptr %6, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = call i64 @luaL_checkinteger(ptr noundef %42, i32 noundef 2)
  %44 = trunc i64 %43 to i32
  store i32 %44, ptr %7, align 4, !tbaa !11
  %45 = load i32, ptr %6, align 4, !tbaa !11
  %46 = load i32, ptr %7, align 4, !tbaa !11
  %47 = icmp sle i32 %45, %46
  br i1 %47, label %52, label %48

48:                                               ; preds = %38
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = call i32 @luaL_argerror(ptr noundef %49, i32 noundef 2, ptr noundef @.str.66)
  %51 = icmp ne i32 %50, 0
  br label %52

52:                                               ; preds = %48, %38
  %53 = phi i1 [ true, %38 ], [ %51, %48 ]
  %54 = zext i1 %53 to i32
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = load double, ptr %4, align 8, !tbaa !9
  %57 = load i32, ptr %7, align 4, !tbaa !11
  %58 = load i32, ptr %6, align 4, !tbaa !11
  %59 = sub nsw i32 %57, %58
  %60 = add nsw i32 %59, 1
  %61 = sitofp i32 %60 to double
  %62 = fmul double %56, %61
  %63 = call double @llvm.floor.f64(double %62)
  %64 = load i32, ptr %6, align 4, !tbaa !11
  %65 = sitofp i32 %64 to double
  %66 = fadd double %63, %65
  call void @lua_pushnumber(ptr noundef %55, double noundef %66)
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %70

67:                                               ; preds = %1
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = call i32 (ptr, ptr, ...) @luaL_error(ptr noundef %68, ptr noundef @.str.16)
  store i32 %69, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %71

70:                                               ; preds = %52, %28, %15
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %71

71:                                               ; preds = %70, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %72 = load i32, ptr %2, align 4
  ret i32 %72
}

declare i32 @rliteLrand48(...) #0

declare i64 @luaL_checkinteger(ptr noundef, i32 noundef) #0

declare i32 @luaL_argerror(ptr noundef, i32 noundef, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.floor.f64(double) #3

declare i32 @luaL_error(ptr noundef, ptr noundef, ...) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, 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.16 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.66 = external hidden unnamed_addr constant [18 x i8], align 1

declare void @lua_pushnumber(ptr noundef, double noundef) local_unnamed_addr #0

declare i32 @lua_gettop(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @rlite_math_random(ptr noundef %0) local_unnamed_addr #1 {
  %2 = tail call i32 (...) @rliteLrand48() #3
  %3 = srem i32 %2, 2147483647
  %4 = sitofp i32 %3 to double
  %5 = fdiv double %4, 0x41DFFFFFFFC00000
  %6 = tail call i32 @lua_gettop(ptr noundef %0) #3
  switch i32 %6, label %33 [
    i32 0, label %7
    i32 1, label %8
    i32 2, label %19
  ]

7:                                                ; preds = %1
  tail call void @lua_pushnumber(ptr noundef %0, double noundef %5) #3
  br label %35

8:                                                ; preds = %1
  %9 = tail call i64 @luaL_checkinteger(ptr noundef %0, i32 noundef 1) #3
  %10 = trunc i64 %9 to i32
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %14, label %12

12:                                               ; preds = %8
  %13 = tail call i32 @luaL_argerror(ptr noundef %0, i32 noundef 1, ptr noundef nonnull @.str.66) #3
  br label %14

14:                                               ; preds = %12, %8
  %15 = sitofp i32 %10 to double
  %16 = fmul double %5, %15
  %17 = tail call double @llvm.floor.f64(double %16)
  %18 = fadd double %17, 1.000000e+00
  tail call void @lua_pushnumber(ptr noundef %0, double noundef %18) #3
  br label %35

19:                                               ; preds = %1
  %20 = tail call i64 @luaL_checkinteger(ptr noundef %0, i32 noundef 1) #3
  %21 = trunc i64 %20 to i32
  %22 = tail call i64 @luaL_checkinteger(ptr noundef %0, i32 noundef 2) #3
  %23 = trunc i64 %22 to i32
  %.not = icmp sgt i32 %21, %23
  br i1 %.not, label %24, label %26

24:                                               ; preds = %19
  %25 = tail call i32 @luaL_argerror(ptr noundef %0, i32 noundef 2, ptr noundef nonnull @.str.66) #3
  br label %26

26:                                               ; preds = %24, %19
  %reass.sub = sub i32 %23, %21
  %27 = add i32 %reass.sub, 1
  %28 = sitofp i32 %27 to double
  %29 = fmul double %5, %28
  %30 = tail call double @llvm.floor.f64(double %29)
  %31 = sitofp i32 %21 to double
  %32 = fadd double %30, %31
  tail call void @lua_pushnumber(ptr noundef %0, double noundef %32) #3
  br label %35

33:                                               ; preds = %1
  %34 = tail call i32 (ptr, ptr, ...) @luaL_error(ptr noundef %0, ptr noundef nonnull @.str.16) #3
  br label %35

35:                                               ; preds = %7, %14, %26, %33
  %.0 = phi i32 [ %34, %33 ], [ 1, %26 ], [ 1, %14 ], [ 1, %7 ]
  ret i32 %.0
}

declare i32 @rliteLrand48(...) local_unnamed_addr #0

declare i64 @luaL_checkinteger(ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @luaL_argerror(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.floor.f64(double) #2

declare i32 @luaL_error(ptr noundef, ptr 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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)"}


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