; 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_decrypt = external hidden unnamed_addr constant [16 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

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

; 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) #4

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

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

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

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

; 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 i32 @sm4_gcm_decrypt(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) #5 {
  %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) #7
  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) #7
  call void @llvm.lifetime.start.p0(i64 16, ptr %24) #7
  %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 80, ptr noundef @__FUNCTION__.sm4_gcm_decrypt) #7
  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 %108

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 84, ptr noundef @__FUNCTION__.sm4_gcm_decrypt) #7
  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 %108

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 88, ptr noundef @__FUNCTION__.sm4_gcm_decrypt) #7
  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 %108

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 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  %78 = load ptr, ptr %15, align 8, !tbaa !5
  %79 = load i64, ptr %16, align 8, !tbaa !9
  %80 = load ptr, ptr %17, align 8, !tbaa !5
  %81 = load i64, ptr %18, align 8, !tbaa !9
  %82 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  call void @ghash(ptr noundef %77, ptr noundef %78, i64 noundef %79, ptr noundef %80, i64 noundef %81, ptr noundef %82)
  %83 = load ptr, ptr %12, align 8, !tbaa !5
  %84 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %85 = getelementptr inbounds [16 x i8], ptr %24, i64 0, i64 0
  call void @sm4_encrypt(ptr noundef %83, ptr noundef %84, ptr noundef %85)
  %86 = getelementptr inbounds [16 x i8], ptr %24, i64 0, i64 0
  %87 = getelementptr inbounds [16 x i8], ptr %24, i64 0, i64 0
  %88 = getelementptr inbounds [16 x i8], ptr %22, i64 0, i64 0
  %89 = load i64, ptr %20, align 8, !tbaa !9
  call void @gmssl_memxor(ptr noundef %86, ptr noundef %87, ptr noundef %88, i64 noundef %89)
  %90 = getelementptr inbounds [16 x i8], ptr %24, i64 0, i64 0
  %91 = load ptr, ptr %19, align 8, !tbaa !5
  %92 = load i64, ptr %20, align 8, !tbaa !9
  %93 = call i32 @memcmp(ptr noundef %90, ptr noundef %91, i64 noundef %92) #8
  %94 = icmp ne i32 %93, 0
  br i1 %94, label %95, label %101

95:                                               ; preds = %76
  br label %96

96:                                               ; preds = %95
  %97 = load ptr, ptr @stderr, align 8, !tbaa !5
  %98 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %97, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 107, ptr noundef @__FUNCTION__.sm4_gcm_decrypt) #7
  br label %99

99:                                               ; preds = %96
  br label %100

100:                                              ; preds = %99
  store i32 -1, ptr %11, align 4
  store i32 1, ptr %25, align 4
  br label %108

101:                                              ; preds = %76
  %102 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  call void @ctr32_incr(ptr noundef %102)
  %103 = load ptr, ptr %12, align 8, !tbaa !5
  %104 = getelementptr inbounds [16 x i8], ptr %23, i64 0, i64 0
  %105 = load ptr, ptr %17, align 8, !tbaa !5
  %106 = load i64, ptr %18, align 8, !tbaa !9
  %107 = load ptr, ptr %21, align 8, !tbaa !5
  call void @sm4_ctr32_encrypt(ptr noundef %103, ptr noundef %104, ptr noundef %105, i64 noundef %106, ptr noundef %107)
  store i32 1, ptr %11, align 4
  store i32 1, ptr %25, align 4
  br label %108

108:                                              ; preds = %101, %100, %57, %48, %36
  call void @llvm.lifetime.end.p0(i64 16, ptr %24) #7
  call void @llvm.lifetime.end.p0(i64 16, ptr %23) #7
  call void @llvm.lifetime.end.p0(i64 16, ptr %22) #7
  %109 = load i32, ptr %11, align 4
  ret i32 %109
}

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #6

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind willreturn memory(read) "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 #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }

!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_decrypt = external hidden unnamed_addr constant [16 x i8], align 1

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

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

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

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

; 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) #4

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

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

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

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

; Function Attrs: mustprogress 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 range(i32 -1, 2) i32 @sm4_gcm_decrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6, ptr nocapture noundef readonly %7, i64 noundef %8, ptr noundef %9) local_unnamed_addr #5 {
  %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) #7
  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) #7
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %13) #7
  %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 80, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt) #8
  br label %38

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 84, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt) #8
  br label %38

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 88, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt) #8
  br label %38

28:                                               ; preds = %23
  call void @sm4_encrypt(ptr noundef %0, ptr noundef nonnull %11, ptr noundef nonnull %11) #7
  %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) #7
  br label %33

33:                                               ; preds = %32, %30
  call void @ghash(ptr noundef nonnull %11, ptr noundef %3, i64 noundef %4, ptr noundef %5, i64 noundef %6, ptr noundef nonnull %11) #7
  call void @sm4_encrypt(ptr noundef %0, ptr noundef nonnull %12, ptr noundef nonnull %13) #7
  call void @gmssl_memxor(ptr noundef nonnull %13, ptr noundef nonnull %13, ptr noundef nonnull %11, i64 noundef %8) #7
  %bcmp = call i32 @bcmp(ptr nonnull %13, ptr %7, i64 %8)
  %.not = icmp eq i32 %bcmp, 0
  br i1 %.not, label %37, label %34

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

37:                                               ; preds = %33
  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 %9) #7
  br label %38

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

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #6

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nofree nounwind willreturn memory(argmem: read) }
attributes #7 = { nounwind }
attributes #8 = { 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,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass