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

; 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.BASE64_CTX = type { i32, i32, [80 x i8], i32, i32 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [57 x i8], align 1
@__FUNCTION__.pem_write = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [18 x i8], align 1

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

17:                                               ; preds = %4
  br label %18

18:                                               ; preds = %17
  %19 = load ptr, ptr @stderr, align 8, !tbaa !5
  %20 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef @.str, ptr noundef @.str.1, i32 noundef 46, ptr noundef @__FUNCTION__.pem_write) #4
  br label %21

21:                                               ; preds = %18
  br label %22

22:                                               ; preds = %21
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %75

23:                                               ; preds = %4
  %24 = load i64, ptr %9, align 8, !tbaa !9
  %25 = icmp ugt i64 %24, 2147483647
  br i1 %25, label %26, label %32

26:                                               ; preds = %23
  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 50, ptr noundef @__FUNCTION__.pem_write) #4
  br label %30

30:                                               ; preds = %27
  br label %31

31:                                               ; preds = %30
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %75

32:                                               ; preds = %23
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef @.str.2, ptr noundef %34) #4
  call void @base64_encode_init(ptr noundef %10)
  br label %36

36:                                               ; preds = %46, %32
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = icmp ne i64 %37, 0
  br i1 %38, label %39, label %65

39:                                               ; preds = %36
  %40 = load i64, ptr %9, align 8, !tbaa !9
  %41 = icmp ult i64 %40, 48
  br i1 %41, label %42, label %45

42:                                               ; preds = %39
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = trunc i64 %43 to i32
  br label %46

45:                                               ; preds = %39
  br label %46

46:                                               ; preds = %45, %42
  %47 = phi i32 [ %44, %42 ], [ 48, %45 ]
  store i32 %47, ptr %12, align 4, !tbaa !11
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load i32, ptr %12, align 4, !tbaa !11
  %50 = getelementptr inbounds [168 x i8], ptr %11, i64 0, i64 0
  %51 = call i32 @base64_encode_update(ptr noundef %10, ptr noundef %48, i32 noundef %49, ptr noundef %50, ptr noundef %13)
  %52 = getelementptr inbounds [168 x i8], ptr %11, i64 0, i64 0
  %53 = load i32, ptr %13, align 4, !tbaa !11
  %54 = sext i32 %53 to i64
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = call i64 @fwrite(ptr noundef %52, i64 noundef 1, i64 noundef %54, ptr noundef %55)
  %57 = load i32, ptr %12, align 4, !tbaa !11
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = sext i32 %57 to i64
  %60 = getelementptr inbounds i8, ptr %58, i64 %59
  store ptr %60, ptr %8, align 8, !tbaa !5
  %61 = load i32, ptr %12, align 4, !tbaa !11
  %62 = sext i32 %61 to i64
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = sub i64 %63, %62
  store i64 %64, ptr %9, align 8, !tbaa !9
  br label %36, !llvm.loop !13

65:                                               ; preds = %36
  %66 = getelementptr inbounds [168 x i8], ptr %11, i64 0, i64 0
  call void @base64_encode_finish(ptr noundef %10, ptr noundef %66, ptr noundef %13)
  %67 = getelementptr inbounds [168 x i8], ptr %11, i64 0, i64 0
  %68 = load i32, ptr %13, align 4, !tbaa !11
  %69 = sext i32 %68 to i64
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = call i64 @fwrite(ptr noundef %67, i64 noundef 1, i64 noundef %69, ptr noundef %70)
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %72, ptr noundef @.str.3, ptr noundef %73) #4
  store i32 1, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %75

75:                                               ; preds = %65, %31, %22
  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 168, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 96, ptr %10) #4
  %76 = load i32, ptr %5, align 4
  ret i32 %76
}

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

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

declare void @base64_encode_init(ptr noundef) #3

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

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%struct.BASE64_CTX = type { i32, i32, [80 x i8], i32, i32 }

@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 [57 x i8], align 1
@__FUNCTION__.pem_write = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [18 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 2) i32 @pem_write(ptr nocapture noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.BASE64_CTX, align 4
  %6 = alloca [168 x i8], align 16
  %7 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 168, ptr nonnull %6) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #5
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %8, label %11

8:                                                ; preds = %4
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 46, ptr noundef nonnull @__FUNCTION__.pem_write) #6
  br label %32

11:                                               ; preds = %4
  %12 = icmp ugt i64 %3, 2147483647
  br i1 %12, label %13, label %16

13:                                               ; preds = %11
  %14 = load ptr, ptr @stderr, align 8, !tbaa !5
  %15 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %14, ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 50, ptr noundef nonnull @__FUNCTION__.pem_write) #6
  br label %32

16:                                               ; preds = %11
  %17 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %0, ptr noundef nonnull @.str.2, ptr noundef %1) #5
  call void @base64_encode_init(ptr noundef nonnull %5) #5
  br label %18

18:                                               ; preds = %16, %18
  %.01722 = phi i64 [ %3, %16 ], [ %26, %18 ]
  %.01821 = phi ptr [ %2, %16 ], [ %25, %18 ]
  %19 = call i64 @llvm.umin.i64(i64 %.01722, i64 48)
  %20 = trunc nuw nsw i64 %19 to i32
  %21 = call i32 @base64_encode_update(ptr noundef nonnull %5, ptr noundef %.01821, i32 noundef %20, ptr noundef nonnull %6, ptr noundef nonnull %7) #5
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = call i64 @fwrite(ptr noundef nonnull %6, i64 noundef 1, i64 noundef %23, ptr noundef %0)
  %25 = getelementptr inbounds i8, ptr %.01821, i64 %19
  %26 = sub i64 %.01722, %19
  %.not20 = icmp eq i64 %26, 0
  br i1 %.not20, label %27, label %18, !llvm.loop !11

27:                                               ; preds = %18
  call void @base64_encode_finish(ptr noundef nonnull %5, ptr noundef nonnull %6, ptr noundef nonnull %7) #5
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = call i64 @fwrite(ptr noundef nonnull %6, i64 noundef 1, i64 noundef %29, ptr noundef %0)
  %31 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %0, ptr noundef nonnull @.str.3, ptr noundef %1) #5
  br label %32

32:                                               ; preds = %27, %13, %8
  %.0 = phi i32 [ -1, %13 ], [ 1, %27 ], [ -1, %8 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #5
  call void @llvm.lifetime.end.p0(i64 168, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %5) #5
  ret i32 %.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: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

declare void @base64_encode_init(ptr noundef) local_unnamed_addr #3

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

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

declare void @base64_encode_finish(ptr noundef, ptr noundef, ptr 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) #1

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind }
attributes #6 = { 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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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