; LLVM IR from microbotsio/CodeCell :: src (shtp)

; 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.shtp_s = type { ptr, ptr, ptr, [8 x i8], i16, i16, [256 x i8], i16, i16, i8, [384 x i8], i16, i32, [384 x i8], i32, [5 x %struct.shtp_App_s], i8, i8, [5 x %struct.shtp_AppListener_s], [8 x %struct.shtp_Channel_s], [8 x %struct.shtp_ChanListener_s], i8, i32, i32, i32, i32, i32 }
%struct.shtp_App_s = type { i32, [32 x i8] }
%struct.shtp_AppListener_s = type { i16, ptr, ptr }
%struct.shtp_Channel_s = type { i8, i8, i32, [32 x i8], i8, ptr, ptr }
%struct.shtp_ChanListener_s = type { i16, [32 x i8], ptr, ptr }
%struct.sh2_Hal_s = type { ptr, ptr, ptr, ptr, ptr }

@advertise = external hidden constant [2 x i8], align 1

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

; 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
declare dso_local i32 @shtp_send(ptr noundef, i8 noundef zeroext, ptr noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local void @shtp_service(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %7, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.shtp_s, ptr %8, i32 0, i32 14
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %23

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.shtp_s, ptr %13, i32 0, i32 14
  store i32 1, ptr %14, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call i32 @shtp_send(ptr noundef %15, i8 noundef zeroext 0, ptr noundef @advertise, i16 noundef zeroext 2)
  store i32 %16, ptr %5, align 4, !tbaa !9
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %22

19:                                               ; preds = %12
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.shtp_s, ptr %20, i32 0, i32 14
  store i32 0, ptr %21, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %19, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %23

23:                                               ; preds = %22, %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.shtp_s, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = getelementptr inbounds %struct.sh2_Hal_s, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !15
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.shtp_s, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.shtp_s, ptr %32, i32 0, i32 13
  %34 = getelementptr inbounds [384 x i8], ptr %33, i64 0, i64 0
  %35 = call i32 %28(ptr noundef %31, ptr noundef %34, i32 noundef 384, ptr noundef %4)
  store i32 %35, ptr %6, align 4, !tbaa !9
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %38, label %46

38:                                               ; preds = %23
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.shtp_s, ptr %40, i32 0, i32 13
  %42 = getelementptr inbounds [384 x i8], ptr %41, i64 0, i64 0
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = trunc i32 %43 to i16
  %45 = load i32, ptr %4, align 4, !tbaa !9
  call void @rxAssemble(ptr noundef %39, ptr noundef %42, i16 noundef zeroext %44, i32 noundef %45)
  br label %46

46:                                               ; preds = %38, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @rxAssemble(ptr noundef, ptr noundef, i16 noundef zeroext, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !10, i64 1072}
!12 = !{!"shtp_s", !6, i64 0, !6, i64 8, !6, i64 16, !7, i64 24, !13, i64 32, !13, i64 34, !7, i64 36, !13, i64 292, !13, i64 294, !7, i64 296, !7, i64 297, !13, i64 682, !10, i64 684, !7, i64 688, !10, i64 1072, !7, i64 1076, !7, i64 1256, !7, i64 1257, !7, i64 1264, !7, i64 1384, !7, i64 1896, !7, i64 2344, !10, i64 2348, !10, i64 2352, !10, i64 2356, !10, i64 2360, !10, i64 2364}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !6, i64 0}
!15 = !{!16, !6, i64 16}
!16 = !{!"sh2_Hal_s", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}


; 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"

@advertise = external hidden constant [2 x i8], align 1

; 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @shtp_send(ptr noundef, i8 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @shtp_service(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %0, i64 1072
  %4 = load i32, ptr %3, align 8, !tbaa !9
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %9

6:                                                ; preds = %1
  store i32 1, ptr %3, align 8, !tbaa !9
  %7 = tail call i32 @shtp_send(ptr noundef nonnull %0, i8 noundef zeroext 0, ptr noundef nonnull @advertise, i16 noundef zeroext 2)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %9, label %8

8:                                                ; preds = %6
  store i32 0, ptr %3, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %6, %8, %1
  %10 = load ptr, ptr %0, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %0, i64 688
  %14 = call i32 %12(ptr noundef %10, ptr noundef nonnull %13, i32 noundef 384, ptr noundef nonnull %2) #2
  %.not12 = icmp eq i32 %14, 0
  br i1 %.not12, label %18, label %15

15:                                               ; preds = %9
  %16 = trunc i32 %14 to i16
  %17 = load i32, ptr %2, align 4, !tbaa !5
  call void @rxAssemble(ptr noundef nonnull %0, ptr noundef nonnull %13, i16 noundef zeroext %16, i32 noundef %17)
  br label %18

18:                                               ; preds = %15, %9
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #2
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @rxAssemble(ptr noundef, ptr noundef, i16 noundef zeroext, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 1072}
!10 = !{!"shtp_s", !11, i64 0, !11, i64 8, !11, i64 16, !7, i64 24, !12, i64 32, !12, i64 34, !7, i64 36, !12, i64 292, !12, i64 294, !7, i64 296, !7, i64 297, !12, i64 682, !6, i64 684, !7, i64 688, !6, i64 1072, !7, i64 1076, !7, i64 1256, !7, i64 1257, !7, i64 1264, !7, i64 1384, !7, i64 1896, !7, i64 2344, !6, i64 2348, !6, i64 2352, !6, i64 2356, !6, i64 2360, !6, i64 2364}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !11, i64 0}
!14 = !{!15, !11, i64 16}
!15 = !{!"sh2_Hal_s", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32}


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