; LLVM IR from guanzhi/GmSSL :: src (zuc)

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

%struct.ZUC256_MAC_CTX_st = type { [16 x i32], i32, i32, [4 x i32], [4 x i32], [4 x i8], i64, i32 }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
declare hidden void @zuc256_set_mac_key(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @zuc256_generate_keystream(ptr noundef, i64 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @zuc256_mac_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  %9 = load i32, ptr %8, align 4, !tbaa !9
  %10 = icmp slt i32 %9, 32
  br i1 %10, label %11, label %12

11:                                               ; preds = %4
  store i32 32, ptr %8, align 4, !tbaa !9
  br label %17

12:                                               ; preds = %4
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp sgt i32 %13, 64
  br i1 %14, label %15, label %16

15:                                               ; preds = %12
  store i32 128, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %15, %12
  br label %17

17:                                               ; preds = %16, %11
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %18, i8 0, i64 128, i1 false)
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !9
  call void @zuc256_set_mac_key(ptr noundef %19, ptr noundef %20, ptr noundef %21, i32 noundef %22)
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = sdiv i32 %24, 32
  %26 = sext i32 %25 to i64
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.ZUC256_MAC_CTX_st, ptr %27, i32 0, i32 3
  %29 = getelementptr inbounds [4 x i32], ptr %28, i64 0, i64 0
  call void @zuc256_generate_keystream(ptr noundef %23, i64 noundef %26, ptr noundef %29)
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = load i32, ptr %8, align 4, !tbaa !9
  %32 = sdiv i32 %31, 32
  %33 = sext i32 %32 to i64
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.ZUC256_MAC_CTX_st, ptr %34, i32 0, i32 4
  %36 = getelementptr inbounds [4 x i32], ptr %35, i64 0, i64 0
  call void @zuc256_generate_keystream(ptr noundef %30, i64 noundef %33, ptr noundef %36)
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = sdiv i32 %37, 32
  %39 = mul nsw i32 %38, 32
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ZUC256_MAC_CTX_st, ptr %40, i32 0, i32 7
  store i32 %39, ptr %41, align 8, !tbaa !11
  ret void
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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" }

!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, !10, i64 120}
!12 = !{!"ZUC256_MAC_CTX_st", !7, i64 0, !10, i64 64, !10, i64 68, !7, i64 72, !7, i64 88, !7, i64 104, !13, i64 112, !10, i64 120}
!13 = !{!"long", !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"

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nounwind uwtable
declare hidden void @zuc256_set_mac_key(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @zuc256_generate_keystream(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @zuc256_mac_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = icmp slt i32 %3, 32
  %6 = icmp ugt i32 %3, 64
  %spec.store.select = select i1 %6, i32 128, i32 %3
  %.0 = select i1 %5, i32 32, i32 %spec.store.select
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %0, i8 0, i64 128, i1 false)
  tail call void @zuc256_set_mac_key(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %.0)
  %7 = lshr i32 %.0, 5
  %8 = zext nneg i32 %7 to i64
  %9 = getelementptr inbounds i8, ptr %0, i64 72
  tail call void @zuc256_generate_keystream(ptr noundef %0, i64 noundef %8, ptr noundef nonnull %9)
  %10 = getelementptr inbounds i8, ptr %0, i64 88
  tail call void @zuc256_generate_keystream(ptr noundef %0, i64 noundef %8, ptr noundef nonnull %10)
  %11 = and i32 %.0, 224
  %12 = getelementptr inbounds i8, ptr %0, i64 120
  store i32 %11, ptr %12, align 8, !tbaa !5
  ret void
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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" }

!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, !9, i64 120}
!6 = !{!"ZUC256_MAC_CTX_st", !7, i64 0, !9, i64 64, !9, i64 68, !7, i64 72, !7, i64 88, !7, i64 104, !10, i64 112, !9, i64 120}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"long", !7, i64 0}


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