; LLVM IR from ohler55/agoo :: ext/agoo (server)

; 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._agooServer = type { i8, i8, i32, i8, i8, i8, i8, i64, %struct._agooQueue, ptr, ptr, ptr, %struct._agooQueue, ptr, i32, i32, %struct._agooAtom, ptr, ptr, %union.pthread_mutex_t, i32, ptr, ptr, %struct._agooAtom }
%struct._agooQueue = type { ptr, ptr, %struct._agooAtom, %struct._agooAtom, i8, i8, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, i32, i32 }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct._agooAtom = type { ptr, %union.pthread_mutex_t }
%struct._agooErr = type { i32, [256 x i8] }

@agoo_server = external dso_local global %struct._agooServer, align 8
@agoo_io_loop_ratio = external dso_local global double, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @agoo_server_setup(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 @agoo_server, i8 0, i64 784, i1 false)
  %5 = call i32 @pthread_mutex_init(ptr noundef getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 19), ptr noundef null) #5
  store ptr null, ptr getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 17), align 8, !tbaa !9
  store ptr null, ptr getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 18), align 8, !tbaa !16
  store i32 32, ptr getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 20), align 8, !tbaa !17
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = call i32 @agoo_pages_init(ptr noundef %6)
  %8 = icmp ne i32 0, %7
  br i1 %8, label %17, label %9

9:                                                ; preds = %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call i32 @agoo_queue_multi_init(ptr noundef %10, ptr noundef getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 8), i64 noundef 1024, i1 noundef zeroext false, i1 noundef zeroext true)
  %12 = icmp ne i32 0, %11
  br i1 %12, label %17, label %13

13:                                               ; preds = %9
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call i32 @agoo_queue_multi_init(ptr noundef %14, ptr noundef getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 12), i64 noundef 1024, i1 noundef zeroext true, i1 noundef zeroext true)
  %16 = icmp ne i32 0, %15
  br i1 %16, label %17, label %21

17:                                               ; preds = %13, %9, %1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._agooErr, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 4, !tbaa !18
  store i32 %20, ptr %2, align 4
  br label %38

21:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  store i32 4, ptr getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 14), align 8, !tbaa !20
  %22 = call i64 @sysconf(i32 noundef 84) #5
  store i64 %22, ptr %4, align 8, !tbaa !21
  %23 = icmp slt i64 0, %22
  br i1 %23, label %24, label %37

24:                                               ; preds = %21
  %25 = load i64, ptr %4, align 8, !tbaa !21
  %26 = sitofp i64 %25 to double
  %27 = load double, ptr @agoo_io_loop_ratio, align 8, !tbaa !22
  %28 = fmul double %26, %27
  %29 = fptosi double %28 to i32
  %30 = sext i32 %29 to i64
  store i64 %30, ptr %4, align 8, !tbaa !21
  %31 = load i64, ptr %4, align 8, !tbaa !21
  %32 = icmp sge i64 1, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %24
  store i64 1, ptr %4, align 8, !tbaa !21
  br label %34

34:                                               ; preds = %33, %24
  %35 = load i64, ptr %4, align 8, !tbaa !21
  %36 = trunc i64 %35 to i32
  store i32 %36, ptr getelementptr inbounds (%struct._agooServer, ptr @agoo_server, i32 0, i32 14), align 8, !tbaa !20
  br label %37

37:                                               ; preds = %34, %21
  store i32 0, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  br label %38

38:                                               ; preds = %37, %17
  %39 = load i32, ptr %2, align 4
  ret i32 %39
}

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) #2

declare i32 @agoo_pages_init(ptr noundef) #3

declare i32 @agoo_queue_multi_init(ptr noundef, ptr noundef, i64 noundef, i1 noundef zeroext, i1 noundef zeroext) #3

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

; Function Attrs: nounwind
declare i64 @sysconf(i32 noundef) #2

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

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 nounwind willreturn memory(argmem: write) }
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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 656}
!10 = !{!"_agooServer", !11, i64 0, !11, i64 1, !12, i64 4, !11, i64 8, !11, i64 9, !11, i64 10, !11, i64 11, !13, i64 16, !14, i64 24, !6, i64 296, !6, i64 304, !6, i64 312, !14, i64 320, !6, i64 592, !12, i64 600, !12, i64 604, !15, i64 608, !6, i64 656, !6, i64 664, !7, i64 672, !12, i64 712, !6, i64 720, !6, i64 728, !15, i64 736}
!11 = !{!"_Bool", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"_agooQueue", !6, i64 0, !6, i64 8, !15, i64 16, !15, i64 64, !11, i64 112, !11, i64 113, !15, i64 120, !15, i64 168, !15, i64 216, !12, i64 264, !12, i64 268}
!15 = !{!"_agooAtom", !6, i64 0, !7, i64 8}
!16 = !{!10, !6, i64 664}
!17 = !{!10, !12, i64 712}
!18 = !{!19, !12, i64 0}
!19 = !{!"_agooErr", !12, i64 0, !7, i64 4}
!20 = !{!10, !12, i64 600}
!21 = !{!13, !13, i64 0}
!22 = !{!23, !23, i64 0}
!23 = !{!"double", !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"

%struct._agooServer = type { i8, i8, i32, i8, i8, i8, i8, i64, %struct._agooQueue, ptr, ptr, ptr, %struct._agooQueue, ptr, i32, i32, %struct._agooAtom, ptr, ptr, %union.pthread_mutex_t, i32, ptr, ptr, %struct._agooAtom }
%struct._agooQueue = type { ptr, ptr, %struct._agooAtom, %struct._agooAtom, i8, i8, %struct._agooAtom, %struct._agooAtom, %struct._agooAtom, i32, i32 }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct._agooAtom = type { ptr, %union.pthread_mutex_t }

@agoo_server = external dso_local global %struct._agooServer, align 8
@agoo_io_loop_ratio = external dso_local local_unnamed_addr global double, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @agoo_server_setup(ptr noundef %0) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(784) @agoo_server, i8 0, i64 784, i1 false)
  %2 = tail call i32 @pthread_mutex_init(ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_server, i64 672), ptr noundef null) #5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) getelementptr inbounds (i8, ptr @agoo_server, i64 656), i8 0, i64 16, i1 false)
  store i32 32, ptr getelementptr inbounds (i8, ptr @agoo_server, i64 712), align 8, !tbaa !5
  %3 = tail call i32 @agoo_pages_init(ptr noundef %0) #5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %8

4:                                                ; preds = %1
  %5 = tail call i32 @agoo_queue_multi_init(ptr noundef %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_server, i64 24), i64 noundef 1024, i1 noundef zeroext false, i1 noundef zeroext true) #5
  %.not8 = icmp eq i32 %5, 0
  br i1 %.not8, label %6, label %8

6:                                                ; preds = %4
  %7 = tail call i32 @agoo_queue_multi_init(ptr noundef %0, ptr noundef nonnull getelementptr inbounds (i8, ptr @agoo_server, i64 320), i64 noundef 1024, i1 noundef zeroext true, i1 noundef zeroext true) #5
  %.not9 = icmp eq i32 %7, 0
  br i1 %.not9, label %10, label %8

8:                                                ; preds = %6, %4, %1
  %9 = load i32, ptr %0, align 4, !tbaa !15
  br label %18

10:                                               ; preds = %6
  store i32 4, ptr getelementptr inbounds (i8, ptr @agoo_server, i64 600), align 8, !tbaa !17
  %11 = tail call i64 @sysconf(i32 noundef 84) #5
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %13, label %18

13:                                               ; preds = %10
  %14 = uitofp nneg i64 %11 to double
  %15 = load double, ptr @agoo_io_loop_ratio, align 8, !tbaa !18
  %16 = fmul double %15, %14
  %17 = fptosi double %16 to i32
  %narrow = tail call i32 @llvm.smax.i32(i32 %17, i32 1)
  store i32 %narrow, ptr getelementptr inbounds (i8, ptr @agoo_server, i64 600), align 8, !tbaa !17
  br label %18

18:                                               ; preds = %10, %13, %8
  %.0 = phi i32 [ %9, %8 ], [ 0, %13 ], [ 0, %10 ]
  ret i32 %.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
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @agoo_pages_init(ptr noundef) local_unnamed_addr #3

declare i32 @agoo_queue_multi_init(ptr noundef, ptr noundef, i64 noundef, i1 noundef zeroext, i1 noundef zeroext) local_unnamed_addr #3

; Function Attrs: nounwind
declare i64 @sysconf(i32 noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #4

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 "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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { 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, !10, i64 712}
!6 = !{!"_agooServer", !7, i64 0, !7, i64 1, !10, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !11, i64 16, !12, i64 24, !13, i64 296, !13, i64 304, !13, i64 312, !12, i64 320, !13, i64 592, !10, i64 600, !10, i64 604, !14, i64 608, !13, i64 656, !13, i64 664, !8, i64 672, !10, i64 712, !13, i64 720, !13, i64 728, !14, i64 736}
!7 = !{!"_Bool", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"_agooQueue", !13, i64 0, !13, i64 8, !14, i64 16, !14, i64 64, !7, i64 112, !7, i64 113, !14, i64 120, !14, i64 168, !14, i64 216, !10, i64 264, !10, i64 268}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!"_agooAtom", !13, i64 0, !8, i64 8}
!15 = !{!16, !10, i64 0}
!16 = !{!"_agooErr", !10, i64 0, !8, i64 4}
!17 = !{!6, !10, i64 600}
!18 = !{!19, !19, i64 0}
!19 = !{!"double", !8, i64 0}


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