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

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

@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
define dso_local i32 @sock_open(i32 noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = icmp ne i32 %10, 2
  br i1 %11, label %12, label %13

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

13:                                               ; preds = %3
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 1
  br i1 %15, label %16, label %20

16:                                               ; preds = %13
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp ne i32 %17, 2
  br i1 %18, label %19, label %20

19:                                               ; preds = %16
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %59

20:                                               ; preds = %16, %13
  %21 = load i32, ptr %7, align 4, !tbaa !5
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %20
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %59

24:                                               ; preds = %20
  %25 = call ptr @sock_alloc()
  store ptr %25, ptr %8, align 8, !tbaa !9
  %26 = load ptr, ptr %8, align 8, !tbaa !9
  %27 = icmp ne ptr %26, null
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %59

29:                                               ; preds = %24
  %30 = load i32, ptr %5, align 4, !tbaa !5
  %31 = load ptr, ptr %8, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.sock, ptr %31, i32 0, i32 1
  store i32 %30, ptr %32, align 4, !tbaa !11
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.sock, ptr %34, i32 0, i32 2
  store i32 %33, ptr %35, align 4, !tbaa !13
  %36 = load ptr, ptr %8, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.sock, ptr %36, i32 0, i32 2
  %38 = load i32, ptr %37, align 4, !tbaa !13
  switch i32 %38, label %47 [
    i32 1, label %39
    i32 2, label %43
  ]

39:                                               ; preds = %29
  %40 = call i32 @tcp_open()
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.sock, ptr %41, i32 0, i32 3
  store i32 %40, ptr %42, align 4, !tbaa !14
  br label %47

43:                                               ; preds = %29
  %44 = call i32 @udp_open()
  %45 = load ptr, ptr %8, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.sock, ptr %45, i32 0, i32 3
  store i32 %44, ptr %46, align 4, !tbaa !14
  br label %47

47:                                               ; preds = %43, %39, %29
  %48 = load ptr, ptr %8, align 8, !tbaa !9
  %49 = getelementptr inbounds %struct.sock, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = icmp eq i32 %50, -1
  br i1 %51, label %52, label %53

52:                                               ; preds = %47
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %59

53:                                               ; preds = %47
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  %55 = ptrtoint ptr %54 to i64
  %56 = sub i64 %55, ptrtoint (ptr @socks to i64)
  %57 = udiv i64 %56, 16
  %58 = trunc i64 %57 to i32
  store i32 %58, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %59

59:                                               ; preds = %53, %52, %28, %23, %19, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %60 = load i32, ptr %4, align 4
  ret i32 %60
}

; Function Attrs: nounwind uwtable
declare hidden ptr @sock_alloc() #1

declare i32 @tcp_open() #2

declare i32 @udp_open() #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 4}
!12 = !{!"sock", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!13 = !{!12, !6, i64 8}
!14 = !{!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.sock = type { i32, i32, i32, i32 }

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

; Function Attrs: nounwind uwtable
define dso_local i32 @sock_open(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq i32 %0, 2
  %4 = add i32 %1, -1
  %or.cond = icmp ult i32 %4, 2
  %or.cond19.not21 = and i1 %.not, %or.cond
  %.not17 = icmp eq i32 %2, 0
  %or.cond20 = and i1 %.not17, %or.cond19.not21
  br i1 %or.cond20, label %5, label %22

5:                                                ; preds = %3
  %6 = tail call ptr @sock_alloc()
  %.not18 = icmp eq ptr %6, null
  br i1 %.not18, label %22, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %6, i64 4
  store i32 2, ptr %8, align 4, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %6, i64 8
  store i32 %1, ptr %9, align 4, !tbaa !10
  %switch = icmp eq i32 %1, 1
  br i1 %switch, label %10, label %12

10:                                               ; preds = %7
  %11 = tail call i32 @tcp_open() #2
  br label %14

12:                                               ; preds = %7
  %13 = tail call i32 @udp_open() #2
  br label %14

14:                                               ; preds = %12, %10
  %.sink = phi i32 [ %11, %10 ], [ %13, %12 ]
  %15 = getelementptr inbounds i8, ptr %6, i64 12
  store i32 %.sink, ptr %15, align 4
  %16 = icmp eq i32 %.sink, -1
  br i1 %16, label %22, label %17

17:                                               ; preds = %14
  %18 = ptrtoint ptr %6 to i64
  %19 = sub i64 %18, ptrtoint (ptr @socks to i64)
  %20 = lshr i64 %19, 4
  %21 = trunc i64 %20 to i32
  br label %22

22:                                               ; preds = %14, %5, %3, %17
  %.0 = phi i32 [ %21, %17 ], [ -1, %3 ], [ -1, %5 ], [ -1, %14 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @sock_alloc() local_unnamed_addr #0

declare i32 @tcp_open() local_unnamed_addr #1

declare i32 @udp_open() local_unnamed_addr #1

attributes #0 = { 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 #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" }
attributes #2 = { 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 4}
!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 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass