; LLVM IR from rweather/lightweight-crypto :: src/genavr/interpret.ll (ZN8AVRState3ptrEih)

; 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.AVRState = type <{ [34 x i8], i8, i8, i8, [4096 x i8], [3 x i8], i32, i32, %class.Sbox, i32, [4 x i8] }>
%class.Sbox = type { %"class.std::vector" }
%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl" }
%"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl" = type { %"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl_data" }
%"struct.std::_Vector_base<unsigned char, std::allocator<unsigned char>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@.str = external hidden unnamed_addr constant [23 x i8], align 1
@_ZTISt16invalid_argument = external constant ptr

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN8AVRState3ptrEih(ptr noundef nonnull align 8 dereferenceable(4172) %0, i32 noundef %1, i8 noundef zeroext %2) #0 align 2 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i8 %2, ptr %6, align 1, !tbaa !11
  %10 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %11 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 0
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = add nsw i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [34 x i8], ptr %11, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !11
  %17 = zext i8 %16 to i32
  %18 = shl i32 %17, 8
  %19 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 0
  %20 = load i32, ptr %5, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [34 x i8], ptr %19, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !11
  %24 = zext i8 %23 to i32
  %25 = or i32 %18, %24
  store i32 %25, ptr %7, align 4, !tbaa !9
  %26 = load i8, ptr %6, align 1, !tbaa !11
  %27 = zext i8 %26 to i32
  %28 = icmp eq i32 %27, 254
  br i1 %28, label %29, label %46

29:                                               ; preds = %3
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = add nsw i32 %30, -1
  store i32 %31, ptr %7, align 4, !tbaa !9
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = trunc i32 %32 to i8
  %34 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 0
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [34 x i8], ptr %34, i64 0, i64 %36
  store i8 %33, ptr %37, align 1, !tbaa !11
  %38 = load i32, ptr %7, align 4, !tbaa !9
  %39 = ashr i32 %38, 8
  %40 = trunc i32 %39 to i8
  %41 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 0
  %42 = load i32, ptr %5, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [34 x i8], ptr %41, i64 0, i64 %44
  store i8 %40, ptr %45, align 1, !tbaa !11
  br label %73

46:                                               ; preds = %3
  %47 = load i8, ptr %6, align 1, !tbaa !11
  %48 = zext i8 %47 to i32
  %49 = icmp eq i32 %48, 255
  br i1 %49, label %50, label %67

50:                                               ; preds = %46
  %51 = load i32, ptr %7, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  %53 = trunc i32 %52 to i8
  %54 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 0
  %55 = load i32, ptr %5, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds [34 x i8], ptr %54, i64 0, i64 %56
  store i8 %53, ptr %57, align 1, !tbaa !11
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = add nsw i32 %58, 1
  %60 = ashr i32 %59, 8
  %61 = trunc i32 %60 to i8
  %62 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 0
  %63 = load i32, ptr %5, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [34 x i8], ptr %62, i64 0, i64 %65
  store i8 %61, ptr %66, align 1, !tbaa !11
  br label %72

67:                                               ; preds = %46
  %68 = load i8, ptr %6, align 1, !tbaa !11
  %69 = zext i8 %68 to i32
  %70 = load i32, ptr %7, align 4, !tbaa !9
  %71 = add nsw i32 %70, %69
  store i32 %71, ptr %7, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %67, %50
  br label %73

73:                                               ; preds = %72, %29
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = icmp slt i32 %74, 0
  br i1 %75, label %79, label %76

76:                                               ; preds = %73
  %77 = load i32, ptr %7, align 4, !tbaa !9
  %78 = icmp sge i32 %77, 4096
  br i1 %78, label %79, label %86

79:                                               ; preds = %76, %73
  %80 = call ptr @__cxa_allocate_exception(i64 16) #4
  invoke void @_ZNSt16invalid_argumentC1EPKc(ptr noundef nonnull align 8 dereferenceable(16) %80, ptr noundef @.str)
          to label %81 unwind label %82

81:                                               ; preds = %79
  call void @__cxa_throw(ptr %80, ptr @_ZTISt16invalid_argument, ptr @_ZNSt16invalid_argumentD1Ev) #5
  unreachable

82:                                               ; preds = %79
  %83 = landingpad { ptr, i32 }
          cleanup
  %84 = extractvalue { ptr, i32 } %83, 0
  store ptr %84, ptr %8, align 8
  %85 = extractvalue { ptr, i32 } %83, 1
  store i32 %85, ptr %9, align 4
  call void @__cxa_free_exception(ptr %80) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %91

86:                                               ; preds = %76
  %87 = getelementptr inbounds %struct.AVRState, ptr %10, i32 0, i32 4
  %88 = load i32, ptr %7, align 4, !tbaa !9
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [4096 x i8], ptr %87, i64 0, i64 %89
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret ptr %90

91:                                               ; preds = %82
  %92 = load ptr, ptr %8, align 8
  %93 = load i32, ptr %9, align 4
  %94 = insertvalue { ptr, i32 } poison, ptr %92, 0
  %95 = insertvalue { ptr, i32 } %94, i32 %93, 1
  resume { ptr, i32 } %95
}

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

declare ptr @__cxa_allocate_exception(i64)

declare void @_ZNSt16invalid_argumentC1EPKc(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef) unnamed_addr #2

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr)

; Function Attrs: nounwind
declare void @_ZNSt16invalid_argumentD1Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #3

declare void @__cxa_throw(ptr, ptr, ptr)

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

attributes #0 = { mustprogress 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 = { "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 }
attributes #5 = { noreturn }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!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"

@.str = external hidden unnamed_addr constant [23 x i8], align 1
@_ZTISt16invalid_argument = external constant ptr

; Function Attrs: mustprogress uwtable
define dso_local noundef nonnull ptr @_ZN8AVRState3ptrEih(ptr noundef nonnull align 8 dereferenceable(4172) %0, i32 noundef %1, i8 noundef zeroext %2) local_unnamed_addr #0 align 2 personality ptr @__gxx_personality_v0 {
  %4 = add nsw i32 %1, 1
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [34 x i8], ptr %0, i64 0, i64 %5
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %8 = zext i8 %7 to i32
  %9 = shl nuw nsw i32 %8, 8
  %10 = sext i32 %1 to i64
  %11 = getelementptr inbounds [34 x i8], ptr %0, i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %13 = zext i8 %12 to i32
  %14 = or disjoint i32 %9, %13
  switch i8 %2, label %25 [
    i8 -2, label %15
    i8 -1, label %20
  ]

15:                                               ; preds = %3
  %16 = add nsw i32 %14, -1
  %17 = trunc i32 %16 to i8
  store i8 %17, ptr %11, align 1, !tbaa !5
  %18 = lshr i32 %16, 8
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %6, align 1, !tbaa !5
  br label %28

20:                                               ; preds = %3
  %21 = add nuw nsw i32 %14, 1
  %22 = trunc i32 %21 to i8
  store i8 %22, ptr %11, align 1, !tbaa !5
  %23 = lshr i32 %21, 8
  %24 = trunc i32 %23 to i8
  store i8 %24, ptr %6, align 1, !tbaa !5
  br label %28

25:                                               ; preds = %3
  %26 = zext i8 %2 to i32
  %27 = add nuw nsw i32 %14, %26
  br label %28

28:                                               ; preds = %20, %25, %15
  %.0 = phi i32 [ %16, %15 ], [ %14, %20 ], [ %27, %25 ]
  %or.cond = icmp ugt i32 %.0, 4095
  br i1 %or.cond, label %29, label %34

29:                                               ; preds = %28
  %30 = tail call ptr @__cxa_allocate_exception(i64 16) #3
  invoke void @_ZNSt16invalid_argumentC1EPKc(ptr noundef nonnull align 8 dereferenceable(16) %30, ptr noundef nonnull @.str)
          to label %31 unwind label %32

31:                                               ; preds = %29
  tail call void @__cxa_throw(ptr nonnull %30, ptr nonnull @_ZTISt16invalid_argument, ptr nonnull @_ZNSt16invalid_argumentD1Ev) #4
  unreachable

32:                                               ; preds = %29
  %33 = landingpad { ptr, i32 }
          cleanup
  tail call void @__cxa_free_exception(ptr %30) #3
  resume { ptr, i32 } %33

34:                                               ; preds = %28
  %35 = getelementptr inbounds i8, ptr %0, i64 37
  %36 = zext nneg i32 %.0 to i64
  %37 = getelementptr inbounds [4096 x i8], ptr %35, i64 0, i64 %36
  ret ptr %37
}

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

declare void @_ZNSt16invalid_argumentC1EPKc(ptr noundef nonnull align 8 dereferenceable(16), ptr noundef) unnamed_addr #1

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; Function Attrs: nounwind
declare void @_ZNSt16invalid_argumentD1Ev(ptr noundef nonnull align 8 dereferenceable(16)) unnamed_addr #2

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

attributes #0 = { mustprogress 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 = { "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 "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 }
attributes #4 = { noreturn }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}


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