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

; 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_CTX = type { %struct.SM4_KEY, [16 x i8], [16 x i8], i64 }
%struct.SM4_KEY = type { [32 x i32] }

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm4_cbc_padding_decrypt(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sm4_cbc_decrypt_finish(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  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
  %8 = load ptr, ptr %5, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %10, label %13

10:                                               ; preds = %3
  %11 = load ptr, ptr %7, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %18, label %13

13:                                               ; preds = %10, %3
  br label %14

14:                                               ; preds = %13
  %15 = load ptr, ptr @stderr, align 8, !tbaa !5
  %16 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef @.str.4, ptr noundef @.str.1, i32 noundef 224, ptr noundef @__FUNCTION__.sm4_cbc_decrypt_finish) #2
  br label %17

17:                                               ; preds = %14
  store i32 -1, ptr %4, align 4
  br label %52

18:                                               ; preds = %10
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %23, label %21

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

23:                                               ; preds = %18
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.SM4_CBC_CTX, ptr %24, i32 0, i32 3
  %26 = load i64, ptr %25, align 8, !tbaa !11
  %27 = icmp ne i64 %26, 16
  br i1 %27, label %28, label %33

28:                                               ; preds = %23
  br label %29

29:                                               ; preds = %28
  %30 = load ptr, ptr @stderr, align 8, !tbaa !5
  %31 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %30, ptr noundef @.str.4, ptr noundef @.str.1, i32 noundef 232, ptr noundef @__FUNCTION__.sm4_cbc_decrypt_finish) #2
  br label %32

32:                                               ; preds = %29
  store i32 -1, ptr %4, align 4
  br label %52

33:                                               ; preds = %23
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.SM4_CBC_CTX, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.SM4_CBC_CTX, ptr %36, i32 0, i32 1
  %38 = getelementptr inbounds [16 x i8], ptr %37, i64 0, i64 0
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.SM4_CBC_CTX, ptr %39, i32 0, i32 2
  %41 = getelementptr inbounds [16 x i8], ptr %40, i64 0, i64 0
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = call i32 @sm4_cbc_padding_decrypt(ptr noundef %35, ptr noundef %38, ptr noundef %41, i64 noundef 16, ptr noundef %42, ptr noundef %43)
  %45 = icmp ne i32 %44, 1
  br i1 %45, label %46, label %51

46:                                               ; preds = %33
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr @stderr, align 8, !tbaa !5
  %49 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %48, ptr noundef @.str.4, ptr noundef @.str.1, i32 noundef 236, ptr noundef @__FUNCTION__.sm4_cbc_decrypt_finish) #2
  br label %50

50:                                               ; preds = %47
  store i32 -1, ptr %4, align 4
  br label %52

51:                                               ; preds = %33
  store i32 1, ptr %4, align 4
  br label %52

52:                                               ; preds = %51, %50, %32, %21, %17
  %53 = load i32, ptr %4, align 4
  ret i32 %53
}

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 = { 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 #2 = { 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 160}
!12 = !{!"", !13, i64 0, !7, i64 128, !7, i64 144, !10, i64 160}
!13 = !{!"", !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.1 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [13 x i8], align 1
@__FUNCTION__.sm4_cbc_decrypt_finish = external hidden unnamed_addr constant [23 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @sm4_cbc_padding_decrypt(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

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

6:                                                ; preds = %3
  %7 = load ptr, ptr @stderr, align 8, !tbaa !5
  %8 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.1, i32 noundef 224, ptr noundef nonnull @__FUNCTION__.sm4_cbc_decrypt_finish) #2
  br label %24

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

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

11:                                               ; preds = %9
  %12 = getelementptr inbounds i8, ptr %0, i64 160
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %.not14 = icmp eq i64 %13, 16
  br i1 %.not14, label %17, label %14

14:                                               ; preds = %11
  %15 = load ptr, ptr @stderr, align 8, !tbaa !5
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.1, i32 noundef 232, ptr noundef nonnull @__FUNCTION__.sm4_cbc_decrypt_finish) #2
  br label %24

17:                                               ; preds = %11
  %18 = getelementptr inbounds i8, ptr %0, i64 128
  %19 = getelementptr inbounds i8, ptr %0, i64 144
  %20 = tail call i32 @sm4_cbc_padding_decrypt(ptr noundef nonnull %0, ptr noundef nonnull %18, ptr noundef nonnull %19, i64 noundef 16, ptr noundef nonnull %1, ptr noundef nonnull %2)
  %.not15 = icmp eq i32 %20, 1
  br i1 %.not15, label %24, label %21

21:                                               ; preds = %17
  %22 = load ptr, ptr @stderr, align 8, !tbaa !5
  %23 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.1, i32 noundef 236, ptr noundef nonnull @__FUNCTION__.sm4_cbc_decrypt_finish) #2
  br label %24

24:                                               ; preds = %17, %21, %14, %10, %6
  %.0 = phi i32 [ -1, %14 ], [ -1, %21 ], [ 1, %10 ], [ -1, %6 ], [ 1, %17 ]
  ret i32 %.0
}

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 = { 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 #2 = { 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 160}
!12 = !{!"", !13, i64 0, !7, i64 128, !7, i64 144, !10, i64 160}
!13 = !{!"", !7, i64 0}


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