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

@shtp_initialized = external hidden global i8, align 1
@.str = external hidden unnamed_addr constant [8 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @shtp_open(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load i8, ptr @shtp_initialized, align 1, !tbaa !9, !range !11, !noundef !12
  %7 = trunc i8 %6 to i1
  br i1 %7, label %9, label %8

8:                                                ; preds = %1
  call void @shtp_init()
  br label %9

9:                                                ; preds = %8, %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store ptr null, ptr %2, align 8
  br label %48

13:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %14 = call ptr @getInstance()
  store ptr %14, ptr %4, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %13
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %47

18:                                               ; preds = %13
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %19, i8 0, i64 2368, i1 false)
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.shtp_s, ptr %21, i32 0, i32 0
  store ptr %20, ptr %22, align 8, !tbaa !13
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.shtp_s, ptr %23, i32 0, i32 1
  store ptr null, ptr %24, align 8, !tbaa !17
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.shtp_s, ptr %25, i32 0, i32 2
  store ptr null, ptr %26, align 8, !tbaa !18
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.shtp_s, ptr %27, i32 0, i32 4
  store i16 256, ptr %28, align 8, !tbaa !19
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.shtp_s, ptr %29, i32 0, i32 5
  store i16 256, ptr %30, align 2, !tbaa !20
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  call void @addApp(ptr noundef %31, i32 noundef 0)
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  call void @addChannel(ptr noundef %32, i8 noundef zeroext 0, i32 noundef 0, ptr noundef @.str, i1 noundef zeroext false)
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = call i32 @shtp_listenAdvert(ptr noundef %33, i16 noundef zeroext 0, ptr noundef @shtpAdvertHdlr, ptr noundef %34)
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = call i32 @shtp_listenChan(ptr noundef %36, i16 noundef zeroext 0, ptr noundef @.str, ptr noundef @shtpCmdListener, ptr noundef %37)
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.shtp_s, ptr %39, i32 0, i32 14
  store i32 1, ptr %40, align 8, !tbaa !21
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.sh2_Hal_s, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !22
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = call i32 %43(ptr noundef %44)
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %46, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %47

47:                                               ; preds = %18, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %48

48:                                               ; preds = %47, %12
  %49 = load ptr, ptr %2, align 8
  ret ptr %49
}

; Function Attrs: nounwind uwtable
declare hidden void @shtp_init() #0

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

; Function Attrs: nounwind uwtable
declare hidden ptr @getInstance() #0

; 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 void @addApp(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @addChannel(ptr noundef, i8 noundef zeroext, i32 noundef, ptr noundef, i1 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @shtp_listenAdvert(ptr noundef, i16 noundef zeroext, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @shtpAdvertHdlr(ptr noundef, i8 noundef zeroext, i8 noundef zeroext, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @shtp_listenChan(ptr noundef, i16 noundef zeroext, ptr noundef, ptr noundef, ptr noundef) #0

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

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

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 = { 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 = !{!"_Bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !6, i64 0}
!14 = !{!"shtp_s", !6, i64 0, !6, i64 8, !6, i64 16, !7, i64 24, !15, i64 32, !15, i64 34, !7, i64 36, !15, i64 292, !15, i64 294, !7, i64 296, !7, i64 297, !15, i64 682, !16, i64 684, !7, i64 688, !16, i64 1072, !7, i64 1076, !7, i64 1256, !7, i64 1257, !7, i64 1264, !7, i64 1384, !7, i64 1896, !7, i64 2344, !16, i64 2348, !16, i64 2352, !16, i64 2356, !16, i64 2360, !16, i64 2364}
!15 = !{!"short", !7, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!14, !6, i64 8}
!18 = !{!14, !6, i64 16}
!19 = !{!14, !15, i64 32}
!20 = !{!14, !15, i64 34}
!21 = !{!14, !16, i64 1072}
!22 = !{!23, !6, i64 0}
!23 = !{!"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"

@shtp_initialized = external hidden local_unnamed_addr global i8, align 1
@.str = external hidden unnamed_addr constant [8 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @shtp_open(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr @shtp_initialized, align 1, !tbaa !5, !range !9, !noundef !10
  %3 = trunc nuw i8 %2 to i1
  br i1 %3, label %5, label %4

4:                                                ; preds = %1
  tail call void @shtp_init()
  br label %5

5:                                                ; preds = %4, %1
  %6 = icmp eq ptr %0, null
  br i1 %6, label %20, label %7

7:                                                ; preds = %5
  %8 = tail call ptr @getInstance()
  %9 = icmp eq ptr %8, null
  br i1 %9, label %20, label %10

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %8, i64 24
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(2368) %11, i8 0, i64 2344, i1 false)
  store ptr %0, ptr %8, align 8, !tbaa !11
  %12 = getelementptr inbounds i8, ptr %8, i64 8
  %13 = getelementptr inbounds i8, ptr %8, i64 32
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %12, i8 0, i64 16, i1 false)
  store i16 256, ptr %13, align 8, !tbaa !16
  %14 = getelementptr inbounds i8, ptr %8, i64 34
  store i16 256, ptr %14, align 2, !tbaa !17
  tail call void @addApp(ptr noundef nonnull %8, i32 noundef 0)
  tail call void @addChannel(ptr noundef nonnull %8, i8 noundef zeroext 0, i32 noundef 0, ptr noundef nonnull @.str, i1 noundef zeroext false)
  %15 = tail call i32 @shtp_listenAdvert(ptr noundef nonnull %8, i16 noundef zeroext 0, ptr noundef nonnull @shtpAdvertHdlr, ptr noundef nonnull %8)
  %16 = tail call i32 @shtp_listenChan(ptr noundef nonnull %8, i16 noundef zeroext 0, ptr noundef nonnull @.str, ptr noundef nonnull @shtpCmdListener, ptr noundef nonnull %8)
  %17 = getelementptr inbounds i8, ptr %8, i64 1072
  store i32 1, ptr %17, align 8, !tbaa !18
  %18 = load ptr, ptr %0, align 8, !tbaa !19
  %19 = tail call i32 %18(ptr noundef nonnull %0) #2
  br label %20

20:                                               ; preds = %10, %7, %5
  %.1 = phi ptr [ null, %5 ], [ %8, %10 ], [ null, %7 ]
  ret ptr %.1
}

; Function Attrs: nounwind uwtable
declare hidden void @shtp_init() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @getInstance() local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
declare hidden void @addApp(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @addChannel(ptr noundef, i8 noundef zeroext, i32 noundef, ptr noundef, i1 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @shtp_listenAdvert(ptr noundef, i16 noundef zeroext, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @shtpAdvertHdlr(ptr noundef, i8 noundef zeroext, i8 noundef zeroext, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @shtp_listenChan(ptr noundef, i16 noundef zeroext, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @shtpCmdListener(ptr noundef, ptr noundef, i16 noundef zeroext, i32 noundef) #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 nounwind willreturn memory(argmem: write) }
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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !13, i64 0}
!12 = !{!"shtp_s", !13, i64 0, !13, i64 8, !13, i64 16, !7, i64 24, !14, i64 32, !14, i64 34, !7, i64 36, !14, i64 292, !14, i64 294, !7, i64 296, !7, i64 297, !14, i64 682, !15, i64 684, !7, i64 688, !15, i64 1072, !7, i64 1076, !7, i64 1256, !7, i64 1257, !7, i64 1264, !7, i64 1384, !7, i64 1896, !7, i64 2344, !15, i64 2348, !15, i64 2352, !15, i64 2356, !15, i64 2360, !15, i64 2364}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !14, i64 32}
!17 = !{!12, !14, i64 34}
!18 = !{!12, !15, i64 1072}
!19 = !{!20, !13, i64 0}
!20 = !{!"sh2_Hal_s", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}


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