; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (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"

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind
declare void @free(ptr 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 uwtable
define dso_local i32 @mbedtls_pem_write_buffer(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr noundef %6) #4 {
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i32, align 4
  %17 = alloca ptr, align 8
  %18 = alloca ptr, align 8
  %19 = alloca ptr, align 8
  %20 = alloca i64, align 8
  %21 = alloca i64, align 8
  %22 = alloca i64, align 8
  %23 = alloca i32, align 4
  store ptr %0, ptr %9, align 8, !tbaa !5
  store ptr %1, ptr %10, align 8, !tbaa !5
  store ptr %2, ptr %11, align 8, !tbaa !5
  store i64 %3, ptr %12, align 8, !tbaa !9
  store ptr %4, ptr %13, align 8, !tbaa !5
  store i64 %5, ptr %14, align 8, !tbaa !9
  store ptr %6, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #7
  %24 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %24, ptr %19, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #7
  store i64 0, ptr %20, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %21) #7
  call void @llvm.lifetime.start.p0(i64 8, ptr %22) #7
  store i64 0, ptr %22, align 8, !tbaa !9
  %25 = load ptr, ptr %11, align 8, !tbaa !5
  %26 = load i64, ptr %12, align 8, !tbaa !9
  %27 = call i32 @mbedtls_base64_encode(ptr noundef null, i64 noundef 0, ptr noundef %21, ptr noundef %25, i64 noundef %26)
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = call i64 @strlen(ptr noundef %28) #8
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = call i64 @strlen(ptr noundef %30) #8
  %32 = add i64 %29, %31
  %33 = load i64, ptr %21, align 8, !tbaa !9
  %34 = udiv i64 %33, 64
  %35 = add i64 %32, %34
  %36 = add i64 %35, 1
  store i64 %36, ptr %22, align 8, !tbaa !9
  %37 = load i64, ptr %21, align 8, !tbaa !9
  %38 = load i64, ptr %22, align 8, !tbaa !9
  %39 = add i64 %37, %38
  %40 = load i64, ptr %14, align 8, !tbaa !9
  %41 = icmp ugt i64 %39, %40
  br i1 %41, label %42, label %47

42:                                               ; preds = %7
  %43 = load i64, ptr %21, align 8, !tbaa !9
  %44 = load i64, ptr %22, align 8, !tbaa !9
  %45 = add i64 %43, %44
  %46 = load ptr, ptr %15, align 8, !tbaa !5
  store i64 %45, ptr %46, align 8, !tbaa !9
  store i32 -42, ptr %8, align 4
  store i32 1, ptr %23, align 4
  br label %115

47:                                               ; preds = %7
  %48 = load i64, ptr %21, align 8, !tbaa !9
  %49 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %48) #9
  store ptr %49, ptr %17, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %51, label %52

51:                                               ; preds = %47
  store i32 -4480, ptr %8, align 4
  store i32 1, ptr %23, align 4
  br label %115

52:                                               ; preds = %47
  %53 = load ptr, ptr %17, align 8, !tbaa !5
  %54 = load i64, ptr %21, align 8, !tbaa !9
  %55 = load ptr, ptr %11, align 8, !tbaa !5
  %56 = load i64, ptr %12, align 8, !tbaa !9
  %57 = call i32 @mbedtls_base64_encode(ptr noundef %53, i64 noundef %54, ptr noundef %21, ptr noundef %55, i64 noundef %56)
  store i32 %57, ptr %16, align 4, !tbaa !11
  %58 = icmp ne i32 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %52
  %60 = load ptr, ptr %17, align 8, !tbaa !5
  call void @free(ptr noundef %60) #7
  %61 = load i32, ptr %16, align 4, !tbaa !11
  store i32 %61, ptr %8, align 4
  store i32 1, ptr %23, align 4
  br label %115

62:                                               ; preds = %52
  %63 = load ptr, ptr %19, align 8, !tbaa !5
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = call i64 @strlen(ptr noundef %65) #8
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %63, ptr align 1 %64, i64 %66, i1 false)
  %67 = load ptr, ptr %9, align 8, !tbaa !5
  %68 = call i64 @strlen(ptr noundef %67) #8
  %69 = load ptr, ptr %19, align 8, !tbaa !5
  %70 = getelementptr inbounds i8, ptr %69, i64 %68
  store ptr %70, ptr %19, align 8, !tbaa !5
  %71 = load ptr, ptr %17, align 8, !tbaa !5
  store ptr %71, ptr %18, align 8, !tbaa !5
  br label %72

72:                                               ; preds = %81, %62
  %73 = load i64, ptr %21, align 8, !tbaa !9
  %74 = icmp ne i64 %73, 0
  br i1 %74, label %75, label %97

75:                                               ; preds = %72
  %76 = load i64, ptr %21, align 8, !tbaa !9
  %77 = icmp ugt i64 %76, 64
  br i1 %77, label %78, label %79

78:                                               ; preds = %75
  br label %81

79:                                               ; preds = %75
  %80 = load i64, ptr %21, align 8, !tbaa !9
  br label %81

81:                                               ; preds = %79, %78
  %82 = phi i64 [ 64, %78 ], [ %80, %79 ]
  store i64 %82, ptr %20, align 8, !tbaa !9
  %83 = load ptr, ptr %19, align 8, !tbaa !5
  %84 = load ptr, ptr %18, align 8, !tbaa !5
  %85 = load i64, ptr %20, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %83, ptr align 1 %84, i64 %85, i1 false)
  %86 = load i64, ptr %20, align 8, !tbaa !9
  %87 = load i64, ptr %21, align 8, !tbaa !9
  %88 = sub i64 %87, %86
  store i64 %88, ptr %21, align 8, !tbaa !9
  %89 = load i64, ptr %20, align 8, !tbaa !9
  %90 = load ptr, ptr %19, align 8, !tbaa !5
  %91 = getelementptr inbounds i8, ptr %90, i64 %89
  store ptr %91, ptr %19, align 8, !tbaa !5
  %92 = load i64, ptr %20, align 8, !tbaa !9
  %93 = load ptr, ptr %18, align 8, !tbaa !5
  %94 = getelementptr inbounds i8, ptr %93, i64 %92
  store ptr %94, ptr %18, align 8, !tbaa !5
  %95 = load ptr, ptr %19, align 8, !tbaa !5
  %96 = getelementptr inbounds i8, ptr %95, i32 1
  store ptr %96, ptr %19, align 8, !tbaa !5
  store i8 10, ptr %95, align 1, !tbaa !13
  br label %72, !llvm.loop !14

97:                                               ; preds = %72
  %98 = load ptr, ptr %19, align 8, !tbaa !5
  %99 = load ptr, ptr %10, align 8, !tbaa !5
  %100 = load ptr, ptr %10, align 8, !tbaa !5
  %101 = call i64 @strlen(ptr noundef %100) #8
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %98, ptr align 1 %99, i64 %101, i1 false)
  %102 = load ptr, ptr %10, align 8, !tbaa !5
  %103 = call i64 @strlen(ptr noundef %102) #8
  %104 = load ptr, ptr %19, align 8, !tbaa !5
  %105 = getelementptr inbounds i8, ptr %104, i64 %103
  store ptr %105, ptr %19, align 8, !tbaa !5
  %106 = load ptr, ptr %19, align 8, !tbaa !5
  %107 = getelementptr inbounds i8, ptr %106, i32 1
  store ptr %107, ptr %19, align 8, !tbaa !5
  store i8 0, ptr %106, align 1, !tbaa !13
  %108 = load ptr, ptr %19, align 8, !tbaa !5
  %109 = load ptr, ptr %13, align 8, !tbaa !5
  %110 = ptrtoint ptr %108 to i64
  %111 = ptrtoint ptr %109 to i64
  %112 = sub i64 %110, %111
  %113 = load ptr, ptr %15, align 8, !tbaa !5
  store i64 %112, ptr %113, align 8, !tbaa !9
  %114 = load ptr, ptr %17, align 8, !tbaa !5
  call void @free(ptr noundef %114) #7
  store i32 0, ptr %8, align 4
  store i32 1, ptr %23, align 4
  br label %115

115:                                              ; preds = %97, %59, %51, %42
  call void @llvm.lifetime.end.p0(i64 8, ptr %22) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %21) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #7
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #7
  %116 = load i32, ptr %8, align 4
  ret i32 %116
}

declare i32 @mbedtls_base64_encode(ptr noundef, i64 noundef, ptr noundef, ptr noundef, i64 noundef) #5

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #6

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind allocsize(0,1) "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 "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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { nounwind allocsize(0,1) }

!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 = !{!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"

; 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 nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture 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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_pem_write_buffer(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, ptr noundef %2, i64 noundef %3, ptr noundef %4, i64 noundef %5, ptr nocapture noundef writeonly %6) local_unnamed_addr #4 {
  %8 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %8) #8
  %9 = call i32 @mbedtls_base64_encode(ptr noundef null, i64 noundef 0, ptr noundef nonnull %8, ptr noundef %2, i64 noundef %3) #8
  %10 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #9
  %11 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #9
  %12 = load i64, ptr %8, align 8, !tbaa !5
  %13 = lshr i64 %12, 6
  %14 = add i64 %10, 1
  %15 = add i64 %14, %11
  %16 = add i64 %15, %12
  %17 = add i64 %16, %13
  %18 = icmp ugt i64 %17, %5
  br i1 %18, label %19, label %20

19:                                               ; preds = %7
  store i64 %17, ptr %6, align 8, !tbaa !5
  br label %43

20:                                               ; preds = %7
  %21 = call noalias ptr @calloc(i64 noundef 1, i64 noundef %12) #10
  %22 = icmp eq ptr %21, null
  br i1 %22, label %43, label %23

23:                                               ; preds = %20
  %24 = call i32 @mbedtls_base64_encode(ptr noundef nonnull %21, i64 noundef %12, ptr noundef nonnull %8, ptr noundef %2, i64 noundef %3) #8
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %26, label %25

25:                                               ; preds = %23
  call void @free(ptr noundef nonnull %21) #8
  br label %43

26:                                               ; preds = %23
  %27 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %4, ptr align 1 %0, i64 %27, i1 false)
  %28 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #9
  %29 = getelementptr inbounds i8, ptr %4, i64 %28
  %.pr = load i64, ptr %8, align 8
  %.not4546 = icmp eq i64 %.pr, 0
  br i1 %.not4546, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %26, %.lr.ph
  %.048 = phi ptr [ %35, %.lr.ph ], [ %29, %26 ]
  %.03947 = phi ptr [ %34, %.lr.ph ], [ %21, %26 ]
  %30 = phi i64 [ %32, %.lr.ph ], [ %.pr, %26 ]
  %31 = call i64 @llvm.umin.i64(i64 %30, i64 64)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %.048, ptr noundef nonnull align 1 dereferenceable(1) %.03947, i64 %31, i1 false)
  %32 = sub i64 %30, %31
  %33 = getelementptr inbounds i8, ptr %.048, i64 %31
  %34 = getelementptr inbounds i8, ptr %.03947, i64 %31
  %35 = getelementptr inbounds i8, ptr %33, i64 1
  store i8 10, ptr %33, align 1, !tbaa !9
  %.not45 = icmp eq i64 %32, 0
  br i1 %.not45, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %26
  %.0.lcssa = phi ptr [ %29, %26 ], [ %35, %.lr.ph ]
  %36 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.0.lcssa, ptr align 1 %1, i64 %36, i1 false)
  %37 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #9
  %38 = getelementptr inbounds i8, ptr %.0.lcssa, i64 %37
  %39 = getelementptr inbounds i8, ptr %38, i64 1
  store i8 0, ptr %38, align 1, !tbaa !9
  %40 = ptrtoint ptr %39 to i64
  %41 = ptrtoint ptr %4 to i64
  %42 = sub i64 %40, %41
  store i64 %42, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %21) #8
  br label %43

43:                                               ; preds = %20, %._crit_edge, %25, %19
  %.040 = phi i32 [ -42, %19 ], [ %24, %25 ], [ 0, %._crit_edge ], [ -4480, %20 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %8) #8
  ret i32 %.040
}

declare i32 @mbedtls_base64_encode(ptr noundef, i64 noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #5

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #6

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

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nounwind willreturn memory(argmem: 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 #2 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { "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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #7 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #8 = { nounwind }
attributes #9 = { nounwind willreturn memory(read) }
attributes #10 = { nounwind allocsize(0,1) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass