; LLVM IR from kala13x/libxutils :: src/net (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.XSocketInfo = type { [128 x i8], [256 x i8], i32, i32, i16 }
%struct.sockaddr_in = type { i16, i16, %struct.in_addr, [8 x i8] }
%struct.in_addr = type { i32 }
%struct.hostent = type { ptr, ptr, i32, i32, ptr }

; 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 dso_local i64 @XSock_IPStr(i32 noundef, ptr noundef, i64 noundef) #1

declare i64 @xstrncpy(ptr noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @XSock_InitInfo(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_GetAddr(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  call void @XSock_InitInfo(ptr noundef %8)
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.XSocketInfo, ptr %9, i32 0, i32 2
  store i32 4, ptr %10, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.sockaddr_in, ptr %11, i32 0, i32 2
  %13 = getelementptr inbounds %struct.in_addr, ptr %12, i32 0, i32 0
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = trunc i64 %14 to i32
  %16 = call ptr @gethostbyaddr(ptr noundef %13, i32 noundef %15, i32 noundef 2)
  store ptr %16, ptr %7, align 8, !tbaa !5
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %27

19:                                               ; preds = %3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.XSocketInfo, ptr %20, i32 0, i32 1
  %22 = getelementptr inbounds [256 x i8], ptr %21, i64 0, i64 0
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.hostent, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  %26 = call i64 @xstrncpy(ptr noundef %22, i64 noundef 256, ptr noundef %25)
  br label %27

27:                                               ; preds = %19, %3
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.sockaddr_in, ptr %28, i32 0, i32 2
  %30 = getelementptr inbounds %struct.in_addr, ptr %29, i32 0, i32 0
  %31 = load i32, ptr %30, align 4, !tbaa !17
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.XSocketInfo, ptr %32, i32 0, i32 0
  %34 = getelementptr inbounds [128 x i8], ptr %33, i64 0, i64 0
  %35 = call i64 @XSock_IPStr(i32 noundef %31, ptr noundef %34, i64 noundef 128)
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = icmp ne ptr %36, null
  %38 = zext i1 %37 to i64
  %39 = select i1 %37, i32 1, i32 0
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i32 %39
}

declare ptr @gethostbyaddr(ptr noundef, i32 noundef, i32 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 384}
!12 = !{!"XSocketInfo", !7, i64 0, !7, i64 128, !13, i64 384, !13, i64 388, !14, i64 392}
!13 = !{!"int", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"hostent", !6, i64 0, !6, i64 8, !13, i64 16, !13, i64 20, !6, i64 24}
!17 = !{!18, !13, i64 4}
!18 = !{!"sockaddr_in", !14, i64 0, !14, i64 2, !19, i64 4, !7, i64 8}
!19 = !{!"in_addr", !13, 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: nounwind uwtable
declare dso_local i64 @XSock_IPStr(i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

declare i64 @xstrncpy(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @XSock_InitInfo(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @XSock_GetAddr(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  tail call void @XSock_InitInfo(ptr noundef %0)
  %4 = getelementptr inbounds i8, ptr %0, i64 384
  store i32 4, ptr %4, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 4
  %6 = trunc i64 %2 to i32
  %7 = tail call ptr @gethostbyaddr(ptr noundef nonnull %5, i32 noundef %6, i32 noundef 2) #2
  %.not = icmp ne ptr %7, null
  br i1 %.not, label %8, label %12

8:                                                ; preds = %3
  %9 = getelementptr inbounds i8, ptr %0, i64 128
  %10 = load ptr, ptr %7, align 8, !tbaa !11
  %11 = tail call i64 @xstrncpy(ptr noundef nonnull %9, i64 noundef 256, ptr noundef %10) #2
  br label %12

12:                                               ; preds = %8, %3
  %13 = load i32, ptr %5, align 4, !tbaa !14
  %14 = tail call i64 @XSock_IPStr(i32 noundef %13, ptr noundef nonnull %0, i64 noundef 128)
  %15 = zext i1 %.not to i32
  ret i32 %15
}

declare ptr @gethostbyaddr(ptr noundef, i32 noundef, i32 noundef) 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, !9, i64 384}
!6 = !{!"XSocketInfo", !7, i64 0, !7, i64 128, !9, i64 384, !9, i64 388, !10, i64 392}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"hostent", !13, i64 0, !13, i64 8, !9, i64 16, !9, i64 20, !13, i64 24}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!15, !9, i64 4}
!15 = !{!"sockaddr_in", !10, i64 0, !10, i64 2, !16, i64 4, !7, i64 8}
!16 = !{!"in_addr", !9, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass