; LLVM IR from metaspartan/denarius :: src/stun.ll (Z19GetExternalIPbySTUNmP11sockaddr_inPPKc)

; 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.StunSrv = type { [36 x i8], i16 }

@_ZL10randfiller = external hidden global i64, align 8
@_ZL11StunSrvList = external hidden global [277 x %struct.StunSrv], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z19GetExternalIPbySTUNmP11sockaddr_inPPKc(i64 noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  %13 = load i64, ptr %5, align 8, !tbaa !5
  store i64 %13, ptr @_ZL10randfiller, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #2
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = trunc i64 %14 to i16
  store i16 %15, ptr %8, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  br label %16

16:                                               ; preds = %23, %3
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = lshr i64 %17, 8
  %19 = or i64 %18, -72057594037927936
  store i64 %19, ptr %5, align 8, !tbaa !5
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = urem i64 %20, 277
  %22 = trunc i64 %21 to i16
  store i16 %22, ptr %9, align 2, !tbaa !11
  br label %23

23:                                               ; preds = %16
  %24 = load i16, ptr %9, align 2, !tbaa !11
  %25 = zext i16 %24 to i32
  %26 = icmp eq i32 %25, 0
  br i1 %26, label %16, label %27, !llvm.loop !13

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 256, ptr %10, align 4, !tbaa !15
  br label %28

28:                                               ; preds = %62, %27
  %29 = load i32, ptr %10, align 4, !tbaa !15
  %30 = icmp slt i32 %29, 141824
  br i1 %30, label %31, label %65

31:                                               ; preds = %28
  %32 = load i16, ptr %8, align 2, !tbaa !11
  %33 = zext i16 %32 to i32
  %34 = load i16, ptr %9, align 2, !tbaa !11
  %35 = zext i16 %34 to i32
  %36 = add nsw i32 %33, %35
  %37 = srem i32 %36, 277
  %38 = trunc i32 %37 to i16
  store i16 %38, ptr %8, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %39 = load i16, ptr %8, align 2, !tbaa !11
  %40 = zext i16 %39 to i64
  %41 = getelementptr inbounds [277 x %struct.StunSrv], ptr @_ZL11StunSrvList, i64 0, i64 %40
  %42 = getelementptr inbounds %struct.StunSrv, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds [36 x i8], ptr %42, i64 0, i64 0
  %44 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %43, ptr %44, align 8, !tbaa !9
  %45 = load i16, ptr %8, align 2, !tbaa !11
  %46 = zext i16 %45 to i64
  %47 = getelementptr inbounds [277 x %struct.StunSrv], ptr @_ZL11StunSrvList, i64 0, i64 %46
  %48 = getelementptr inbounds %struct.StunSrv, ptr %47, i32 0, i32 1
  %49 = load i16, ptr %48, align 2, !tbaa !17
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  %51 = call noundef i32 @_ZL11StunRequestPKctP11sockaddr_in(ptr noundef %43, i16 noundef zeroext %49, ptr noundef %50)
  store i32 %51, ptr %11, align 4, !tbaa !15
  %52 = load i32, ptr %11, align 4, !tbaa !15
  %53 = icmp sgt i32 %52, 0
  br i1 %53, label %54, label %58

54:                                               ; preds = %31
  %55 = load i32, ptr %10, align 4, !tbaa !15
  %56 = load i32, ptr %11, align 4, !tbaa !15
  %57 = or i32 %55, %56
  store i32 %57, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %59

58:                                               ; preds = %31
  store i32 0, ptr %12, align 4
  br label %59

59:                                               ; preds = %58, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  %60 = load i32, ptr %12, align 4
  switch i32 %60, label %66 [
    i32 0, label %61
  ]

61:                                               ; preds = %59
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %10, align 4, !tbaa !15
  %64 = add nsw i32 %63, 256
  store i32 %64, ptr %10, align 4, !tbaa !15
  br label %28, !llvm.loop !19

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

66:                                               ; preds = %65, %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  %67 = load i32, ptr %4, align 4
  ret i32 %67
}

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

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL11StunRequestPKctP11sockaddr_in(ptr noundef, i16 noundef zeroext, ptr noundef) #0

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

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{!18, !12, i64 36}
!18 = !{!"_ZTS7StunSrv", !7, i64 0, !12, i64 36}
!19 = distinct !{!19, !14}


; 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.StunSrv = type { [36 x i8], i16 }

@_ZL10randfiller = external hidden local_unnamed_addr global i64, align 8
@_ZL11StunSrvList = external hidden global [277 x %struct.StunSrv], align 16

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z19GetExternalIPbySTUNmP11sockaddr_inPPKc(i64 noundef %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  store i64 %0, ptr @_ZL10randfiller, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %4, %3
  %.019 = phi i64 [ %0, %3 ], [ %6, %4 ]
  %5 = lshr i64 %.019, 8
  %6 = or disjoint i64 %5, -72057594037927936
  %7 = urem i64 %6, 277
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %4, label %.preheader, !llvm.loop !9

.preheader:                                       ; preds = %4
  %9 = trunc i64 %0 to i32
  %10 = trunc nuw nsw i64 %7 to i32
  br label %11

11:                                               ; preds = %.preheader, %21
  %.01827 = phi i32 [ 256, %.preheader ], [ %22, %21 ]
  %.02026 = phi i32 [ %9, %.preheader ], [ %14, %21 ]
  %12 = and i32 %.02026, 65535
  %13 = add nuw nsw i32 %12, %10
  %14 = urem i32 %13, 277
  %15 = zext nneg i32 %14 to i64
  %16 = getelementptr inbounds [277 x %struct.StunSrv], ptr @_ZL11StunSrvList, i64 0, i64 %15
  store ptr %16, ptr %2, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %16, i64 36
  %18 = load i16, ptr %17, align 2, !tbaa !13
  %19 = tail call noundef i32 @_ZL11StunRequestPKctP11sockaddr_in(ptr noundef nonnull %16, i16 noundef zeroext %18, ptr noundef %1)
  %20 = icmp slt i32 %19, 1
  br i1 %20, label %21, label %.split.loop.exit24

21:                                               ; preds = %11
  %22 = add nuw nsw i32 %.01827, 256
  %23 = icmp ult i32 %.01827, 141568
  br i1 %23, label %11, label %.split.loop.exit, !llvm.loop !16

.split.loop.exit24:                               ; preds = %11
  %24 = or i32 %19, %.01827
  br label %.split.loop.exit

.split.loop.exit:                                 ; preds = %21, %.split.loop.exit24
  %.2 = phi i32 [ %24, %.split.loop.exit24 ], [ -1, %21 ]
  ret i32 %.2
}

; Function Attrs: mustprogress uwtable
declare hidden noundef i32 @_ZL11StunRequestPKctP11sockaddr_in(ptr noundef, i16 noundef zeroext, ptr noundef) local_unnamed_addr #0

attributes #0 = { mustprogress 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" }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !15, i64 36}
!14 = !{!"_ZTS7StunSrv", !7, i64 0, !15, i64 36}
!15 = !{!"short", !7, i64 0}
!16 = distinct !{!16, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass