; LLVM IR from MRPT/mrpt :: 3rdparty/xspublic/xstypes (xssocket)

; 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.sockaddr_storage = type { i16, [118 x i8], i64 }
%struct.XsSocket = type { ptr }
%struct.XsSocketPrivate = type { i32, i32, i32, %struct.sockaddr_storage, i32, i32, i32, i32, [32768 x i8] }

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

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

; 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 @XsSocket_select(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XsSocket_writeTo(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i16 noundef zeroext %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i16, align 2
  %12 = alloca %struct.sockaddr_storage, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i16 %4, ptr %11, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 128, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr null, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = call i32 @XsSocket_select(ptr noundef %19, i32 noundef 0, ptr noundef null, ptr noundef %16)
  store i32 %20, ptr %17, align 4, !tbaa !13
  %21 = load i32, ptr %16, align 4, !tbaa !13
  %22 = load i32, ptr %17, align 4, !tbaa !13
  %23 = icmp sle i32 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %5
  %25 = load i32, ptr %17, align 4, !tbaa !13
  store i32 %25, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %59

26:                                               ; preds = %5
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.XsSocket, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = getelementptr inbounds %struct.XsSocketPrivate, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 8, !tbaa !17
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %42

33:                                               ; preds = %26
  store ptr %12, ptr %13, align 8, !tbaa !5
  store i32 128, ptr %14, align 4, !tbaa !13
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = load ptr, ptr %10, align 8, !tbaa !5
  %36 = load i16, ptr %11, align 2, !tbaa !11
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = call i32 @XsSocket_lookup(ptr noundef %34, ptr noundef %35, i16 noundef zeroext %36, ptr noundef %37, ptr noundef %14)
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %41

40:                                               ; preds = %33
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %59

41:                                               ; preds = %33
  br label %42

42:                                               ; preds = %41, %26
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.XsSocket, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !15
  %46 = getelementptr inbounds %struct.XsSocketPrivate, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !20
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = trunc i64 %49 to i32
  %51 = sext i32 %50 to i64
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = load i32, ptr %14, align 4, !tbaa !13
  %54 = call i64 @sendto(i32 noundef %47, ptr noundef %48, i64 noundef %51, i32 noundef 16384, ptr noundef %52, i32 noundef %53)
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %15, align 4, !tbaa !13
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load i32, ptr %15, align 4, !tbaa !13
  call void @translateSocketError(ptr noundef %56, i32 noundef %57)
  %58 = load i32, ptr %15, align 4, !tbaa !13
  store i32 %58, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %59

59:                                               ; preds = %42, %40, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr %12) #3
  %60 = load i32, ptr %6, align 4
  ret i32 %60
}

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

declare i64 @sendto(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i32 noundef) #2

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 = { "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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"XsSocket", !6, i64 0}
!17 = !{!18, !14, i64 8}
!18 = !{!"XsSocketPrivate", !14, i64 0, !14, i64 4, !14, i64 8, !19, i64 16, !14, i64 144, !14, i64 148, !14, i64 152, !14, i64 156, !7, i64 160}
!19 = !{!"sockaddr_storage", !12, i64 0, !7, i64 2, !10, i64 120}
!20 = !{!18, !14, 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.sockaddr_storage = type { i16, [118 x i8], i64 }

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

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

; 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 @XsSocket_select(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XsSocket_writeTo(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i16 noundef zeroext %4) local_unnamed_addr #1 {
  %6 = alloca %struct.sockaddr_storage, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %8) #3
  %9 = call i32 @XsSocket_select(ptr noundef %0, i32 noundef 0, ptr noundef null, ptr noundef nonnull %8)
  %10 = icmp slt i32 %9, 1
  br i1 %10, label %25, label %11

11:                                               ; preds = %5
  %12 = load ptr, ptr %0, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load i32, ptr %13, align 8, !tbaa !12
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %18

16:                                               ; preds = %11
  store i32 128, ptr %7, align 4, !tbaa !5
  %17 = call i32 @XsSocket_lookup(ptr noundef nonnull %0, ptr noundef %3, i16 noundef zeroext %4, ptr noundef nonnull %6, ptr noundef nonnull %7)
  %.not = icmp eq i32 %17, 0
  br i1 %.not, label %._crit_edge, label %25

._crit_edge:                                      ; preds = %16
  %.pre = load ptr, ptr %0, align 8, !tbaa !9
  %.pre17 = load i32, ptr %7, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %._crit_edge, %11
  %19 = phi i32 [ %.pre17, %._crit_edge ], [ 0, %11 ]
  %20 = phi ptr [ %.pre, %._crit_edge ], [ %12, %11 ]
  %.015 = phi ptr [ %6, %._crit_edge ], [ null, %11 ]
  %21 = load i32, ptr %20, align 8, !tbaa !17
  %sext = shl i64 %2, 32
  %22 = ashr exact i64 %sext, 32
  %23 = call i64 @sendto(i32 noundef %21, ptr noundef %1, i64 noundef %22, i32 noundef 16384, ptr noundef %.015, i32 noundef %19) #3
  %24 = trunc i64 %23 to i32
  call void @translateSocketError(ptr noundef nonnull %0, i32 noundef %24)
  br label %25

25:                                               ; preds = %16, %5, %18
  %.0 = phi i32 [ %24, %18 ], [ %9, %5 ], [ -1, %16 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %6) #3
  ret i32 %.0
}

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

declare i64 @sendto(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"XsSocket", !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"XsSocketPrivate", !6, i64 0, !6, i64 4, !6, i64 8, !14, i64 16, !6, i64 144, !6, i64 148, !6, i64 152, !6, i64 156, !7, i64 160}
!14 = !{!"sockaddr_storage", !15, i64 0, !7, i64 2, !16, i64 120}
!15 = !{!"short", !7, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!13, !6, i64 0}


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