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

; 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: nounwind uwtable
declare dso_local i32 @mbedtls_asn1_get_tag(ptr noundef, ptr noundef, ptr noundef, i32 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 i32 @mbedtls_asn1_get_int(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = call i32 @mbedtls_asn1_get_tag(ptr noundef %11, ptr noundef %12, ptr noundef %9, i32 noundef 2)
  store i32 %13, ptr %8, align 4, !tbaa !9
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %17

15:                                               ; preds = %3
  %16 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %16, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

17:                                               ; preds = %3
  %18 = load i64, ptr %9, align 8, !tbaa !11
  %19 = icmp eq i64 %18, 0
  br i1 %19, label %30, label %20

20:                                               ; preds = %17
  %21 = load i64, ptr %9, align 8, !tbaa !11
  %22 = icmp ugt i64 %21, 4
  br i1 %22, label %30, label %23

23:                                               ; preds = %20
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %26 = load i8, ptr %25, align 1, !tbaa !13
  %27 = zext i8 %26 to i32
  %28 = and i32 %27, 128
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %23, %20, %17
  store i32 -100, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

31:                                               ; preds = %23
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %32, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %37, %31
  %34 = load i64, ptr %9, align 8, !tbaa !11
  %35 = add i64 %34, -1
  store i64 %35, ptr %9, align 8, !tbaa !11
  %36 = icmp ugt i64 %34, 0
  br i1 %36, label %37, label %50

37:                                               ; preds = %33
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = shl i32 %39, 8
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load ptr, ptr %41, align 8, !tbaa !5
  %43 = load i8, ptr %42, align 1, !tbaa !13
  %44 = zext i8 %43 to i32
  %45 = or i32 %40, %44
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %45, ptr %46, align 4, !tbaa !9
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = load ptr, ptr %47, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i32 1
  store ptr %49, ptr %47, align 8, !tbaa !5
  br label %33, !llvm.loop !14

50:                                               ; preds = %33
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

51:                                               ; preds = %50, %30, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %52 = load i32, ptr %4, align 4
  ret i32 %52
}

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 }

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_asn1_get_tag(ptr noundef, ptr noundef, ptr noundef, i32 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 i32 @mbedtls_asn1_get_int(ptr noundef %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #2
  %5 = call i32 @mbedtls_asn1_get_tag(ptr noundef %0, ptr noundef %1, ptr noundef nonnull %4, i32 noundef 2)
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %6, label %.loopexit

6:                                                ; preds = %3
  %7 = load i64, ptr %4, align 8
  %8 = add i64 %7, -5
  %or.cond = icmp ult i64 %8, -4
  br i1 %or.cond, label %.loopexit, label %9

9:                                                ; preds = %6
  %10 = load ptr, ptr %0, align 8, !tbaa !5
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %.not11 = icmp sgt i8 %11, -1
  br i1 %.not11, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %9
  store i32 0, ptr %2, align 4, !tbaa !10
  %12 = add nsw i64 %7, -1
  store i64 %12, ptr %4, align 8, !tbaa !12
  %13 = load i8, ptr %10, align 1, !tbaa !9
  %14 = zext i8 %13 to i32
  store i32 %14, ptr %2, align 4, !tbaa !10
  %15 = getelementptr inbounds i8, ptr %10, i64 1
  store ptr %15, ptr %0, align 8, !tbaa !5
  %16 = add nsw i64 %7, -2
  store i64 %16, ptr %4, align 8, !tbaa !12
  %.not12 = icmp eq i64 %12, 0
  br i1 %.not12, label %.loopexit, label %17, !llvm.loop !14

17:                                               ; preds = %.lr.ph
  %18 = shl nuw nsw i32 %14, 8
  %19 = load i8, ptr %15, align 1, !tbaa !9
  %20 = zext i8 %19 to i32
  %21 = or disjoint i32 %18, %20
  store i32 %21, ptr %2, align 4, !tbaa !10
  %22 = getelementptr inbounds i8, ptr %10, i64 2
  store ptr %22, ptr %0, align 8, !tbaa !5
  %23 = add nsw i64 %7, -3
  store i64 %23, ptr %4, align 8, !tbaa !12
  %.not12.1 = icmp eq i64 %16, 0
  br i1 %.not12.1, label %.loopexit, label %24, !llvm.loop !14

24:                                               ; preds = %17
  %25 = shl nuw nsw i32 %21, 8
  %26 = load i8, ptr %22, align 1, !tbaa !9
  %27 = zext i8 %26 to i32
  %28 = or disjoint i32 %25, %27
  store i32 %28, ptr %2, align 4, !tbaa !10
  %29 = getelementptr inbounds i8, ptr %10, i64 3
  store ptr %29, ptr %0, align 8, !tbaa !5
  %30 = add nsw i64 %7, -4
  store i64 %30, ptr %4, align 8, !tbaa !12
  %.not12.2 = icmp eq i64 %23, 0
  br i1 %.not12.2, label %.loopexit, label %31, !llvm.loop !14

31:                                               ; preds = %24
  %32 = shl nuw i32 %28, 8
  %33 = load i8, ptr %29, align 1, !tbaa !9
  %34 = zext i8 %33 to i32
  %35 = or disjoint i32 %32, %34
  store i32 %35, ptr %2, align 4, !tbaa !10
  %36 = getelementptr inbounds i8, ptr %10, i64 4
  store ptr %36, ptr %0, align 8, !tbaa !5
  %37 = add nsw i64 %7, -5
  store i64 %37, ptr %4, align 8, !tbaa !12
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %17, %24, %31, %6, %9, %3
  %.0 = phi i32 [ %5, %3 ], [ -100, %9 ], [ -100, %6 ], [ 0, %31 ], [ 0, %24 ], [ 0, %17 ], [ 0, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #2
  ret i32 %.0
}

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 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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