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

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

; 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_get(i32 noundef) #1

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

15:                                               ; preds = %3
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

16:                                               ; preds = %3
  %17 = load ptr, ptr %8, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.sock, ptr %17, i32 0, i32 2
  %19 = load i32, ptr %18, align 4, !tbaa !11
  switch i32 %19, label %56 [
    i32 1, label %20
    i32 2, label %38
  ]

20:                                               ; preds = %16
  %21 = load ptr, ptr %8, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.sock, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !13
  switch i32 %23, label %37 [
    i32 2, label %24
  ]

24:                                               ; preds = %20
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.sockaddr_in, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = getelementptr inbounds %struct.ip_endpoint, ptr %9, i32 0, i32 0
  store i32 %27, ptr %28, align 4, !tbaa !17
  %29 = load ptr, ptr %6, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.sockaddr_in, ptr %29, i32 0, i32 1
  %31 = load i16, ptr %30, align 2, !tbaa !19
  %32 = getelementptr inbounds %struct.ip_endpoint, ptr %9, i32 0, i32 1
  store i16 %31, ptr %32, align 4, !tbaa !20
  %33 = load ptr, ptr %8, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.sock, ptr %33, i32 0, i32 3
  %35 = load i32, ptr %34, align 4, !tbaa !21
  %36 = call i32 @tcp_bind(i32 noundef %35, ptr noundef %9)
  store i32 %36, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

37:                                               ; preds = %20
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

38:                                               ; preds = %16
  %39 = load ptr, ptr %8, align 8, !tbaa !9
  %40 = getelementptr inbounds %struct.sock, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %40, align 4, !tbaa !13
  switch i32 %41, label %55 [
    i32 2, label %42
  ]

42:                                               ; preds = %38
  %43 = load ptr, ptr %6, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.sockaddr_in, ptr %43, i32 0, i32 2
  %45 = load i32, ptr %44, align 4, !tbaa !14
  %46 = getelementptr inbounds %struct.ip_endpoint, ptr %9, i32 0, i32 0
  store i32 %45, ptr %46, align 4, !tbaa !17
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.sockaddr_in, ptr %47, i32 0, i32 1
  %49 = load i16, ptr %48, align 2, !tbaa !19
  %50 = getelementptr inbounds %struct.ip_endpoint, ptr %9, i32 0, i32 1
  store i16 %49, ptr %50, align 4, !tbaa !20
  %51 = load ptr, ptr %8, align 8, !tbaa !9
  %52 = getelementptr inbounds %struct.sock, ptr %51, i32 0, i32 3
  %53 = load i32, ptr %52, align 4, !tbaa !21
  %54 = call i32 @udp_bind(i32 noundef %53, ptr noundef %9)
  store i32 %54, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

55:                                               ; preds = %38
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

56:                                               ; preds = %16
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

57:                                               ; preds = %56, %55, %42, %37, %24, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %58 = load i32, ptr %4, align 4
  ret i32 %58
}

declare i32 @tcp_bind(i32 noundef, ptr noundef) #2

declare i32 @udp_bind(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 = !{!15, !6, i64 4}
!15 = !{!"sockaddr_in", !16, i64 0, !16, i64 2, !6, i64 4}
!16 = !{!"short", !7, i64 0}
!17 = !{!18, !6, i64 0}
!18 = !{!"ip_endpoint", !6, i64 0, !16, i64 4}
!19 = !{!15, !16, i64 2}
!20 = !{!18, !16, i64 4}
!21 = !{!12, !6, i64 12}


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

; 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_get(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @sock_bind(i32 noundef %0, ptr nocapture noundef readonly %1, i32 noundef %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
  switch i32 %8, label %33 [
    i32 1, label %9
    i32 2, label %21
  ]

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

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %1, i64 4
  %14 = load i32, ptr %13, align 4, !tbaa !11
  store i32 %14, ptr %4, align 4, !tbaa !14
  %15 = getelementptr inbounds i8, ptr %1, i64 2
  %16 = load i16, ptr %15, align 2, !tbaa !16
  %17 = getelementptr inbounds i8, ptr %4, i64 4
  store i16 %16, ptr %17, align 4, !tbaa !17
  %18 = getelementptr inbounds i8, ptr %5, i64 12
  %19 = load i32, ptr %18, align 4, !tbaa !18
  %20 = call i32 @tcp_bind(i32 noundef %19, ptr noundef nonnull %4) #3
  br label %33

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

24:                                               ; preds = %21
  %25 = getelementptr inbounds i8, ptr %1, i64 4
  %26 = load i32, ptr %25, align 4, !tbaa !11
  store i32 %26, ptr %4, align 4, !tbaa !14
  %27 = getelementptr inbounds i8, ptr %1, i64 2
  %28 = load i16, ptr %27, align 2, !tbaa !16
  %29 = getelementptr inbounds i8, ptr %4, i64 4
  store i16 %28, ptr %29, align 4, !tbaa !17
  %30 = getelementptr inbounds i8, ptr %5, i64 12
  %31 = load i32, ptr %30, align 4, !tbaa !18
  %32 = call i32 @udp_bind(i32 noundef %31, ptr noundef nonnull %4) #3
  br label %33

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

declare i32 @tcp_bind(i32 noundef, ptr noundef) local_unnamed_addr #2

declare i32 @udp_bind(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 = !{!12, !7, i64 4}
!12 = !{!"sockaddr_in", !13, i64 0, !13, i64 2, !7, i64 4}
!13 = !{!"short", !8, i64 0}
!14 = !{!15, !7, i64 0}
!15 = !{!"ip_endpoint", !7, i64 0, !13, i64 4}
!16 = !{!12, !13, i64 2}
!17 = !{!15, !13, i64 4}
!18 = !{!6, !7, i64 12}


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