; LLVM IR from kala13x/libxutils :: tools (xtop)

; 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.xapi_ = type { ptr, %struct.XEvents, i64, ptr, i8, i8, i8 }
%struct.XEvents = type { ptr, i32, ptr, ptr, i32, i32, i8, %struct.XHash }
%struct.XHash = type { ptr, ptr, i64, [32 x %struct.xlist] }
%struct.xlist = type { ptr, ptr, %struct.xlist_data, i8 }
%struct.xlist_data = type { ptr, ptr, ptr, i64 }
%struct.xapi_endpoint_ = type { i32, %struct.XSocketCert, ptr, ptr, ptr, i32, i16, i32, i8, i8, i8 }
%struct.XSocketCert = type { ptr, ptr, ptr, ptr, ptr, i32 }
%struct.xmon_args_ = type { ptr, i8, i8, i8, i8, [4096 x i8], [4096 x i8], [256 x i8], [256 x i8], [2048 x i8], [2048 x i8], i64, i16, i16, i16, i8, i32 }

@.str.204 = external hidden unnamed_addr constant [34 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

declare void @XLog_Display(i32 noundef, i8 noundef zeroext, ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XTOPApp_ServiceCb(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @XTOPApp_ServerMode(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca %struct.xapi_, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.xapi_endpoint_, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1896, ptr %6) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = call i32 @XAPI_Init(ptr noundef %6, ptr noundef @XTOPApp_ServiceCb, ptr noundef %10, i64 noundef 0)
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.xmon_args_, ptr %13, i32 0, i32 0
  store ptr %12, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 96, ptr %8) #3
  call void @XAPI_InitEndpoint(ptr noundef %8)
  %15 = getelementptr inbounds %struct.xapi_endpoint_, ptr %8, i32 0, i32 0
  store i32 2, ptr %15, align 8, !tbaa !14
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.xmon_args_, ptr %16, i32 0, i32 6
  %18 = getelementptr inbounds [4096 x i8], ptr %17, i64 0, i64 0
  %19 = getelementptr inbounds %struct.xapi_endpoint_, ptr %8, i32 0, i32 3
  store ptr %18, ptr %19, align 8, !tbaa !17
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.xmon_args_, ptr %20, i32 0, i32 14
  %22 = load i16, ptr %21, align 4, !tbaa !18
  %23 = getelementptr inbounds %struct.xapi_endpoint_, ptr %8, i32 0, i32 6
  store i16 %22, ptr %23, align 4, !tbaa !19
  %24 = call i32 @XAPI_AddEndpoint(ptr noundef %6, ptr noundef %8, i32 noundef 2)
  %25 = icmp slt i32 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %2
  call void @XAPI_Destroy(ptr noundef %6)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %38

27:                                               ; preds = %2
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.xmon_args_, ptr %28, i32 0, i32 14
  %30 = load i16, ptr %29, align 4, !tbaa !18
  %31 = zext i16 %30 to i32
  call void (i32, i8, ptr, ...) @XLog_Display(i32 noundef 2, i8 noundef zeroext 1, ptr noundef @.str.204, i32 noundef %31)
  br label %32

32:                                               ; preds = %34, %27
  %33 = call i32 @XAPI_Service(ptr noundef %6, i32 noundef 100)
  store i32 %33, ptr %7, align 4, !tbaa !20
  br label %34

34:                                               ; preds = %32
  %35 = load i32, ptr %7, align 4, !tbaa !20
  %36 = icmp eq i32 %35, 11
  br i1 %36, label %32, label %37, !llvm.loop !21

37:                                               ; preds = %34
  call void @XAPI_Destroy(ptr noundef %6)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %38

38:                                               ; preds = %37, %26
  call void @llvm.lifetime.end.p0(i64 96, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 1896, ptr %6) #3
  %39 = load i32, ptr %3, align 4
  ret i32 %39
}

declare i32 @XAPI_Init(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #1

declare void @XAPI_InitEndpoint(ptr noundef) #1

declare i32 @XAPI_AddEndpoint(ptr noundef, ptr noundef, i32 noundef) #1

declare void @XAPI_Destroy(ptr noundef) #1

declare i32 @XAPI_Service(ptr noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !6, i64 0}
!10 = !{!"xmon_args_", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !7, i64 12, !7, i64 4108, !7, i64 8204, !7, i64 8460, !7, i64 8716, !7, i64 10764, !11, i64 12816, !12, i64 12824, !12, i64 12826, !12, i64 12828, !7, i64 12830, !13, i64 12832}
!11 = !{!"long", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !13, i64 0}
!15 = !{!"xapi_endpoint_", !13, i64 0, !16, i64 8, !6, i64 56, !6, i64 64, !6, i64 72, !13, i64 80, !12, i64 84, !13, i64 88, !7, i64 92, !7, i64 93, !7, i64 94}
!16 = !{!"XSocketCert", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !13, i64 40}
!17 = !{!15, !6, i64 64}
!18 = !{!10, !12, i64 12828}
!19 = !{!15, !12, i64 84}
!20 = !{!13, !13, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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"

%struct.xapi_ = type { ptr, %struct.XEvents, i64, ptr, i8, i8, i8 }
%struct.XEvents = type { ptr, i32, ptr, ptr, i32, i32, i8, %struct.XHash }
%struct.XHash = type { ptr, ptr, i64, [32 x %struct.xlist] }
%struct.xlist = type { ptr, ptr, %struct.xlist_data, i8 }
%struct.xlist_data = type { ptr, ptr, ptr, i64 }
%struct.xapi_endpoint_ = type { i32, %struct.XSocketCert, ptr, ptr, ptr, i32, i16, i32, i8, i8, i8 }
%struct.XSocketCert = type { ptr, ptr, ptr, ptr, ptr, i32 }

@.str.204 = external hidden unnamed_addr constant [34 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

declare void @XLog_Display(i32 noundef, i8 noundef zeroext, ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @XTOPApp_ServiceCb(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @XTOPApp_ServerMode(ptr noundef %0, ptr noundef %1) local_unnamed_addr #2 {
  %3 = alloca %struct.xapi_, align 8
  %4 = alloca %struct.xapi_endpoint_, align 8
  call void @llvm.lifetime.start.p0(i64 1896, ptr nonnull %3) #3
  %5 = call i32 @XAPI_Init(ptr noundef nonnull %3, ptr noundef nonnull @XTOPApp_ServiceCb, ptr noundef %0, i64 noundef 0) #3
  store ptr %1, ptr %0, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 96, ptr nonnull %4) #3
  call void @XAPI_InitEndpoint(ptr noundef nonnull %4) #3
  store i32 2, ptr %4, align 8, !tbaa !13
  %6 = getelementptr inbounds i8, ptr %0, i64 4108
  %7 = getelementptr inbounds i8, ptr %4, i64 64
  store ptr %6, ptr %7, align 8, !tbaa !16
  %8 = getelementptr inbounds i8, ptr %0, i64 12828
  %9 = load i16, ptr %8, align 4, !tbaa !17
  %10 = getelementptr inbounds i8, ptr %4, i64 84
  store i16 %9, ptr %10, align 4, !tbaa !18
  %11 = call i32 @XAPI_AddEndpoint(ptr noundef nonnull %3, ptr noundef nonnull %4, i32 noundef 2) #3
  %12 = icmp slt i32 %11, 0
  br i1 %12, label %.loopexit, label %13

13:                                               ; preds = %2
  %14 = load i16, ptr %8, align 4, !tbaa !17
  %15 = zext i16 %14 to i32
  call void (i32, i8, ptr, ...) @XLog_Display(i32 noundef 2, i8 noundef zeroext 1, ptr noundef nonnull @.str.204, i32 noundef %15) #3
  br label %16

16:                                               ; preds = %16, %13
  %17 = call i32 @XAPI_Service(ptr noundef nonnull %3, i32 noundef 100) #3
  %18 = icmp eq i32 %17, 11
  br i1 %18, label %16, label %.loopexit, !llvm.loop !19

.loopexit:                                        ; preds = %16, %2
  %.0 = phi i32 [ -1, %2 ], [ 0, %16 ]
  call void @XAPI_Destroy(ptr noundef nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 96, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 1896, ptr nonnull %3) #3
  ret i32 %.0
}

declare i32 @XAPI_Init(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare void @XAPI_InitEndpoint(ptr noundef) local_unnamed_addr #1

declare i32 @XAPI_AddEndpoint(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

declare void @XAPI_Destroy(ptr noundef) local_unnamed_addr #1

declare i32 @XAPI_Service(ptr noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !7, i64 0}
!6 = !{!"xmon_args_", !7, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !8, i64 12, !8, i64 4108, !8, i64 8204, !8, i64 8460, !8, i64 8716, !8, i64 10764, !10, i64 12816, !11, i64 12824, !11, i64 12826, !11, i64 12828, !8, i64 12830, !12, i64 12832}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"xapi_endpoint_", !12, i64 0, !15, i64 8, !7, i64 56, !7, i64 64, !7, i64 72, !12, i64 80, !11, i64 84, !12, i64 88, !8, i64 92, !8, i64 93, !8, i64 94}
!15 = !{!"XSocketCert", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !12, i64 40}
!16 = !{!14, !7, i64 64}
!17 = !{!6, !11, i64 12828}
!18 = !{!14, !11, i64 84}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass