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

; 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_connect(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 %40

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
  %20 = icmp ne i32 %19, 1
  br i1 %20, label %21, label %22

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

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

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

39:                                               ; preds = %22
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %40

40:                                               ; preds = %39, %26, %21, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %41 = load i32, ptr %4, align 4
  ret i32 %41
}

declare i32 @tcp_connect(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_connect(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 %21, label %6

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

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 %21

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_connect(i32 noundef %19, ptr noundef nonnull %4) #3
  br label %21

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

declare i32 @tcp_connect(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