; LLVM IR from square/subzero :: core/external-crypto/aes (aesxam)

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

@fillrand.a = external hidden global [2 x i64], align 16
@fillrand.mt = external hidden global i64, align 8
@fillrand.count = external hidden global i64, align 8
@fillrand.r = external hidden global [4 x i8], align 1

; 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 dso_local void @fillrand(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %6 = load i64, ptr @fillrand.mt, align 8, !tbaa !11
  %7 = icmp ne i64 %6, 0
  br i1 %7, label %8, label %10

8:                                                ; preds = %2
  store i64 0, ptr @fillrand.mt, align 8, !tbaa !11
  %9 = call i64 @read_tsc()
  store i64 %9, ptr @fillrand.a, align 16, !tbaa !13
  br label %10

10:                                               ; preds = %8, %2
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %42, %10
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = load i32, ptr %4, align 4, !tbaa !9
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %45

15:                                               ; preds = %11
  %16 = load i64, ptr @fillrand.count, align 8, !tbaa !11
  %17 = icmp eq i64 %16, 4
  br i1 %17, label %18, label %33

18:                                               ; preds = %15
  %19 = load i64, ptr @fillrand.a, align 16, !tbaa !11
  %20 = and i64 %19, 65535
  %21 = mul i64 36969, %20
  %22 = load i64, ptr @fillrand.a, align 16, !tbaa !11
  %23 = lshr i64 %22, 16
  %24 = add i64 %21, %23
  store i64 %24, ptr @fillrand.a, align 16, !tbaa !11
  %25 = shl i64 %24, 16
  %26 = load i64, ptr getelementptr inbounds ([2 x i64], ptr @fillrand.a, i64 0, i64 1), align 8, !tbaa !11
  %27 = and i64 %26, 65535
  %28 = mul i64 18000, %27
  %29 = load i64, ptr getelementptr inbounds ([2 x i64], ptr @fillrand.a, i64 0, i64 1), align 8, !tbaa !11
  %30 = lshr i64 %29, 16
  %31 = add i64 %28, %30
  store i64 %31, ptr getelementptr inbounds ([2 x i64], ptr @fillrand.a, i64 0, i64 1), align 8, !tbaa !11
  %32 = add i64 %25, %31
  store i64 %32, ptr @fillrand.r, align 1, !tbaa !11
  store i64 0, ptr @fillrand.count, align 8, !tbaa !11
  br label %33

33:                                               ; preds = %18, %15
  %34 = load i64, ptr @fillrand.count, align 8, !tbaa !11
  %35 = add i64 %34, 1
  store i64 %35, ptr @fillrand.count, align 8, !tbaa !11
  %36 = getelementptr inbounds [4 x i8], ptr @fillrand.r, i64 0, i64 %34
  %37 = load i8, ptr %36, align 1, !tbaa !15
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %38, i64 %40
  store i8 %37, ptr %41, align 1, !tbaa !15
  br label %42

42:                                               ; preds = %33
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !9
  br label %11, !llvm.loop !16

45:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @read_tsc() #2

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 = { 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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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"

@fillrand.a = external hidden local_unnamed_addr global [2 x i64], align 16
@fillrand.mt = external hidden local_unnamed_addr global i64, align 8
@fillrand.count = external hidden local_unnamed_addr global i64, align 8
@fillrand.r = external hidden local_unnamed_addr global [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @fillrand(ptr nocapture noundef writeonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i64, ptr @fillrand.mt, align 8, !tbaa !5
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %6, label %4

4:                                                ; preds = %2
  store i64 0, ptr @fillrand.mt, align 8, !tbaa !5
  %5 = tail call i64 @read_tsc()
  store i64 %5, ptr @fillrand.a, align 16, !tbaa !9
  br label %6

6:                                                ; preds = %4, %2
  %7 = icmp sgt i32 %1, 0
  br i1 %7, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %6
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %24
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %24 ]
  %8 = load i64, ptr @fillrand.count, align 8, !tbaa !5
  %9 = icmp eq i64 %8, 4
  br i1 %9, label %10, label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.phi.trans.insert = getelementptr inbounds [4 x i8], ptr @fillrand.r, i64 0, i64 %8
  %.pre = load i8, ptr %.phi.trans.insert, align 1, !tbaa !11
  br label %24

10:                                               ; preds = %.lr.ph
  %11 = load i64, ptr @fillrand.a, align 16, !tbaa !5
  %12 = and i64 %11, 65535
  %13 = mul nuw nsw i64 %12, 36969
  %14 = lshr i64 %11, 16
  %15 = add nuw nsw i64 %13, %14
  store i64 %15, ptr @fillrand.a, align 16, !tbaa !5
  %16 = shl i64 %15, 16
  %17 = load i64, ptr getelementptr inbounds (i8, ptr @fillrand.a, i64 8), align 8, !tbaa !5
  %18 = and i64 %17, 65535
  %19 = mul nuw nsw i64 %18, 18000
  %20 = lshr i64 %17, 16
  %21 = add nuw nsw i64 %19, %20
  store i64 %21, ptr getelementptr inbounds (i8, ptr @fillrand.a, i64 8), align 8, !tbaa !5
  %22 = add i64 %21, %16
  store i64 %22, ptr @fillrand.r, align 1, !tbaa !5
  %23 = trunc i64 %21 to i8
  br label %24

24:                                               ; preds = %.lr.ph._crit_edge, %10
  %25 = phi i8 [ %23, %10 ], [ %.pre, %.lr.ph._crit_edge ]
  %26 = phi i64 [ 0, %10 ], [ %8, %.lr.ph._crit_edge ]
  %27 = add i64 %26, 1
  store i64 %27, ptr @fillrand.count, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  store i8 %25, ptr %28, align 1, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %24, %6
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @read_tsc() 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 = { 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" }

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


; Effective O3 passes: BDCEPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass