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

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

; Function Attrs: nounwind uwtable
define dso_local void @XsSocket_createFromNativeSocket(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load i32, ptr %10, align 4, !tbaa !9
  call void @XsSocket_initialize(ptr noundef %12, i32 noundef %13)
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.XsSocket, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.XsSocketPrivate, ptr %17, i32 0, i32 0
  store i32 %14, ptr %18, align 8, !tbaa !13
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %21, label %34

21:                                               ; preds = %5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.XsSocket, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds %struct.XsSocketPrivate, ptr %24, i32 0, i32 3
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %25, ptr align 2 %26, i64 %28, i1 false)
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.XsSocket, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = getelementptr inbounds %struct.XsSocketPrivate, ptr %32, i32 0, i32 4
  store i32 %29, ptr %33, align 8, !tbaa !18
  br label %55

34:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.XsSocket, ptr %35, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !11
  %38 = getelementptr inbounds %struct.XsSocketPrivate, ptr %37, i32 0, i32 4
  store i32 128, ptr %38, align 8, !tbaa !18
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.XsSocket, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds %struct.XsSocketPrivate, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !13
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.XsSocket, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = getelementptr inbounds %struct.XsSocketPrivate, ptr %46, i32 0, i32 3
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.XsSocket, ptr %48, i32 0, i32 0
  %50 = load ptr, ptr %49, align 8, !tbaa !11
  %51 = getelementptr inbounds %struct.XsSocketPrivate, ptr %50, i32 0, i32 4
  %52 = call i32 @getpeername(i32 noundef %43, ptr noundef %47, ptr noundef %51) #4
  store i32 %52, ptr %11, align 4, !tbaa !9
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i32, ptr %11, align 4, !tbaa !9
  call void @translateSocketError(ptr noundef %53, i32 noundef %54)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %55

55:                                               ; preds = %34, %21
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.XsSocket, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !11
  %59 = getelementptr inbounds %struct.XsSocketPrivate, ptr %58, i32 0, i32 3
  %60 = getelementptr inbounds %struct.sockaddr_storage, ptr %59, i32 0, i32 0
  %61 = load i16, ptr %60, align 8, !tbaa !19
  %62 = zext i16 %61 to i32
  switch i32 %62, label %73 [
    i32 10, label %63
    i32 2, label %68
  ]

63:                                               ; preds = %55
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.XsSocket, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !11
  %67 = getelementptr inbounds %struct.XsSocketPrivate, ptr %66, i32 0, i32 1
  store i32 1, ptr %67, align 4, !tbaa !20
  br label %74

68:                                               ; preds = %55
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.XsSocket, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !11
  %72 = getelementptr inbounds %struct.XsSocketPrivate, ptr %71, i32 0, i32 1
  store i32 0, ptr %72, align 4, !tbaa !20
  br label %74

73:                                               ; preds = %55
  br label %74

74:                                               ; preds = %73, %68, %63
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind
declare i32 @getpeername(i32 noundef, ptr noundef, ptr noundef) #3

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

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

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: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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, !6, i64 0}
!12 = !{!"XsSocket", !6, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"XsSocketPrivate", !10, i64 0, !10, i64 4, !10, i64 8, !15, i64 16, !10, i64 144, !10, i64 148, !10, i64 152, !10, i64 156, !7, i64 160}
!15 = !{!"sockaddr_storage", !16, i64 0, !7, i64 2, !17, i64 120}
!16 = !{!"short", !7, i64 0}
!17 = !{!"long", !7, i64 0}
!18 = !{!14, !10, i64 144}
!19 = !{!14, !16, i64 16}
!20 = !{!14, !10, i64 4}


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

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

; Function Attrs: nounwind uwtable
define dso_local void @XsSocket_createFromNativeSocket(ptr noundef %0, i32 noundef %1, ptr noundef readonly %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  tail call void @XsSocket_initialize(ptr noundef %0, i32 noundef %4)
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  store i32 %1, ptr %6, align 8, !tbaa !10
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %12, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %6, i64 16
  %9 = zext i32 %3 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 8 %8, ptr nonnull align 2 %2, i64 %9, i1 false)
  %10 = load ptr, ptr %0, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 144
  store i32 %3, ptr %11, align 8, !tbaa !16
  br label %16

12:                                               ; preds = %5
  %13 = getelementptr inbounds i8, ptr %6, i64 144
  store i32 128, ptr %13, align 8, !tbaa !16
  %14 = getelementptr inbounds i8, ptr %6, i64 16
  %15 = tail call i32 @getpeername(i32 noundef %1, ptr noundef nonnull %14, ptr noundef nonnull %13) #3
  tail call void @translateSocketError(ptr noundef nonnull %0, i32 noundef %15)
  %.pre = load ptr, ptr %0, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %12, %7
  %17 = phi ptr [ %.pre, %12 ], [ %10, %7 ]
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %19 = load i16, ptr %18, align 8, !tbaa !17
  switch i16 %19, label %22 [
    i16 10, label %.sink.split
    i16 2, label %20
  ]

20:                                               ; preds = %16
  br label %.sink.split

.sink.split:                                      ; preds = %16, %20
  %.sink = phi i32 [ 0, %20 ], [ 1, %16 ]
  %21 = getelementptr inbounds i8, ptr %17, i64 4
  store i32 %.sink, ptr %21, align 4, !tbaa !18
  br label %22

22:                                               ; preds = %.sink.split, %16
  ret void
}

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

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

; Function Attrs: nounwind uwtable
declare hidden void @translateSocketError(ptr noundef, i32 noundef) local_unnamed_addr #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: readwrite) }
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 = { 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 = !{!"XsSocket", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"XsSocketPrivate", !12, i64 0, !12, i64 4, !12, i64 8, !13, i64 16, !12, i64 144, !12, i64 148, !12, i64 152, !12, i64 156, !8, i64 160}
!12 = !{!"int", !8, i64 0}
!13 = !{!"sockaddr_storage", !14, i64 0, !8, i64 2, !15, i64 120}
!14 = !{!"short", !8, i64 0}
!15 = !{!"long", !8, i64 0}
!16 = !{!11, !12, i64 144}
!17 = !{!11, !14, i64 16}
!18 = !{!11, !12, i64 4}


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