; LLVM IR from pandax381/microps :: sock (sock_accept)

; 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.sock = type { i32, i32, i32, i32 }
%struct.ip_endpoint = type { i32, i16 }
%struct.sockaddr_in = type { i16, i16, i32 }

@socks = external hidden global [128 x %struct.sock], align 16

; 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: nounwind uwtable
declare hidden ptr @sock_alloc() #1

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

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

17:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

18:                                               ; preds = %3
  %19 = load ptr, ptr %8, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.sock, ptr %19, i32 0, i32 2
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp ne i32 %21, 1
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

24:                                               ; preds = %18
  %25 = load ptr, ptr %8, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.sock, ptr %25, i32 0, i32 1
  %27 = load i32, ptr %26, align 4, !tbaa !13
  switch i32 %27, label %70 [
    i32 2, label %28
  ]

28:                                               ; preds = %24
  %29 = load ptr, ptr %8, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.sock, ptr %29, i32 0, i32 3
  %31 = load i32, ptr %30, align 4, !tbaa !14
  %32 = call i32 @tcp_accept(i32 noundef %31, ptr noundef %10)
  store i32 %32, ptr %11, align 4, !tbaa !5
  %33 = load i32, ptr %11, align 4, !tbaa !5
  %34 = icmp eq i32 %33, -1
  br i1 %34, label %35, label %36

35:                                               ; preds = %28
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

36:                                               ; preds = %28
  %37 = load ptr, ptr %8, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.sock, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %38, align 4, !tbaa !13
  %40 = trunc i32 %39 to i16
  %41 = load ptr, ptr %6, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.sockaddr_in, ptr %41, i32 0, i32 0
  store i16 %40, ptr %42, align 4, !tbaa !15
  %43 = getelementptr inbounds %struct.ip_endpoint, ptr %10, i32 0, i32 0
  %44 = load i32, ptr %43, align 4, !tbaa !18
  %45 = load ptr, ptr %6, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.sockaddr_in, ptr %45, i32 0, i32 2
  store i32 %44, ptr %46, align 4, !tbaa !20
  %47 = getelementptr inbounds %struct.ip_endpoint, ptr %10, i32 0, i32 1
  %48 = load i16, ptr %47, align 4, !tbaa !21
  %49 = load ptr, ptr %6, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.sockaddr_in, ptr %49, i32 0, i32 1
  store i16 %48, ptr %50, align 2, !tbaa !22
  %51 = call ptr @sock_alloc()
  store ptr %51, ptr %9, align 8, !tbaa !9
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.sock, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !13
  %55 = load ptr, ptr %9, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.sock, ptr %55, i32 0, i32 1
  store i32 %54, ptr %56, align 4, !tbaa !13
  %57 = load ptr, ptr %8, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.sock, ptr %57, i32 0, i32 2
  %59 = load i32, ptr %58, align 4, !tbaa !11
  %60 = load ptr, ptr %9, align 8, !tbaa !9
  %61 = getelementptr inbounds %struct.sock, ptr %60, i32 0, i32 2
  store i32 %59, ptr %61, align 4, !tbaa !11
  %62 = load i32, ptr %11, align 4, !tbaa !5
  %63 = load ptr, ptr %9, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.sock, ptr %63, i32 0, i32 3
  store i32 %62, ptr %64, align 4, !tbaa !14
  %65 = load ptr, ptr %9, align 8, !tbaa !9
  %66 = ptrtoint ptr %65 to i64
  %67 = sub i64 %66, ptrtoint (ptr @socks to i64)
  %68 = udiv i64 %67, 16
  %69 = trunc i64 %68 to i32
  store i32 %69, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

70:                                               ; preds = %24
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %71

71:                                               ; preds = %70, %36, %35, %23, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %72 = load i32, ptr %4, align 4
  ret i32 %72
}

declare i32 @tcp_accept(i32 noundef, ptr 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"sock", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!13 = !{!12, !6, i64 4}
!14 = !{!12, !6, i64 12}
!15 = !{!16, !17, i64 0}
!16 = !{!"sockaddr_in", !17, i64 0, !17, i64 2, !6, i64 4}
!17 = !{!"short", !7, i64 0}
!18 = !{!19, !6, i64 0}
!19 = !{!"ip_endpoint", !6, i64 0, !17, i64 4}
!20 = !{!16, !6, i64 4}
!21 = !{!19, !17, i64 4}
!22 = !{!16, !17, i64 2}


; 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.sock = type { i32, i32, i32, i32 }
%struct.ip_endpoint = type { i32, i16 }

@socks = external hidden global [128 x %struct.sock], align 16

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

; 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 hidden ptr @sock_alloc() local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sock_accept(i32 noundef %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef readnone %2) local_unnamed_addr #1 {
  %4 = alloca %struct.ip_endpoint, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %5 = tail call ptr @sock_get(i32 noundef %0)
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %33, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %5, i64 8
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %.not19 = icmp eq i32 %8, 1
  br i1 %.not19, label %9, label %33

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %5, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !10
  %cond = icmp eq i32 %11, 2
  br i1 %cond, label %12, label %33

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %5, i64 12
  %14 = load i32, ptr %13, align 4, !tbaa !11
  %15 = call i32 @tcp_accept(i32 noundef %14, ptr noundef nonnull %4) #3
  %16 = icmp eq i32 %15, -1
  br i1 %16, label %33, label %17

17:                                               ; preds = %12
  %18 = load i32, ptr %10, align 4, !tbaa !10
  %19 = trunc i32 %18 to i16
  store i16 %19, ptr %1, align 4, !tbaa !12
  %20 = load i32, ptr %4, align 4, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %1, i64 4
  store i32 %20, ptr %21, align 4, !tbaa !17
  %22 = getelementptr inbounds i8, ptr %4, i64 4
  %23 = load i16, ptr %22, align 4, !tbaa !18
  %24 = getelementptr inbounds i8, ptr %1, i64 2
  store i16 %23, ptr %24, align 2, !tbaa !19
  %25 = call ptr @sock_alloc()
  %26 = getelementptr inbounds i8, ptr %25, i64 4
  %27 = load <2 x i32>, ptr %10, align 4, !tbaa !20
  store <2 x i32> %27, ptr %26, align 4, !tbaa !20
  %28 = getelementptr inbounds i8, ptr %25, i64 12
  store i32 %15, ptr %28, align 4, !tbaa !11
  %29 = ptrtoint ptr %25 to i64
  %30 = sub i64 %29, ptrtoint (ptr @socks to i64)
  %31 = lshr i64 %30, 4
  %32 = trunc i64 %31 to i32
  br label %33

33:                                               ; preds = %9, %12, %6, %3, %17
  %.0 = phi i32 [ %32, %17 ], [ -1, %3 ], [ -1, %6 ], [ -1, %12 ], [ -1, %9 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret i32 %.0
}

declare i32 @tcp_accept(i32 noundef, ptr 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, !7, i64 8}
!6 = !{!"sock", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 4}
!11 = !{!6, !7, i64 12}
!12 = !{!13, !14, i64 0}
!13 = !{!"sockaddr_in", !14, i64 0, !14, i64 2, !7, i64 4}
!14 = !{!"short", !8, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"ip_endpoint", !7, i64 0, !14, i64 4}
!17 = !{!13, !7, i64 4}
!18 = !{!16, !14, i64 4}
!19 = !{!13, !14, i64 2}
!20 = !{!7, !7, i64 0}


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