; LLVM IR from tezc/sc :: socket (sc_sock)

; 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.sc_sock = type { %struct.sc_sock_fd, i8, i32, [128 x i8] }
%struct.sc_sock_fd = type { i32, i32, i32 }

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #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 hidden void @sc_sock_errstr(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @sc_sock_err() #2

; Function Attrs: nounwind uwtable
define dso_local i32 @sc_sock_send(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = icmp sle i32 %13, 0
  br i1 %14, label %15, label %17

15:                                               ; preds = %4
  %16 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %16, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %45

17:                                               ; preds = %4
  br label %18

18:                                               ; preds = %35, %17
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.sc_sock, ptr %19, i32 0, i32 0
  %21 = getelementptr inbounds %struct.sc_sock_fd, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 4, !tbaa !11
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = load i32, ptr %8, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = call i64 @send(i32 noundef %22, ptr noundef %23, i64 noundef %25, i32 noundef %26)
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %10, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = icmp eq i32 %29, -1
  br i1 %30, label %31, label %43

31:                                               ; preds = %18
  %32 = call i32 @sc_sock_err()
  store i32 %32, ptr %11, align 4, !tbaa !9
  %33 = load i32, ptr %11, align 4, !tbaa !9
  %34 = icmp eq i32 %33, 4
  br i1 %34, label %35, label %36

35:                                               ; preds = %31
  br label %18

36:                                               ; preds = %31
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = icmp eq i32 %37, 11
  br i1 %38, label %39, label %41

39:                                               ; preds = %36
  %40 = call ptr @__errno_location() #5
  store i32 11, ptr %40, align 4, !tbaa !9
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %45

41:                                               ; preds = %36
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  call void @sc_sock_errstr(ptr noundef %42, i32 noundef 0)
  store i32 -1, ptr %10, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %41, %18
  %44 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %44, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %45

45:                                               ; preds = %43, %39, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %46 = load i32, ptr %5, align 4
  ret i32 %46
}

declare i64 @send(i32 noundef, ptr noundef, i64 noundef, i32 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind willreturn memory(none) "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 = { "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 }
attributes #5 = { nounwind willreturn memory(none) }

!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, !10, i64 0}
!12 = !{!"sc_sock", !13, i64 0, !14, i64 12, !10, i64 16, !7, i64 20}
!13 = !{!"sc_sock_fd", !10, i64 0, !10, i64 4, !10, i64 8}
!14 = !{!"_Bool", !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"

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden i32 @sc_sock_err() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @sc_sock_send(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = icmp slt i32 %2, 1
  br i1 %5, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %4
  %6 = zext nneg i32 %2 to i64
  br label %7

7:                                                ; preds = %.preheader, %12
  %8 = load i32, ptr %0, align 4, !tbaa !5
  %9 = tail call i64 @send(i32 noundef %8, ptr noundef %1, i64 noundef %6, i32 noundef %3) #3
  %10 = trunc i64 %9 to i32
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %.loopexit

12:                                               ; preds = %7
  %13 = tail call i32 @sc_sock_err()
  switch i32 %13, label %16 [
    i32 4, label %7
    i32 11, label %14
  ]

14:                                               ; preds = %12
  %15 = tail call ptr @__errno_location() #4
  store i32 11, ptr %15, align 4, !tbaa !12
  br label %.loopexit

16:                                               ; preds = %12
  tail call void @sc_sock_errstr(ptr noundef nonnull %0, i32 noundef 0)
  br label %.loopexit

.loopexit:                                        ; preds = %7, %16, %4, %14
  %.0 = phi i32 [ -1, %14 ], [ %2, %4 ], [ -1, %16 ], [ %10, %7 ]
  ret i32 %.0
}

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

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { 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 }
attributes #4 = { nounwind willreturn memory(none) }

!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, !8, i64 0}
!6 = !{!"sc_sock", !7, i64 0, !11, i64 12, !8, i64 16, !9, i64 20}
!7 = !{!"sc_sock_fd", !8, i64 0, !8, i64 4, !8, i64 8}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"_Bool", !9, i64 0}
!12 = !{!8, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass