; LLVM IR from specinfo-ua/UAPKI :: library/uapkif/src/asn1 (per_encoder)

; 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.asn_enc_rval_s = type { i64, ptr, ptr }
%struct.enc_dyn_arg = type { ptr, i64, i64 }

; Function Attrs: nounwind uwtable
declare hidden void @uper_encode_internal(ptr dead_on_unwind noalias writable sret(%struct.asn_enc_rval_s) align 8, ptr noundef, ptr noundef, ptr noundef, 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: nounwind uwtable
define dso_local i64 @uper_encode_to_new_buffer(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.asn_enc_rval_s, align 8
  %11 = alloca %struct.enc_dyn_arg, align 8
  %12 = alloca %struct.asn_enc_rval_s, align 8
  %13 = 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 ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr %10) #6
  call void @llvm.lifetime.start.p0(i64 24, ptr %11) #6
  call void @llvm.memset.p0.i64(ptr align 8 %11, i8 0, i64 24, i1 false)
  call void @llvm.lifetime.start.p0(i64 24, ptr %12) #6
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = load ptr, ptr %8, align 8, !tbaa !5
  call void @uper_encode_internal(ptr dead_on_unwind writable sret(%struct.asn_enc_rval_s) align 8 %12, ptr noundef %14, ptr noundef %15, ptr noundef %16, ptr noundef @encode_dyn_cb, ptr noundef %11)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %10, ptr align 8 %12, i64 24, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 24, ptr %12) #6
  %17 = getelementptr inbounds %struct.asn_enc_rval_s, ptr %10, i32 0, i32 0
  %18 = load i64, ptr %17, align 8, !tbaa !12
  switch i64 %18, label %39 [
    i64 -1, label %19
    i64 0, label %23
  ]

19:                                               ; preds = %4
  %20 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  call void @free(ptr noundef %21) #6
  %22 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  store ptr null, ptr %22, align 8, !tbaa !14
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %47

23:                                               ; preds = %4
  %24 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  call void @free(ptr noundef %25) #6
  %26 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  store ptr null, ptr %26, align 8, !tbaa !14
  %27 = call noalias ptr @malloc(i64 noundef 1) #7
  %28 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  store ptr %27, ptr %28, align 8, !tbaa !14
  %29 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  %31 = icmp ne ptr %30, null
  br i1 %31, label %32, label %38

32:                                               ; preds = %23
  %33 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  store i8 0, ptr %34, align 1, !tbaa !16
  %35 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !14
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %36, ptr %37, align 8, !tbaa !5
  store i64 1, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %47

38:                                               ; preds = %23
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %47

39:                                               ; preds = %4
  %40 = getelementptr inbounds %struct.enc_dyn_arg, ptr %11, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %41, ptr %42, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.asn_enc_rval_s, ptr %10, i32 0, i32 0
  %44 = load i64, ptr %43, align 8, !tbaa !12
  %45 = add nsw i64 %44, 7
  %46 = ashr i64 %45, 3
  store i64 %46, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %47

47:                                               ; preds = %39, %38, %32, %19
  call void @llvm.lifetime.end.p0(i64 24, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr %10) #6
  %48 = load i64, ptr %5, align 8
  ret i64 %48
}

; 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 hidden i32 @encode_dyn_cb(ptr noundef, i64 noundef, ptr noundef) #0

; 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) #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #4

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #5

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind allocsize(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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }

!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 = !{i64 0, i64 8, !10, i64 8, i64 8, !5, i64 16, i64 8, !5}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !11, i64 0}
!13 = !{!"asn_enc_rval_s", !11, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!15, !6, i64 0}
!15 = !{!"enc_dyn_arg", !6, i64 0, !11, i64 8, !11, i64 16}
!16 = !{!7, !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"

%struct.asn_enc_rval_s = type { i64, ptr, ptr }
%struct.enc_dyn_arg = type { ptr, i64, i64 }

; Function Attrs: nounwind uwtable
declare hidden void @uper_encode_internal(ptr dead_on_unwind noalias writable sret(%struct.asn_enc_rval_s) align 8, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) 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
define dso_local range(i64 -1152921504606846976, 1152921504606846976) i64 @uper_encode_to_new_buffer(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca %struct.enc_dyn_arg, align 8
  %6 = alloca %struct.asn_enc_rval_s, align 8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %5) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %5, i8 0, i64 24, i1 false)
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %6) #5
  call void @uper_encode_internal(ptr dead_on_unwind nonnull writable sret(%struct.asn_enc_rval_s) align 8 %6, ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef nonnull @encode_dyn_cb, ptr noundef nonnull %5)
  %.sroa.0.0.copyload = load i64, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %6) #5
  %7 = load ptr, ptr %5, align 8, !tbaa !9
  switch i64 %.sroa.0.0.copyload, label %12 [
    i64 -1, label %8
    i64 0, label %9
  ]

8:                                                ; preds = %4
  call void @free(ptr noundef %7) #5
  br label %15

9:                                                ; preds = %4
  call void @free(ptr noundef %7) #5
  %10 = call noalias dereferenceable_or_null(1) ptr @malloc(i64 noundef 1) #6
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %15, label %11

11:                                               ; preds = %9
  store i8 0, ptr %10, align 1, !tbaa !12
  store ptr %10, ptr %3, align 8, !tbaa !13
  br label %15

12:                                               ; preds = %4
  store ptr %7, ptr %3, align 8, !tbaa !13
  %13 = add nsw i64 %.sroa.0.0.copyload, 7
  %14 = ashr i64 %13, 3
  br label %15

15:                                               ; preds = %9, %12, %11, %8
  %.0 = phi i64 [ %14, %12 ], [ 1, %11 ], [ -1, %8 ], [ -1, %9 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %5) #5
  ret i64 %.0
}

; Function Attrs: mustprogress 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 hidden i32 @encode_dyn_cb(ptr noundef, i64 noundef, ptr noundef) #0

; 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!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 = !{!10, !11, i64 0}
!10 = !{!"enc_dyn_arg", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!11, !11, i64 0}


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