; LLVM IR from Morpho-lang/morpho :: src/support (random)

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

@xoshiro256pp_state = external hidden global [4 x i64], align 16
@xoshiro256p_state = external hidden global [4 x i64], align 16
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stderr = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [95 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @splitmix64_seed(i64 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

; Function Attrs: nounwind uwtable
define dso_local void @random_initialize() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i64, align 8
  %3 = alloca [8 x i8], align 1
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #5
  %5 = call i64 @time(ptr noundef null) #5
  store i64 %5, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %6 = call noalias ptr @fopen(ptr noundef @.str, ptr noundef @.str.1)
  store ptr %6, ptr %1, align 8, !tbaa !9
  %7 = load ptr, ptr %1, align 8, !tbaa !9
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %30

9:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  store i32 0, ptr %4, align 4, !tbaa !11
  br label %10

10:                                               ; preds = %22, %9
  %11 = load i32, ptr %4, align 4, !tbaa !11
  %12 = sext i32 %11 to i64
  %13 = icmp ult i64 %12, 8
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  br label %25

15:                                               ; preds = %10
  %16 = load ptr, ptr %1, align 8, !tbaa !9
  %17 = call i32 @fgetc(ptr noundef %16)
  %18 = trunc i32 %17 to i8
  %19 = load i32, ptr %4, align 4, !tbaa !11
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [8 x i8], ptr %3, i64 0, i64 %20
  store i8 %18, ptr %21, align 1, !tbaa !13
  br label %22

22:                                               ; preds = %15
  %23 = load i32, ptr %4, align 4, !tbaa !11
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr %4, align 4, !tbaa !11
  br label %10, !llvm.loop !14

25:                                               ; preds = %14
  %26 = getelementptr inbounds [8 x i8], ptr %3, i64 0, i64 0
  %27 = load i64, ptr %26, align 1, !tbaa !5
  store i64 %27, ptr %2, align 8, !tbaa !5
  %28 = load ptr, ptr %1, align 8, !tbaa !9
  %29 = call i32 @fclose(ptr noundef %28)
  br label %33

30:                                               ; preds = %0
  %31 = load ptr, ptr @stderr, align 8, !tbaa !9
  %32 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %31, ptr noundef @.str.2) #5
  br label %33

33:                                               ; preds = %30, %25
  %34 = load i64, ptr %2, align 8, !tbaa !5
  call void @splitmix64_seed(i64 noundef %34)
  %35 = call i64 @splitmix64_next()
  store i64 %35, ptr @xoshiro256pp_state, align 16, !tbaa !5
  %36 = call i64 @splitmix64_next()
  store i64 %36, ptr getelementptr inbounds ([4 x i64], ptr @xoshiro256pp_state, i64 0, i64 1), align 8, !tbaa !5
  %37 = call i64 @splitmix64_next()
  store i64 %37, ptr getelementptr inbounds ([4 x i64], ptr @xoshiro256pp_state, i64 0, i64 2), align 16, !tbaa !5
  %38 = call i64 @splitmix64_next()
  store i64 %38, ptr getelementptr inbounds ([4 x i64], ptr @xoshiro256pp_state, i64 0, i64 3), align 8, !tbaa !5
  %39 = call i64 @splitmix64_next()
  store i64 %39, ptr @xoshiro256p_state, align 16, !tbaa !5
  %40 = call i64 @splitmix64_next()
  store i64 %40, ptr getelementptr inbounds ([4 x i64], ptr @xoshiro256p_state, i64 0, i64 1), align 8, !tbaa !5
  %41 = call i64 @splitmix64_next()
  store i64 %41, ptr getelementptr inbounds ([4 x i64], ptr @xoshiro256p_state, i64 0, i64 2), align 16, !tbaa !5
  %42 = call i64 @splitmix64_next()
  store i64 %42, ptr getelementptr inbounds ([4 x i64], ptr @xoshiro256p_state, i64 0, i64 3), align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #5
  ret void
}

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #2

declare noalias ptr @fopen(ptr noundef, ptr noundef) #3

declare i32 @fgetc(ptr noundef) #3

declare i32 @fclose(ptr noundef) #3

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @splitmix64_next() #4

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 = { inlinehint 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 }

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

@xoshiro256pp_state = external hidden local_unnamed_addr global [4 x i64], align 16
@xoshiro256p_state = external hidden local_unnamed_addr global [4 x i64], align 16
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [95 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @splitmix64_seed(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @random_initialize() local_unnamed_addr #0 {
  %1 = tail call i64 @time(ptr noundef null) #4
  %2 = tail call noalias ptr @fopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1)
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %22, label %.preheader.preheader

.preheader.preheader:                             ; preds = %0
  %3 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %4 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %5 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %6 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %7 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %8 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %9 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %10 = tail call i32 @fgetc(ptr noundef nonnull %2)
  %11 = and i32 %10, 255
  %.sroa.10.0.insert.ext = zext nneg i32 %11 to i64
  %.sroa.10.0.insert.shift = shl nuw i64 %.sroa.10.0.insert.ext, 56
  %12 = and i32 %9, 255
  %.sroa.9.0.insert.ext = zext nneg i32 %12 to i64
  %.sroa.9.0.insert.shift = shl nuw nsw i64 %.sroa.9.0.insert.ext, 48
  %.sroa.9.0.insert.insert = or disjoint i64 %.sroa.10.0.insert.shift, %.sroa.9.0.insert.shift
  %13 = and i32 %8, 255
  %.sroa.8.0.insert.ext = zext nneg i32 %13 to i64
  %.sroa.8.0.insert.shift = shl nuw nsw i64 %.sroa.8.0.insert.ext, 40
  %.sroa.8.0.insert.insert = or disjoint i64 %.sroa.9.0.insert.insert, %.sroa.8.0.insert.shift
  %14 = and i32 %7, 255
  %.sroa.7.0.insert.ext = zext nneg i32 %14 to i64
  %.sroa.7.0.insert.shift = shl nuw nsw i64 %.sroa.7.0.insert.ext, 32
  %.sroa.7.0.insert.insert = or disjoint i64 %.sroa.8.0.insert.insert, %.sroa.7.0.insert.shift
  %15 = shl i32 %6, 24
  %.sroa.6.0.insert.shift = zext i32 %15 to i64
  %.sroa.6.0.insert.insert = or disjoint i64 %.sroa.7.0.insert.insert, %.sroa.6.0.insert.shift
  %16 = shl i32 %5, 16
  %17 = shl i32 %4, 8
  %18 = and i32 %16, 16711680
  %.sroa.5.0.insert.shift.masked = zext nneg i32 %18 to i64
  %.sroa.4.0.insert.mask = or disjoint i64 %.sroa.6.0.insert.insert, %.sroa.5.0.insert.shift.masked
  %19 = and i32 %3, 255
  %.sroa.0.0.insert.ext = zext nneg i32 %19 to i64
  %20 = and i32 %17, 65280
  %.sroa.4.0.insert.shift.masked = zext nneg i32 %20 to i64
  %.sroa.0.0.insert.mask = or i64 %.sroa.4.0.insert.mask, %.sroa.4.0.insert.shift.masked
  %.sroa.0.0.insert.insert = or i64 %.sroa.0.0.insert.mask, %.sroa.0.0.insert.ext
  %21 = tail call i32 @fclose(ptr noundef nonnull %2)
  br label %25

22:                                               ; preds = %0
  %23 = load ptr, ptr @stderr, align 8, !tbaa !5
  %24 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %23, ptr noundef nonnull @.str.2) #5
  br label %25

25:                                               ; preds = %22, %.preheader.preheader
  %.06 = phi i64 [ %.sroa.0.0.insert.insert, %.preheader.preheader ], [ %1, %22 ]
  tail call void @splitmix64_seed(i64 noundef %.06)
  %26 = tail call i64 @splitmix64_next()
  store i64 %26, ptr @xoshiro256pp_state, align 16, !tbaa !9
  %27 = tail call i64 @splitmix64_next()
  store i64 %27, ptr getelementptr inbounds (i8, ptr @xoshiro256pp_state, i64 8), align 8, !tbaa !9
  %28 = tail call i64 @splitmix64_next()
  store i64 %28, ptr getelementptr inbounds (i8, ptr @xoshiro256pp_state, i64 16), align 16, !tbaa !9
  %29 = tail call i64 @splitmix64_next()
  store i64 %29, ptr getelementptr inbounds (i8, ptr @xoshiro256pp_state, i64 24), align 8, !tbaa !9
  %30 = tail call i64 @splitmix64_next()
  store i64 %30, ptr @xoshiro256p_state, align 16, !tbaa !9
  %31 = tail call i64 @splitmix64_next()
  store i64 %31, ptr getelementptr inbounds (i8, ptr @xoshiro256p_state, i64 8), align 8, !tbaa !9
  %32 = tail call i64 @splitmix64_next()
  store i64 %32, ptr getelementptr inbounds (i8, ptr @xoshiro256p_state, i64 16), align 16, !tbaa !9
  %33 = tail call i64 @splitmix64_next()
  store i64 %33, ptr getelementptr inbounds (i8, ptr @xoshiro256p_state, i64 24), align 8, !tbaa !9
  ret void
}

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fgetc(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @splitmix64_next() local_unnamed_addr #3

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 = { 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 #2 = { nofree 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 #3 = { inlinehint 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 #4 = { nounwind }
attributes #5 = { cold 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}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass