; LLVM IR from pandax381/microps :: udp (udp_recvfrom)

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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.udp_pcb = type { i32, %struct.ip_endpoint, %struct.queue_head, %struct.sched_ctx }
%struct.ip_endpoint = type { i32, i16 }
%struct.queue_head = type { ptr, ptr, i32 }
%struct.sched_ctx = type { %union.pthread_cond_t, i32, i32 }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }
%struct.udp_queue_entry = type { %struct.ip_endpoint, i16 }

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [57 x i8], align 1
@mutex = external hidden global %union.pthread_mutex_t, align 8
@.str.7 = external hidden unnamed_addr constant [21 x i8], align 1
@__func__.udp_recvfrom = external hidden unnamed_addr constant [13 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [7 x i8], align 1

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

declare i32 @lprintf(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mutex_lock(ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mutex_unlock(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden ptr @udp_pcb_get(i32 noundef) #4

; Function Attrs: nounwind uwtable
declare hidden void @udp_pcb_release(ptr noundef) #4

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

19:                                               ; preds = %4
  %20 = load ptr, ptr @stderr, align 8, !tbaa !9
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %20, i32 noundef 69, ptr noundef @.str, i32 noundef 396, ptr noundef @__func__.udp_recvfrom, ptr noundef @.str.7, i32 noundef %21)
  %23 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %82

24:                                               ; preds = %4
  br label %25

25:                                               ; preds = %51, %24
  %26 = load ptr, ptr %10, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.udp_pcb, ptr %26, i32 0, i32 2
  %28 = call ptr @queue_pop(ptr noundef %27)
  store ptr %28, ptr %11, align 8, !tbaa !9
  %29 = icmp ne ptr %28, null
  %30 = xor i1 %29, true
  br i1 %30, label %31, label %52

31:                                               ; preds = %25
  %32 = load ptr, ptr %10, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.udp_pcb, ptr %32, i32 0, i32 3
  %34 = call i32 @sched_sleep(ptr noundef %33, ptr noundef @mutex, ptr noundef null)
  %35 = icmp eq i32 %34, -1
  br i1 %35, label %36, label %41

36:                                               ; preds = %31
  %37 = load ptr, ptr @stderr, align 8, !tbaa !9
  %38 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %37, i32 noundef 68, ptr noundef @.str, i32 noundef 402, ptr noundef @__func__.udp_recvfrom, ptr noundef @.str.14)
  %39 = call i32 @mutex_unlock(ptr noundef @mutex)
  %40 = call ptr @__errno_location() #7
  store i32 4, ptr %40, align 4, !tbaa !5
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %82

41:                                               ; preds = %31
  %42 = load ptr, ptr %10, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.udp_pcb, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !13
  %45 = icmp eq i32 %44, 2
  br i1 %45, label %46, label %51

46:                                               ; preds = %41
  %47 = load ptr, ptr @stderr, align 8, !tbaa !9
  %48 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %47, i32 noundef 68, ptr noundef @.str, i32 noundef 408, ptr noundef @__func__.udp_recvfrom, ptr noundef @.str.15)
  %49 = load ptr, ptr %10, align 8, !tbaa !9
  call void @udp_pcb_release(ptr noundef %49)
  %50 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %82

51:                                               ; preds = %41
  br label %25, !llvm.loop !19

52:                                               ; preds = %25
  %53 = call i32 @mutex_unlock(ptr noundef @mutex)
  %54 = load ptr, ptr %9, align 8, !tbaa !9
  %55 = icmp ne ptr %54, null
  br i1 %55, label %56, label %60

56:                                               ; preds = %52
  %57 = load ptr, ptr %9, align 8, !tbaa !9
  %58 = load ptr, ptr %11, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.udp_queue_entry, ptr %58, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %57, ptr align 4 %59, i64 8, i1 false), !tbaa.struct !21
  br label %60

60:                                               ; preds = %56, %52
  %61 = load i64, ptr %8, align 8, !tbaa !11
  %62 = load ptr, ptr %11, align 8, !tbaa !9
  %63 = getelementptr inbounds %struct.udp_queue_entry, ptr %62, i32 0, i32 1
  %64 = load i16, ptr %63, align 4, !tbaa !23
  %65 = zext i16 %64 to i64
  %66 = icmp ult i64 %61, %65
  br i1 %66, label %67, label %69

67:                                               ; preds = %60
  %68 = load i64, ptr %8, align 8, !tbaa !11
  br label %74

69:                                               ; preds = %60
  %70 = load ptr, ptr %11, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.udp_queue_entry, ptr %70, i32 0, i32 1
  %72 = load i16, ptr %71, align 4, !tbaa !23
  %73 = zext i16 %72 to i64
  br label %74

74:                                               ; preds = %69, %67
  %75 = phi i64 [ %68, %67 ], [ %73, %69 ]
  store i64 %75, ptr %12, align 8, !tbaa !11
  %76 = load ptr, ptr %7, align 8, !tbaa !9
  %77 = load ptr, ptr %11, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.udp_queue_entry, ptr %77, i64 1
  %79 = load i64, ptr %12, align 8, !tbaa !11
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %76, ptr align 4 %78, i64 %79, i1 false)
  %80 = load ptr, ptr %11, align 8, !tbaa !9
  call void @memory_free(ptr noundef %80)
  %81 = load i64, ptr %12, align 8, !tbaa !11
  store i64 %81, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %82

82:                                               ; preds = %74, %46, %36, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  %83 = load i64, ptr %5, align 8
  ret i64 %83
}

declare ptr @queue_pop(ptr noundef) #1

declare i32 @sched_sleep(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #5

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @memory_free(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint 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 #4 = { 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 #5 = { nounwind willreturn memory(none) "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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"udp_pcb", !6, i64 0, !15, i64 4, !17, i64 16, !18, i64 40}
!15 = !{!"ip_endpoint", !6, i64 0, !16, i64 4}
!16 = !{!"short", !7, i64 0}
!17 = !{!"queue_head", !10, i64 0, !10, i64 8, !6, i64 16}
!18 = !{!"sched_ctx", !7, i64 0, !6, i64 48, !6, i64 52}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{i64 0, i64 4, !5, i64 4, i64 2, !22}
!22 = !{!16, !16, i64 0}
!23 = !{!24, !16, i64 8}
!24 = !{!"udp_queue_entry", !15, i64 0, !16, i64 8}


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

%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [57 x i8], align 1
@mutex = external hidden global %union.pthread_mutex_t, align 8
@.str.7 = external hidden unnamed_addr constant [21 x i8], align 1
@__func__.udp_recvfrom = external hidden unnamed_addr constant [13 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [7 x i8], align 1

declare i32 @lprintf(ptr noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mutex_lock(ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mutex_unlock(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden ptr @udp_pcb_get(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden void @udp_pcb_release(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local range(i64 -1, 65536) i64 @udp_recvfrom(i32 noundef %0, ptr nocapture noundef writeonly %1, i64 noundef %2, ptr noundef writeonly %3) local_unnamed_addr #3 {
  %5 = tail call i32 @mutex_lock(ptr noundef nonnull @mutex)
  %6 = tail call ptr @udp_pcb_get(i32 noundef %0)
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %9, label %.preheader

.preheader:                                       ; preds = %4
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %8 = getelementptr inbounds i8, ptr %6, i64 40
  br label %13

9:                                                ; preds = %4
  %10 = load ptr, ptr @stderr, align 8, !tbaa !5
  %11 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %10, i32 noundef 69, ptr noundef nonnull @.str, i32 noundef 396, ptr noundef nonnull @__func__.udp_recvfrom, ptr noundef nonnull @.str.7, i32 noundef %0) #6
  %12 = tail call i32 @mutex_unlock(ptr noundef nonnull @mutex)
  br label %39

13:                                               ; preds = %.preheader, %23
  %14 = tail call ptr @queue_pop(ptr noundef nonnull %7) #6
  %.not22 = icmp eq ptr %14, null
  br i1 %.not22, label %15, label %30

15:                                               ; preds = %13
  %16 = tail call i32 @sched_sleep(ptr noundef nonnull %8, ptr noundef nonnull @mutex, ptr noundef null) #6
  %17 = icmp eq i32 %16, -1
  br i1 %17, label %18, label %23

18:                                               ; preds = %15
  %19 = load ptr, ptr @stderr, align 8, !tbaa !5
  %20 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %19, i32 noundef 68, ptr noundef nonnull @.str, i32 noundef 402, ptr noundef nonnull @__func__.udp_recvfrom, ptr noundef nonnull @.str.14) #6
  %21 = tail call i32 @mutex_unlock(ptr noundef nonnull @mutex)
  %22 = tail call ptr @__errno_location() #7
  store i32 4, ptr %22, align 4, !tbaa !9
  br label %39

23:                                               ; preds = %15
  %24 = load i32, ptr %6, align 8, !tbaa !11
  %25 = icmp eq i32 %24, 2
  br i1 %25, label %26, label %13, !llvm.loop !17

26:                                               ; preds = %23
  %27 = load ptr, ptr @stderr, align 8, !tbaa !5
  %28 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %27, i32 noundef 68, ptr noundef nonnull @.str, i32 noundef 408, ptr noundef nonnull @__func__.udp_recvfrom, ptr noundef nonnull @.str.15) #6
  tail call void @udp_pcb_release(ptr noundef nonnull %6)
  %29 = tail call i32 @mutex_unlock(ptr noundef nonnull @mutex)
  br label %39

30:                                               ; preds = %13
  %31 = tail call i32 @mutex_unlock(ptr noundef nonnull @mutex)
  %.not23 = icmp eq ptr %3, null
  br i1 %.not23, label %34, label %32

32:                                               ; preds = %30
  %33 = load i64, ptr %14, align 4
  store i64 %33, ptr %3, align 4
  br label %34

34:                                               ; preds = %32, %30
  %35 = getelementptr inbounds i8, ptr %14, i64 8
  %36 = load i16, ptr %35, align 4, !tbaa !19
  %37 = zext i16 %36 to i64
  %. = tail call i64 @llvm.umin.i64(i64 %37, i64 %2)
  %38 = getelementptr inbounds i8, ptr %14, i64 12
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %1, ptr nonnull align 4 %38, i64 %., i1 false)
  tail call void @memory_free(ptr noundef nonnull %14)
  br label %39

39:                                               ; preds = %34, %26, %18, %9
  %.0 = phi i64 [ -1, %18 ], [ -1, %26 ], [ %., %34 ], [ -1, %9 ]
  ret i64 %.0
}

declare ptr @queue_pop(ptr noundef) local_unnamed_addr #0

declare i32 @sched_sleep(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @memory_free(ptr noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #5

attributes #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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 #4 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(none) }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"udp_pcb", !10, i64 0, !13, i64 4, !15, i64 16, !16, i64 40}
!13 = !{!"ip_endpoint", !10, i64 0, !14, i64 4}
!14 = !{!"short", !7, i64 0}
!15 = !{!"queue_head", !6, i64 0, !6, i64 8, !10, i64 16}
!16 = !{!"sched_ctx", !7, i64 0, !10, i64 48, !10, i64 52}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!20, !14, i64 8}
!20 = !{!"udp_queue_entry", !13, i64 0, !14, i64 8}


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