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

%struct.SM4_GCM_CTX = type { %struct.SM4_CTR_CTX, %struct.GHASH_CTX, [16 x i8], i64, [16 x i8], i64, i64 }
%struct.SM4_CTR_CTX = type { %struct.SM4_KEY, [16 x i8], [16 x i8], i64 }
%struct.SM4_KEY = type { [32 x i32] }
%struct.GHASH_CTX = type { [2 x i64], [2 x i64], i64, i64, [16 x i8], i64 }

@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_finish = external hidden unnamed_addr constant [23 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 @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 willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #4

declare i32 @sm4_ctr32_encrypt_finish(ptr noundef, ptr noundef, ptr noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm4_gcm_decrypt_finish(ptr noundef %0, ptr noundef %1, ptr noundef %2) #5 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [16 x i8], align 16
  %9 = 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
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #6
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %15

12:                                               ; preds = %3
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  %14 = icmp ne ptr %13, null
  br i1 %14, label %21, label %15

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

16:                                               ; preds = %15
  %17 = load ptr, ptr @stderr, align 8, !tbaa !5
  %18 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 312, ptr noundef @__FUNCTION__.sm4_gcm_decrypt_finish) #6
  br label %19

19:                                               ; preds = %16
  br label %20

20:                                               ; preds = %19
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %86

21:                                               ; preds = %12
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %26, label %24

24:                                               ; preds = %21
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 16, ptr %25, align 8, !tbaa !9
  store i32 1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %86

26:                                               ; preds = %21
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %27, i32 0, i32 5
  %29 = load i64, ptr %28, align 8, !tbaa !11
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %30, i32 0, i32 3
  %32 = load i64, ptr %31, align 8, !tbaa !16
  %33 = icmp ne i64 %29, %32
  br i1 %33, label %34, label %40

34:                                               ; preds = %26
  br label %35

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

38:                                               ; preds = %35
  br label %39

39:                                               ; preds = %38
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %86

40:                                               ; preds = %26
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %41, i32 0, i32 1
  %43 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  call void @ghash_finish(ptr noundef %42, ptr noundef %43)
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = call i32 @sm4_ctr32_encrypt_finish(ptr noundef %45, ptr noundef %46, ptr noundef %47)
  %49 = icmp ne i32 %48, 1
  br i1 %49, label %50, label %56

50:                                               ; preds = %40
  br label %51

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

54:                                               ; preds = %51
  br label %55

55:                                               ; preds = %54
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %86

56:                                               ; preds = %40
  %57 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %58 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %59, i32 0, i32 2
  %61 = getelementptr inbounds [16 x i8], ptr %60, i64 0, i64 0
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %62, i32 0, i32 3
  %64 = load i64, ptr %63, align 8, !tbaa !16
  call void @gmssl_memxor(ptr noundef %57, ptr noundef %58, ptr noundef %61, i64 noundef %64)
  %65 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %66, i32 0, i32 4
  %68 = getelementptr inbounds [16 x i8], ptr %67, i64 0, i64 0
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %69, i32 0, i32 3
  %71 = load i64, ptr %70, align 8, !tbaa !16
  %72 = call i32 @memcmp(ptr noundef %65, ptr noundef %68, i64 noundef %71) #7
  %73 = icmp ne i32 %72, 0
  br i1 %73, label %74, label %80

74:                                               ; preds = %56
  br label %75

75:                                               ; preds = %74
  %76 = load ptr, ptr @stderr, align 8, !tbaa !5
  %77 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %76, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 331, ptr noundef @__FUNCTION__.sm4_gcm_decrypt_finish) #6
  br label %78

78:                                               ; preds = %75
  br label %79

79:                                               ; preds = %78
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %86

80:                                               ; preds = %56
  %81 = load ptr, ptr %5, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %81, i32 0, i32 4
  %83 = getelementptr inbounds [16 x i8], ptr %82, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 8 %83, i8 0, i64 16, i1 false)
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.SM4_GCM_CTX, ptr %84, i32 0, i32 5
  store i64 0, ptr %85, align 8, !tbaa !11
  store i32 1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %86

86:                                               ; preds = %80, %79, %55, %39, %24, %20
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #6
  %87 = load i32, ptr %4, align 4
  ret i32 %87
}

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 = { 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 #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 }
attributes #7 = { 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 = !{!12, !10, i64 280}
!12 = !{!"", !13, i64 0, !15, i64 168, !7, i64 240, !10, i64 256, !7, i64 264, !10, i64 280, !10, i64 288}
!13 = !{!"", !14, i64 0, !7, i64 128, !7, i64 144, !10, i64 160}
!14 = !{!"", !7, i64 0}
!15 = !{!"", !7, i64 0, !7, i64 16, !10, i64 32, !10, i64 40, !7, i64 48, !10, i64 64}
!16 = !{!12, !10, i64 256}


; 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_finish = external hidden unnamed_addr constant [23 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 @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

declare i32 @sm4_ctr32_encrypt_finish(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm4_gcm_decrypt_finish(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #4 {
  %4 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #6
  %5 = icmp ne ptr %0, null
  %6 = icmp ne ptr %2, null
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %10, label %7

7:                                                ; preds = %3
  %8 = load ptr, ptr @stderr, align 8, !tbaa !5
  %9 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %8, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 312, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt_finish) #7
  br label %35

10:                                               ; preds = %3
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %11, label %12

11:                                               ; preds = %10
  store i64 16, ptr %2, align 8, !tbaa !9
  br label %35

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %0, i64 280
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %0, i64 256
  %16 = load i64, ptr %15, align 8, !tbaa !16
  %.not20 = icmp eq i64 %14, %16
  br i1 %.not20, label %20, label %17

17:                                               ; preds = %12
  %18 = load ptr, ptr @stderr, align 8, !tbaa !5
  %19 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 320, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt_finish) #7
  br label %35

20:                                               ; preds = %12
  %21 = getelementptr inbounds i8, ptr %0, i64 168
  call void @ghash_finish(ptr noundef nonnull %21, ptr noundef nonnull %4) #6
  %22 = call i32 @sm4_ctr32_encrypt_finish(ptr noundef nonnull %0, ptr noundef nonnull %1, ptr noundef nonnull %2) #6
  %.not21 = icmp eq i32 %22, 1
  br i1 %.not21, label %26, label %23

23:                                               ; preds = %20
  %24 = load ptr, ptr @stderr, align 8, !tbaa !5
  %25 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %24, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 325, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt_finish) #7
  br label %35

26:                                               ; preds = %20
  %27 = getelementptr inbounds i8, ptr %0, i64 240
  %28 = load i64, ptr %15, align 8, !tbaa !16
  call void @gmssl_memxor(ptr noundef nonnull %4, ptr noundef nonnull %4, ptr noundef nonnull %27, i64 noundef %28) #6
  %29 = getelementptr inbounds i8, ptr %0, i64 264
  %30 = load i64, ptr %15, align 8, !tbaa !16
  %bcmp = call i32 @bcmp(ptr nonnull %4, ptr nonnull %29, i64 %30)
  %.not22 = icmp eq i32 %bcmp, 0
  br i1 %.not22, label %34, label %31

31:                                               ; preds = %26
  %32 = load ptr, ptr @stderr, align 8, !tbaa !5
  %33 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %32, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 331, ptr noundef nonnull @__FUNCTION__.sm4_gcm_decrypt_finish) #7
  br label %35

34:                                               ; preds = %26
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %29, i8 0, i64 24, i1 false)
  br label %35

35:                                               ; preds = %34, %31, %23, %17, %11, %7
  %.0 = phi i32 [ -1, %17 ], [ -1, %23 ], [ -1, %31 ], [ 1, %34 ], [ 1, %11 ], [ -1, %7 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #6
  ret i32 %.0
}

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

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 = { 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 = { nofree nounwind willreturn memory(argmem: read) }
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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 280}
!12 = !{!"", !13, i64 0, !15, i64 168, !7, i64 240, !10, i64 256, !7, i64 264, !10, i64 280, !10, i64 288}
!13 = !{!"", !14, i64 0, !7, i64 128, !7, i64 144, !10, i64 160}
!14 = !{!"", !7, i64 0}
!15 = !{!"", !7, i64 0, !7, i64 16, !10, i64 32, !10, i64 40, !7, i64 48, !10, i64 64}
!16 = !{!12, !10, i64 256}


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