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

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

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

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.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 uwtable
declare dso_local i32 @asn1_bit_string_to_der_ex(i32 noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @asn1_bits_to_der_ex(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i8, align 1
  %12 = alloca [4 x i8], align 1
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #4
  store i8 -128, ptr %11, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  call void @llvm.memset.p0.i64(ptr align 1 %12, i8 0, i64 4, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  store i32 0, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %7, align 4, !tbaa !5
  %16 = icmp slt i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %4
  store i32 0, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %71

18:                                               ; preds = %4
  br label %19

19:                                               ; preds = %51, %18
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = icmp sgt i32 %20, 0
  br i1 %21, label %22, label %52

22:                                               ; preds = %19
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = and i32 %23, 1
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %26, label %36

26:                                               ; preds = %22
  %27 = load i8, ptr %11, align 1, !tbaa !13
  %28 = zext i8 %27 to i32
  %29 = load i32, ptr %13, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [4 x i8], ptr %12, i64 0, i64 %30
  %32 = load i8, ptr %31, align 1, !tbaa !13
  %33 = zext i8 %32 to i32
  %34 = or i32 %33, %28
  %35 = trunc i32 %34 to i8
  store i8 %35, ptr %31, align 1, !tbaa !13
  br label %36

36:                                               ; preds = %26, %22
  %37 = load i8, ptr %11, align 1, !tbaa !13
  %38 = zext i8 %37 to i32
  %39 = ashr i32 %38, 1
  %40 = trunc i32 %39 to i8
  store i8 %40, ptr %11, align 1, !tbaa !13
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = ashr i32 %41, 1
  store i32 %42, ptr %7, align 4, !tbaa !5
  %43 = load i64, ptr %10, align 8, !tbaa !11
  %44 = add i64 %43, 1
  store i64 %44, ptr %10, align 8, !tbaa !11
  %45 = load i64, ptr %10, align 8, !tbaa !11
  %46 = urem i64 %45, 8
  %47 = icmp eq i64 %46, 0
  br i1 %47, label %48, label %51

48:                                               ; preds = %36
  %49 = load i32, ptr %13, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %13, align 4, !tbaa !5
  store i8 -128, ptr %11, align 1, !tbaa !13
  br label %51

51:                                               ; preds = %48, %36
  br label %19, !llvm.loop !14

52:                                               ; preds = %19
  %53 = load i64, ptr %10, align 8, !tbaa !11
  %54 = icmp ne i64 %53, 0
  br i1 %54, label %56, label %55

55:                                               ; preds = %52
  store i64 1, ptr %10, align 8, !tbaa !11
  br label %56

56:                                               ; preds = %55, %52
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = getelementptr inbounds [4 x i8], ptr %12, i64 0, i64 0
  %59 = load i64, ptr %10, align 8, !tbaa !11
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  %61 = load ptr, ptr %9, align 8, !tbaa !9
  %62 = call i32 @asn1_bit_string_to_der_ex(i32 noundef %57, ptr noundef %58, i64 noundef %59, ptr noundef %60, ptr noundef %61)
  %63 = icmp ne i32 %62, 1
  br i1 %63, label %64, label %70

64:                                               ; preds = %56
  br label %65

65:                                               ; preds = %64
  %66 = load ptr, ptr @stderr, align 8, !tbaa !9
  %67 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %66, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 845, ptr noundef @__FUNCTION__.asn1_bits_to_der_ex) #4
  br label %68

68:                                               ; preds = %65
  br label %69

69:                                               ; preds = %68
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %71

70:                                               ; preds = %56
  store i32 1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %71

71:                                               ; preds = %70, %69, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %72 = load i32, ptr %5, align 4
  ret i32 %72
}

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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; 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 [58 x i8], align 1
@__FUNCTION__.asn1_bits_to_der_ex = external hidden unnamed_addr constant [20 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @asn1_bits_to_der_ex(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %5 = alloca [4 x i8], align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #4
  store i32 0, ptr %5, align 4
  %6 = icmp slt i32 %1, 0
  br i1 %6, label %24, label %.preheader

.preheader:                                       ; preds = %4
  %.not25 = icmp eq i32 %1, 0
  br i1 %.not25, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %13
  %.029 = phi i32 [ %spec.select24, %13 ], [ 0, %.preheader ]
  %.01528 = phi i8 [ %spec.select, %13 ], [ -128, %.preheader ]
  %.01727 = phi i64 [ %16, %13 ], [ 0, %.preheader ]
  %.01926 = phi i32 [ %15, %13 ], [ %1, %.preheader ]
  %7 = and i32 %.01926, 1
  %.not23 = icmp eq i32 %7, 0
  br i1 %.not23, label %13, label %8

8:                                                ; preds = %.lr.ph
  %9 = zext nneg i32 %.029 to i64
  %10 = getelementptr inbounds [4 x i8], ptr %5, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = or i8 %11, %.01528
  store i8 %12, ptr %10, align 1, !tbaa !5
  br label %13

13:                                               ; preds = %8, %.lr.ph
  %14 = lshr i8 %.01528, 1
  %15 = lshr i32 %.01926, 1
  %16 = add nuw nsw i64 %.01727, 1
  %17 = and i64 %16, 7
  %18 = icmp eq i64 %17, 0
  %spec.select = select i1 %18, i8 -128, i8 %14
  %19 = zext i1 %18 to i32
  %spec.select24 = add nuw nsw i32 %.029, %19
  %.not = icmp ult i32 %.01926, 2
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %13, %.preheader
  %.017.lcssa = phi i64 [ 0, %.preheader ], [ %16, %13 ]
  %spec.store.select = tail call i64 @llvm.umax.i64(i64 %.017.lcssa, i64 1)
  %20 = call i32 @asn1_bit_string_to_der_ex(i32 noundef %0, ptr noundef nonnull %5, i64 noundef %spec.store.select, ptr noundef %2, ptr noundef %3)
  %.not22 = icmp eq i32 %20, 1
  br i1 %.not22, label %24, label %21

21:                                               ; preds = %._crit_edge
  %22 = load ptr, ptr @stderr, align 8, !tbaa !10
  %23 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 845, ptr noundef nonnull @__FUNCTION__.asn1_bits_to_der_ex) #5
  br label %24

24:                                               ; preds = %._crit_edge, %4, %21
  %.018 = phi i32 [ -1, %21 ], [ 0, %4 ], [ 1, %._crit_edge ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #4
  ret i32 %.018
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #3

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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }
attributes #5 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass