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

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

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [61 x i8], align 1
@__FUNCTION__.sm4_gcm_encrypt = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @sm4_gcm_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6, ptr noundef %7, i64 noundef %8, ptr noundef %9) #0 {
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  %17 = alloca ptr, align 8
  %18 = alloca i64, align 8
  %19 = alloca ptr, align 8
  %20 = alloca i64, align 8
  %21 = alloca ptr, align 8
  %22 = alloca [16 x i8], align 16
  %23 = alloca [16 x i8], align 16
  %24 = alloca [16 x i8], align 16
  %25 = alloca i32, align 4
  store ptr %0, ptr %12, align 8, !tbaa !5
  store ptr %1, ptr %13, align 8, !tbaa !5
  store i64 %2, ptr %14, align 8, !tbaa !9
  store ptr %3, ptr %15, align 8, !tbaa !5
  store i64 %4, ptr %16, align 8, !tbaa !9
  store ptr %5, ptr %17, align 8, !tbaa !5
  store i64 %6, ptr %18, align 8, !tbaa !9
  store ptr %7, ptr %19, align 8, !tbaa !5
  store i64 %8, ptr %20, align 8, !tbaa !9
  store ptr %9, ptr %21, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %22) #6
  call void @llvm.memset.p0.i64(ptr align 16 %22, i8 0, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 16, ptr %23) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr %24) #6
  %26 = load i64, ptr %14, align 8, !tbaa !9
  %27 = icmp ult i64 %26, 1
  br i1 %27, label %31, label %28

28:                                               ; preds = %10
  %29 = load i64, ptr %14, align 8, !tbaa !9
  %30 = icmp ugt i64 %29, 64
  br i1 %30, label %31, label %37

31:                                               ; preds = %28, %10
  br label %32

32:                                               ; preds = %31
  %33 = load ptr, ptr @stderr, align 8, !tbaa !5
  %34 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 38, ptr noundef @__FUNCTION__.sm4_gcm_encrypt) #6
  br label %35

35:                                               ; preds = %32
  br label %36

36:                                               ; preds = %35
  store i32 -1, ptr %11, align 4
  store i32 1, ptr %25, align 4
  br label %96

37:                                               ; preds = %28
  %38 = load i64, ptr %20, align 8, !tbaa !9
  %39 = icmp ult i64 %38, 12
  br i1 %39, label %43, label %40

40:                                               ; preds = %37
  %41 = load i64, ptr %20, align 8, !tbaa !9
  %42 = icmp ugt i64 %41, 16
  br i1 %42, label %43, label %49

43:                                               ; preds = %40, %37
  br label %44

44:                                               ; preds = %43
  %45 = load ptr, ptr @stderr, align 8, !tbaa !5
  %46 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %45, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 42, ptr noundef @__FUNCTION__.sm4_gcm_encrypt) #6
  br label %47

47:                                               ; preds = %44
  br label %48

48:                                               ; preds = %47
  store i32 -1, ptr %11, align 4
  store i32 1, ptr %25, align 4
  br label %96

49:                                               ; preds = %40
  %50 = load i64, ptr %18, align 8, !tbaa !9
  %51 = icmp ugt i64 %50, 68719476704
  br i1 %51, label %52, label %58

52:                                               ; preds = %49
  br label %53

53:                                               ; preds = %52
  %54 = load ptr, ptr @stderr, align 8, !tbaa !5
  %55 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %54, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 46, ptr noundef @__FUNCTION__.sm4_gcm_encrypt) #6
  br label %56

56:                                               ; preds = %53
  br label %57

57:                                               ; preds = %56
  store i32 -1, ptr %11, align 4
  store i32 1, ptr %25, align 4
  br label %96

58:                                               ; preds = %49
  %59 = load ptr, ptr %12, align 8, !tbaa !5
  %60 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  %61 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  call void @sm4_encrypt(ptr noundef %59, ptr noundef %60, ptr noundef %61)
  %62 = load i64, ptr %14, align 8, !tbaa !9
  %63 = icmp eq i64 %62, 12
  br i1 %63, label %64, label %71

64:                                               ; preds = %58
  %65 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %65, ptr align 1 %66, i64 12, i1 false)
  %67 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 14
  store i8 0, ptr %67, align 2, !tbaa !11
  %68 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 13
  store i8 0, ptr %68, align 1, !tbaa !11
  %69 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 12
  store i8 0, ptr %69, align 4, !tbaa !11
  %70 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 15
  store i8 1, ptr %70, align 1, !tbaa !11
  br label %76

71:                                               ; preds = %58
  %72 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  %73 = load ptr, ptr %13, align 8, !tbaa !5
  %74 = load i64, ptr %14, align 8, !tbaa !9
  %75 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  call void @ghash(ptr noundef %72, ptr noundef null, i64 noundef 0, ptr noundef %73, i64 noundef %74, ptr noundef %75)
  br label %76

76:                                               ; preds = %71, %64
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  %78 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %79 = getelementptr inbounds [16 x i8], ptr %24, i64 0, i64 0
  call void @sm4_encrypt(ptr noundef %77, ptr noundef %78, ptr noundef %79)
  %80 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  call void @ctr32_incr(ptr noundef %80)
  %81 = load ptr, ptr %12, align 8, !tbaa !5
  %82 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %83 = load ptr, ptr %17, align 8, !tbaa !5
  %84 = load i64, ptr %18, align 8, !tbaa !9
  %85 = load ptr, ptr %19, align 8, !tbaa !5
  call void @sm4_ctr32_encrypt(ptr noundef %81, ptr noundef %82, ptr noundef %83, i64 noundef %84, ptr noundef %85)
  %86 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  %87 = load ptr, ptr %15, align 8, !tbaa !5
  %88 = load i64, ptr %16, align 8, !tbaa !9
  %89 = load ptr, ptr %19, align 8, !tbaa !5
  %90 = load i64, ptr %18, align 8, !tbaa !9
  %91 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  call void @ghash(ptr noundef %86, ptr noundef %87, i64 noundef %88, ptr noundef %89, i64 noundef %90, ptr noundef %91)
  %92 = load ptr, ptr %21, align 8, !tbaa !5
  %93 = getelementptr inbounds [16 x i8], ptr %24, i64 0, i64 0
  %94 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  %95 = load i64, ptr %20, align 8, !tbaa !9
  call void @gmssl_memxor(ptr noundef %92, ptr noundef %93, ptr noundef %94, i64 noundef %95)
  store i32 1, ptr %11, align 4
  store i32 1, ptr %25, align 4
  br label %96

96:                                               ; preds = %76, %57, %48, %36
  call void @llvm.lifetime.end.p0(i64 16, ptr %24) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %23) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %22) #6
  %97 = load i32, ptr %11, align 4
  ret i32 %97
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

declare void @sm4_encrypt(ptr noundef, ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

declare void @ghash(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) #4

; Function Attrs: nounwind uwtable
declare hidden void @ctr32_incr(ptr noundef) #0

declare void @sm4_ctr32_encrypt(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) #4

declare void @gmssl_memxor(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { 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 = !{!7, !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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [61 x i8], align 1
@__FUNCTION__.sm4_gcm_encrypt = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm4_gcm_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6, ptr noundef %7, i64 noundef %8, ptr noundef %9) local_unnamed_addr #0 {
  %11 = alloca [16 x i8], align 16
  %12 = alloca [16 x i8], align 16
  %13 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %11) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %11, i8 0, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %12) #6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %13) #6
  %14 = add i64 %2, -65
  %or.cond = icmp ult i64 %14, -64
  br i1 %or.cond, label %15, label %18

15:                                               ; preds = %10
  %16 = load ptr, ptr @stderr, align 8, !tbaa !5
  %17 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %16, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 38, ptr noundef nonnull @__FUNCTION__.sm4_gcm_encrypt) #7
  br label %34

18:                                               ; preds = %10
  %19 = add i64 %8, -17
  %or.cond3 = icmp ult i64 %19, -5
  br i1 %or.cond3, label %20, label %23

20:                                               ; preds = %18
  %21 = load ptr, ptr @stderr, align 8, !tbaa !5
  %22 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 42, ptr noundef nonnull @__FUNCTION__.sm4_gcm_encrypt) #7
  br label %34

23:                                               ; preds = %18
  %24 = icmp ugt i64 %6, 68719476704
  br i1 %24, label %25, label %28

25:                                               ; preds = %23
  %26 = load ptr, ptr @stderr, align 8, !tbaa !5
  %27 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %26, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 46, ptr noundef nonnull @__FUNCTION__.sm4_gcm_encrypt) #7
  br label %34

28:                                               ; preds = %23
  call void @sm4_encrypt(ptr noundef %0, ptr noundef nonnull %11, ptr noundef nonnull %11) #6
  %29 = icmp eq i64 %2, 12
  br i1 %29, label %30, label %32

30:                                               ; preds = %28
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(12) %12, ptr noundef nonnull align 1 dereferenceable(12) %1, i64 12, i1 false)
  %31 = getelementptr inbounds i8, ptr %12, i64 12
  store <4 x i8> <i8 0, i8 0, i8 0, i8 1>, ptr %31, align 4, !tbaa !9
  br label %33

32:                                               ; preds = %28
  call void @ghash(ptr noundef nonnull %11, ptr noundef null, i64 noundef 0, ptr noundef %1, i64 noundef %2, ptr noundef nonnull %12) #6
  br label %33

33:                                               ; preds = %32, %30
  call void @sm4_encrypt(ptr noundef %0, ptr noundef nonnull %12, ptr noundef nonnull %13) #6
  call void @ctr32_incr(ptr noundef nonnull %12)
  call void @sm4_ctr32_encrypt(ptr noundef %0, ptr noundef nonnull %12, ptr noundef %5, i64 noundef %6, ptr noundef %7) #6
  call void @ghash(ptr noundef nonnull %11, ptr noundef %3, i64 noundef %4, ptr noundef %7, i64 noundef %6, ptr noundef nonnull %11) #6
  call void @gmssl_memxor(ptr noundef %9, ptr noundef nonnull %13, ptr noundef nonnull %11, i64 noundef %8) #6
  br label %34

34:                                               ; preds = %33, %25, %20, %15
  %.0 = phi i32 [ -1, %15 ], [ -1, %20 ], [ -1, %25 ], [ 1, %33 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %13) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %12) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %11) #6
  ret i32 %.0
}

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

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

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

declare void @sm4_encrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #5

declare void @ghash(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
declare hidden void @ctr32_incr(ptr noundef) local_unnamed_addr #0

declare void @sm4_ctr32_encrypt(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

declare void @gmssl_memxor(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 #4 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!7, !7, i64 0}


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