; LLVM IR from monolith-network/nettle :: lib/Socket.ll (ZN6nettle6Socket12socketReadInEPvi)

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

%"class.nettle::Socket" = type { i8, i32, %struct.sockaddr_in, %struct.timeval, %struct.timeval, %"class.std::function" }
%struct.sockaddr_in = type { i16, i16, %struct.in_addr, [8 x i8] }
%struct.in_addr = type { i32 }
%struct.timeval = type { i64, i64 }
%"class.std::function" = type { %"class.std::_Function_base", ptr }
%"class.std::_Function_base" = type { %"union.std::_Any_data", ptr }
%"union.std::_Any_data" = type { %"union.std::_Nocopy_types" }
%"union.std::_Nocopy_types" = type { { i64, i64 } }

; 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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6nettle6Socket12socketReadInEPvi(ptr noundef nonnull align 8 dereferenceable(88) %0, ptr noundef %1, i32 noundef %2) #1 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  %13 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  store i64 %15, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %16, ptr %11, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %35, %3
  %18 = load i64, ptr %10, align 8, !tbaa !11
  %19 = icmp ugt i64 %18, 0
  br i1 %19, label %20, label %47

20:                                               ; preds = %17
  %21 = getelementptr inbounds %"class.nettle::Socket", ptr %13, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !13
  %23 = load ptr, ptr %11, align 8, !tbaa !5
  %24 = load i64, ptr %10, align 8, !tbaa !11
  %25 = call i64 @recv(i32 noundef %22, ptr noundef %23, i64 noundef %24, i32 noundef 0)
  %26 = trunc i64 %25 to i32
  store i32 %26, ptr %8, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = icmp eq i32 %27, -1
  br i1 %28, label %29, label %31

29:                                               ; preds = %20
  %30 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %30, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %49

31:                                               ; preds = %20
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = icmp eq i32 0, %32
  br i1 %33, label %34, label %35

34:                                               ; preds = %31
  br label %47

35:                                               ; preds = %31
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = load ptr, ptr %11, align 8, !tbaa !5
  %38 = sext i32 %36 to i64
  %39 = getelementptr inbounds i8, ptr %37, i64 %38
  store ptr %39, ptr %11, align 8, !tbaa !5
  %40 = load i32, ptr %8, align 4, !tbaa !9
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = add nsw i32 %41, %40
  store i32 %42, ptr %9, align 4, !tbaa !9
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = load i64, ptr %10, align 8, !tbaa !11
  %46 = sub i64 %45, %44
  store i64 %46, ptr %10, align 8, !tbaa !11
  br label %17, !llvm.loop !22

47:                                               ; preds = %34, %17
  %48 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %48, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %49

49:                                               ; preds = %47, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %50 = load i32, ptr %4, align 4
  ret i32 %50
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 4}
!14 = !{!"_ZTSN6nettle6SocketE", !15, i64 0, !10, i64 4, !16, i64 8, !19, i64 24, !19, i64 40, !20, i64 56}
!15 = !{!"bool", !7, i64 0}
!16 = !{!"_ZTS11sockaddr_in", !17, i64 0, !17, i64 2, !18, i64 4, !7, i64 8}
!17 = !{!"short", !7, i64 0}
!18 = !{!"_ZTS7in_addr", !10, i64 0}
!19 = !{!"_ZTS7timeval", !12, i64 0, !12, i64 8}
!20 = !{!"_ZTSSt8functionIFvN6nettle11SocketErrorEEE", !21, i64 0, !6, i64 24}
!21 = !{!"_ZTSSt14_Function_base", !7, i64 0, !6, i64 16}
!22 = distinct !{!22, !23}
!23 = !{!"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"

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6nettle6Socket12socketReadInEPvi(ptr nocapture noundef nonnull readonly align 8 dereferenceable(88) %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 align 2 {
  %.not21 = icmp eq i32 %2, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %4 = sext i32 %2 to i64
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  br label %6

6:                                                ; preds = %.lr.ph, %10
  %.024 = phi ptr [ %1, %.lr.ph ], [ %12, %10 ]
  %.01623 = phi i64 [ %4, %.lr.ph ], [ %14, %10 ]
  %.01722 = phi i32 [ 0, %.lr.ph ], [ %13, %10 ]
  %7 = load i32, ptr %5, align 4, !tbaa !5
  %8 = tail call i64 @recv(i32 noundef %7, ptr noundef %.024, i64 noundef %.01623, i32 noundef 0)
  %9 = trunc i64 %8 to i32
  switch i32 %9, label %10 [
    i32 -1, label %._crit_edge.loopexit29
    i32 0, label %._crit_edge
  ]

10:                                               ; preds = %6
  %sext = shl i64 %8, 32
  %11 = ashr exact i64 %sext, 32
  %12 = getelementptr inbounds i8, ptr %.024, i64 %11
  %13 = add nsw i32 %.01722, %9
  %14 = sub i64 %.01623, %11
  %.not = icmp eq i64 %14, 0
  br i1 %.not, label %._crit_edge, label %6, !llvm.loop !19

._crit_edge.loopexit29:                           ; preds = %6
  br label %._crit_edge

._crit_edge:                                      ; preds = %10, %6, %._crit_edge.loopexit29, %3
  %.018 = phi i32 [ 0, %3 ], [ %.01722, %6 ], [ %13, %10 ], [ %9, %._crit_edge.loopexit29 ]
  ret i32 %.018
}

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

attributes #0 = { mustprogress 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 = { "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" }

!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 4}
!6 = !{!"_ZTSN6nettle6SocketE", !7, i64 0, !10, i64 4, !11, i64 8, !14, i64 24, !14, i64 40, !16, i64 56}
!7 = !{!"bool", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_ZTS11sockaddr_in", !12, i64 0, !12, i64 2, !13, i64 4, !8, i64 8}
!12 = !{!"short", !8, i64 0}
!13 = !{!"_ZTS7in_addr", !10, i64 0}
!14 = !{!"_ZTS7timeval", !15, i64 0, !15, i64 8}
!15 = !{!"long", !8, i64 0}
!16 = !{!"_ZTSSt8functionIFvN6nettle11SocketErrorEEE", !17, i64 0, !18, i64 24}
!17 = !{!"_ZTSSt14_Function_base", !8, i64 0, !18, i64 16}
!18 = !{!"any pointer", !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass