; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (x509write_crt)

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

@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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: nounwind uwtable
declare dso_local i32 @mbedtls_x509write_crt_set_extension(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef) #2

declare i32 @mbedtls_asn1_write_len(ptr noundef, ptr noundef, i64 noundef) #3

declare i32 @mbedtls_asn1_write_tag(ptr noundef, ptr noundef, i8 noundef zeroext) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509write_crt_set_subject_key_identifier(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [2068 x i8], align 16
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 2068, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %9 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %10 = getelementptr inbounds i8, ptr %9, i64 2068
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !9
  %11 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %11, i8 0, i64 2068, i1 false)
  br label %12

12:                                               ; preds = %1
  %13 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.mbedtls_x509write_cert, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = call i32 @mbedtls_pk_write_pubkey(ptr noundef %6, ptr noundef %13, ptr noundef %16)
  store i32 %17, ptr %4, align 4, !tbaa !15
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %12
  %20 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %20, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %78

21:                                               ; preds = %12
  %22 = load i32, ptr %4, align 4, !tbaa !15
  %23 = sext i32 %22 to i64
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = add i64 %24, %23
  store i64 %25, ptr %7, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %21
  br label %27

27:                                               ; preds = %26
  br label %28

28:                                               ; preds = %27
  %29 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %30 = getelementptr inbounds i8, ptr %29, i64 2068
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = sub i64 0, %31
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %36 = getelementptr inbounds i8, ptr %35, i64 2068
  %37 = getelementptr inbounds i8, ptr %36, i64 -20
  call void @mbedtls_sha1(ptr noundef %33, i64 noundef %34, ptr noundef %37)
  %38 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %39 = getelementptr inbounds i8, ptr %38, i64 2068
  %40 = getelementptr inbounds i8, ptr %39, i64 -20
  store ptr %40, ptr %6, align 8, !tbaa !5
  store i64 20, ptr %7, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %28
  %42 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = call i32 @mbedtls_asn1_write_len(ptr noundef %6, ptr noundef %42, i64 noundef %43)
  store i32 %44, ptr %4, align 4, !tbaa !15
  %45 = icmp slt i32 %44, 0
  br i1 %45, label %46, label %48

46:                                               ; preds = %41
  %47 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %47, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %78

48:                                               ; preds = %41
  %49 = load i32, ptr %4, align 4, !tbaa !15
  %50 = sext i32 %49 to i64
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = add i64 %51, %50
  store i64 %52, ptr %7, align 8, !tbaa !9
  br label %53

53:                                               ; preds = %48
  br label %54

54:                                               ; preds = %53
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55
  %57 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %58 = call i32 @mbedtls_asn1_write_tag(ptr noundef %6, ptr noundef %57, i8 noundef zeroext 4)
  store i32 %58, ptr %4, align 4, !tbaa !15
  %59 = icmp slt i32 %58, 0
  br i1 %59, label %60, label %62

60:                                               ; preds = %56
  %61 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %61, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %78

62:                                               ; preds = %56
  %63 = load i32, ptr %4, align 4, !tbaa !15
  %64 = sext i32 %63 to i64
  %65 = load i64, ptr %7, align 8, !tbaa !9
  %66 = add i64 %65, %64
  store i64 %66, ptr %7, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %62
  br label %68

68:                                               ; preds = %67
  br label %69

69:                                               ; preds = %68
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %72 = getelementptr inbounds i8, ptr %71, i64 2068
  %73 = load i64, ptr %7, align 8, !tbaa !9
  %74 = sub i64 0, %73
  %75 = getelementptr inbounds i8, ptr %72, i64 %74
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = call i32 @mbedtls_x509write_crt_set_extension(ptr noundef %70, ptr noundef @.str.1, i64 noundef 3, i32 noundef 0, ptr noundef %75, i64 noundef %76)
  store i32 %77, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %78

78:                                               ; preds = %69, %60, %46, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 2068, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %79 = load i32, ptr %2, align 4
  ret i32 %79
}

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

declare void @mbedtls_sha1(ptr noundef, i64 noundef, ptr noundef) #3

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { 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 = { "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, !6, i64 32}
!12 = !{!"mbedtls_x509write_cert", !13, i64 0, !14, i64 8, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !13, i64 64, !7, i64 68, !7, i64 84, !6, i64 104}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !13, i64 0, !10, i64 8, !6, i64 16}
!15 = !{!13, !13, 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"

@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 @mbedtls_x509write_crt_set_extension(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare i32 @mbedtls_asn1_write_len(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

declare i32 @mbedtls_asn1_write_tag(ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509write_crt_set_subject_key_identifier(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca [2068 x i8], align 16
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 2068, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  %4 = getelementptr inbounds i8, ptr %2, i64 2068
  store ptr %4, ptr %3, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(2068) %2, i8 0, i64 2068, i1 false)
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = call i32 @mbedtls_pk_write_pubkey(ptr noundef nonnull %3, ptr noundef nonnull %2, ptr noundef %6) #4
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %26, label %9

9:                                                ; preds = %1
  %10 = zext nneg i32 %7 to i64
  %11 = sub nsw i64 0, %10
  %12 = getelementptr inbounds i8, ptr %4, i64 %11
  %13 = getelementptr inbounds i8, ptr %2, i64 2048
  call void @mbedtls_sha1(ptr noundef nonnull %12, i64 noundef %10, ptr noundef nonnull %13) #4
  store ptr %13, ptr %3, align 8, !tbaa !5
  %14 = call i32 @mbedtls_asn1_write_len(ptr noundef nonnull %3, ptr noundef nonnull %2, i64 noundef 20) #4
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %26, label %16

16:                                               ; preds = %9
  %17 = call i32 @mbedtls_asn1_write_tag(ptr noundef nonnull %3, ptr noundef nonnull %2, i8 noundef zeroext 4) #4
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %26, label %19

19:                                               ; preds = %16
  %narrow = add nuw i32 %14, 20
  %20 = zext i32 %narrow to i64
  %21 = zext nneg i32 %17 to i64
  %22 = add nuw nsw i64 %21, %20
  %23 = sub nsw i64 0, %22
  %24 = getelementptr inbounds i8, ptr %4, i64 %23
  %25 = call i32 @mbedtls_x509write_crt_set_extension(ptr noundef nonnull %0, ptr noundef nonnull @.str.1, i64 noundef 3, i32 noundef 0, ptr noundef nonnull %24, i64 noundef %22)
  br label %26

26:                                               ; preds = %16, %9, %1, %19
  %.0 = phi i32 [ %25, %19 ], [ %7, %1 ], [ %14, %9 ], [ %17, %16 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 2068, ptr nonnull %2) #4
  ret i32 %.0
}

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

declare void @mbedtls_sha1(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { "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, !6, i64 32}
!10 = !{!"mbedtls_x509write_cert", !11, i64 0, !12, i64 8, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !11, i64 64, !7, i64 68, !7, i64 84, !6, i64 104}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !11, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"long", !7, i64 0}


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