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

; 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_CBC_SM3_HMAC_CTX = type { %struct.SM4_CBC_CTX, %struct.SM3_HMAC_CTX, [32 x i8], i64 }
%struct.SM4_CBC_CTX = type { %struct.SM4_KEY, [16 x i8], [16 x i8], i64 }
%struct.SM4_KEY = type { [32 x i32] }
%struct.SM3_HMAC_CTX = type { %struct.SM3_CTX, [64 x i8] }
%struct.SM3_CTX = type { [8 x i32], i64, [64 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 [70 x i8], align 1
@__FUNCTION__.sm4_cbc_sm3_hmac_decrypt_init = external hidden unnamed_addr constant [30 x i8], align 1

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

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

declare void @sm3_hmac_init(ptr noundef, ptr noundef, i64 noundef) #2

declare void @sm3_hmac_update(ptr noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @sm4_cbc_sm3_hmac_decrypt_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i64 noundef %4) #3 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i64 %4, ptr %11, align 8, !tbaa !9
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %26

14:                                               ; preds = %5
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %26

17:                                               ; preds = %14
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %26

20:                                               ; preds = %17
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %31, label %23

23:                                               ; preds = %20
  %24 = load i64, ptr %11, align 8, !tbaa !9
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %31

26:                                               ; preds = %23, %17, %14, %5
  br label %27

27:                                               ; preds = %26
  %28 = load ptr, ptr @stderr, align 8, !tbaa !5
  %29 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 74, ptr noundef @__FUNCTION__.sm4_cbc_sm3_hmac_decrypt_init) #4
  br label %30

30:                                               ; preds = %27
  store i32 -1, ptr %6, align 4
  br label %60

31:                                               ; preds = %23, %20
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %32, i8 0, i64 384, i1 false)
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.SM4_CBC_SM3_HMAC_CTX, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  %37 = call i32 @sm4_cbc_decrypt_init(ptr noundef %34, ptr noundef %35, ptr noundef %36)
  %38 = icmp ne i32 %37, 1
  br i1 %38, label %39, label %44

39:                                               ; preds = %31
  br label %40

40:                                               ; preds = %39
  %41 = load ptr, ptr @stderr, align 8, !tbaa !5
  %42 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %41, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 79, ptr noundef @__FUNCTION__.sm4_cbc_sm3_hmac_decrypt_init) #4
  br label %43

43:                                               ; preds = %40
  store i32 -1, ptr %6, align 4
  br label %60

44:                                               ; preds = %31
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.SM4_CBC_SM3_HMAC_CTX, ptr %45, i32 0, i32 1
  %47 = load ptr, ptr %8, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 16
  call void @sm3_hmac_init(ptr noundef %46, ptr noundef %48, i64 noundef 32)
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = icmp ne ptr %49, null
  br i1 %50, label %51, label %59

51:                                               ; preds = %44
  %52 = load i64, ptr %11, align 8, !tbaa !9
  %53 = icmp ne i64 %52, 0
  br i1 %53, label %54, label %59

54:                                               ; preds = %51
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SM4_CBC_SM3_HMAC_CTX, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %10, align 8, !tbaa !5
  %58 = load i64, ptr %11, align 8, !tbaa !9
  call void @sm3_hmac_update(ptr noundef %56, ptr noundef %57, i64 noundef %58)
  br label %59

59:                                               ; preds = %54, %51, %44
  store i32 1, ptr %6, align 4
  br label %60

60:                                               ; preds = %59, %43, %30
  %61 = load i32, ptr %6, align 4
  ret i32 %61
}

declare i32 @sm4_cbc_decrypt_init(ptr noundef, ptr noundef, ptr noundef) #2

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


; 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 [70 x i8], align 1
@__FUNCTION__.sm4_cbc_sm3_hmac_decrypt_init = external hidden unnamed_addr constant [30 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #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

declare void @sm3_hmac_init(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @sm3_hmac_update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @sm4_cbc_sm3_hmac_decrypt_init(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #3 {
  %6 = icmp ne ptr %0, null
  %7 = icmp ne ptr %1, null
  %or.cond = and i1 %6, %7
  %8 = icmp ne ptr %2, null
  %or.cond3 = and i1 %or.cond, %8
  br i1 %or.cond3, label %9, label %12

9:                                                ; preds = %5
  %10 = icmp eq ptr %3, null
  %11 = icmp ne i64 %4, 0
  %or.cond5 = and i1 %10, %11
  br i1 %or.cond5, label %12, label %15

12:                                               ; preds = %5, %9
  %13 = load ptr, ptr @stderr, align 8, !tbaa !5
  %14 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 74, ptr noundef nonnull @__FUNCTION__.sm4_cbc_sm3_hmac_decrypt_init) #4
  br label %25

15:                                               ; preds = %9
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(384) %0, i8 0, i64 384, i1 false)
  %16 = tail call i32 @sm4_cbc_decrypt_init(ptr noundef nonnull %0, ptr noundef nonnull %1, ptr noundef nonnull %2) #5
  %.not = icmp eq i32 %16, 1
  br i1 %.not, label %20, label %17

17:                                               ; preds = %15
  %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 79, ptr noundef nonnull @__FUNCTION__.sm4_cbc_sm3_hmac_decrypt_init) #4
  br label %25

20:                                               ; preds = %15
  %21 = getelementptr inbounds i8, ptr %0, i64 168
  %22 = getelementptr inbounds i8, ptr %1, i64 16
  tail call void @sm3_hmac_init(ptr noundef nonnull %21, ptr noundef nonnull %22, i64 noundef 32) #5
  %23 = icmp ne ptr %3, null
  %or.cond7 = and i1 %23, %11
  br i1 %or.cond7, label %24, label %25

24:                                               ; preds = %20
  tail call void @sm3_hmac_update(ptr noundef nonnull %21, ptr noundef nonnull %3, i64 noundef %4) #5
  br label %25

25:                                               ; preds = %20, %24, %17, %12
  %.0 = phi i32 [ -1, %12 ], [ -1, %17 ], [ 1, %24 ], [ 1, %20 ]
  ret i32 %.0
}

declare i32 @sm4_cbc_decrypt_init(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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


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