; LLVM IR from pandax381/microps :: tcp (tcp_close)

; 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.tcp_pcb = type { i32, i32, %struct.ip_endpoint, %struct.ip_endpoint, %struct.anon, i32, %struct.anon.0, i32, i16, i16, [65535 x i8], %struct.sched_ctx, %struct.queue_head, %struct.timeval, ptr, %struct.queue_head }
%struct.ip_endpoint = type { i32, i16 }
%struct.anon = type { i32, i32, i16, i16, i32, i32 }
%struct.anon.0 = type { i32, i16, i16 }
%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.timeval = type { i64, i64 }
%struct.queue_head = type { ptr, ptr, i32 }

@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [57 x i8], align 1
@mutex = external hidden global %union.pthread_mutex_t, align 8
@.str.12 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [19 x i8], align 1
@__func__.tcp_close = external hidden unnamed_addr constant [10 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 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) #2

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

; Function Attrs: nounwind uwtable
declare hidden i64 @tcp_output(ptr noundef, i8 noundef zeroext, ptr noundef, i64 noundef) #3

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

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

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

11:                                               ; preds = %1
  %12 = load ptr, ptr @stderr, align 8, !tbaa !9
  %13 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %12, i32 noundef 69, ptr noundef @.str.1, i32 noundef 1360, ptr noundef @__func__.tcp_close, ptr noundef @.str.12)
  %14 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %87

15:                                               ; preds = %1
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.tcp_pcb, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !11
  switch i32 %18, label %67 [
    i32 1, label %19
    i32 2, label %23
    i32 3, label %26
    i32 4, label %29
    i32 5, label %39
    i32 6, label %49
    i32 7, label %49
    i32 10, label %53
    i32 8, label %63
    i32 11, label %63
    i32 9, label %63
  ]

19:                                               ; preds = %15
  %20 = load ptr, ptr @stderr, align 8, !tbaa !9
  %21 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %20, i32 noundef 69, ptr noundef @.str.1, i32 noundef 1366, ptr noundef @__func__.tcp_close, ptr noundef @.str.23)
  %22 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %87

23:                                               ; preds = %15
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.tcp_pcb, ptr %24, i32 0, i32 0
  store i32 1, ptr %25, align 8, !tbaa !11
  br label %74

26:                                               ; preds = %15
  %27 = load ptr, ptr %4, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.tcp_pcb, ptr %27, i32 0, i32 0
  store i32 1, ptr %28, align 8, !tbaa !11
  br label %74

29:                                               ; preds = %15
  %30 = load ptr, ptr %4, align 8, !tbaa !9
  %31 = call i64 @tcp_output(ptr noundef %30, i8 noundef zeroext 17, ptr noundef null, i64 noundef 0)
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.tcp_pcb, ptr %32, i32 0, i32 4
  %34 = getelementptr inbounds %struct.anon, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !21
  %36 = add i32 %35, 1
  store i32 %36, ptr %34, align 8, !tbaa !21
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.tcp_pcb, ptr %37, i32 0, i32 0
  store i32 6, ptr %38, align 8, !tbaa !11
  br label %74

39:                                               ; preds = %15
  %40 = load ptr, ptr %4, align 8, !tbaa !9
  %41 = call i64 @tcp_output(ptr noundef %40, i8 noundef zeroext 17, ptr noundef null, i64 noundef 0)
  %42 = load ptr, ptr %4, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.tcp_pcb, ptr %42, i32 0, i32 4
  %44 = getelementptr inbounds %struct.anon, ptr %43, i32 0, i32 0
  %45 = load i32, ptr %44, align 8, !tbaa !21
  %46 = add i32 %45, 1
  store i32 %46, ptr %44, align 8, !tbaa !21
  %47 = load ptr, ptr %4, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.tcp_pcb, ptr %47, i32 0, i32 0
  store i32 6, ptr %48, align 8, !tbaa !11
  br label %74

49:                                               ; preds = %15, %15
  %50 = load ptr, ptr @stderr, align 8, !tbaa !9
  %51 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %50, i32 noundef 69, ptr noundef @.str.1, i32 noundef 1387, ptr noundef @__func__.tcp_close, ptr noundef @.str.27)
  %52 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %87

53:                                               ; preds = %15
  %54 = load ptr, ptr %4, align 8, !tbaa !9
  %55 = call i64 @tcp_output(ptr noundef %54, i8 noundef zeroext 17, ptr noundef null, i64 noundef 0)
  %56 = load ptr, ptr %4, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.tcp_pcb, ptr %56, i32 0, i32 4
  %58 = getelementptr inbounds %struct.anon, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 8, !tbaa !21
  %60 = add i32 %59, 1
  store i32 %60, ptr %58, align 8, !tbaa !21
  %61 = load ptr, ptr %4, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.tcp_pcb, ptr %61, i32 0, i32 0
  store i32 11, ptr %62, align 8, !tbaa !11
  br label %74

63:                                               ; preds = %15, %15, %15
  %64 = load ptr, ptr @stderr, align 8, !tbaa !9
  %65 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %64, i32 noundef 69, ptr noundef @.str.1, i32 noundef 1398, ptr noundef @__func__.tcp_close, ptr noundef @.str.27)
  %66 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %87

67:                                               ; preds = %15
  %68 = load ptr, ptr @stderr, align 8, !tbaa !9
  %69 = load ptr, ptr %4, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.tcp_pcb, ptr %69, i32 0, i32 0
  %71 = load i32, ptr %70, align 8, !tbaa !11
  %72 = call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %68, i32 noundef 69, ptr noundef @.str.1, i32 noundef 1402, ptr noundef @__func__.tcp_close, ptr noundef @.str.28, i32 noundef %71)
  %73 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %87

74:                                               ; preds = %53, %39, %29, %26, %23
  %75 = load ptr, ptr %4, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct.tcp_pcb, ptr %75, i32 0, i32 0
  %77 = load i32, ptr %76, align 8, !tbaa !11
  %78 = icmp eq i32 %77, 1
  br i1 %78, label %79, label %81

79:                                               ; preds = %74
  %80 = load ptr, ptr %4, align 8, !tbaa !9
  call void @tcp_pcb_release(ptr noundef %80)
  br label %85

81:                                               ; preds = %74
  %82 = load ptr, ptr %4, align 8, !tbaa !9
  %83 = getelementptr inbounds %struct.tcp_pcb, ptr %82, i32 0, i32 11
  %84 = call i32 @sched_wakeup(ptr noundef %83)
  br label %85

85:                                               ; preds = %81, %79
  %86 = call i32 @mutex_unlock(ptr noundef @mutex)
  store i32 0, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %87

87:                                               ; preds = %85, %67, %63, %49, %19, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %88 = load i32, ptr %2, align 4
  ret i32 %88
}

declare i32 @sched_wakeup(ptr noundef) #1

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 = { 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 = { 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 0}
!12 = !{!"tcp_pcb", !6, i64 0, !6, i64 4, !13, i64 8, !13, i64 16, !15, i64 24, !6, i64 44, !16, i64 48, !6, i64 56, !14, i64 60, !14, i64 62, !7, i64 64, !17, i64 65600, !18, i64 65656, !19, i64 65680, !10, i64 65696, !18, i64 65704}
!13 = !{!"ip_endpoint", !6, i64 0, !14, i64 4}
!14 = !{!"short", !7, i64 0}
!15 = !{!"", !6, i64 0, !6, i64 4, !14, i64 8, !14, i64 10, !6, i64 12, !6, i64 16}
!16 = !{!"", !6, i64 0, !14, i64 4, !14, i64 6}
!17 = !{!"sched_ctx", !7, i64 0, !6, i64 48, !6, i64 52}
!18 = !{!"queue_head", !10, i64 0, !10, i64 8, !6, i64 16}
!19 = !{!"timeval", !20, i64 0, !20, i64 8}
!20 = !{!"long", !7, i64 0}
!21 = !{!12, !6, i64 24}


; 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.1 = external hidden unnamed_addr constant [57 x i8], align 1
@mutex = external hidden global %union.pthread_mutex_t, align 8
@.str.12 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.23 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.27 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [19 x i8], align 1
@__func__.tcp_close = external hidden unnamed_addr constant [10 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: inlinehint nounwind uwtable
declare hidden i32 @mutex_lock(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden i64 @tcp_output(ptr noundef, i8 noundef zeroext, ptr noundef, i64 noundef) local_unnamed_addr #2

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @tcp_close(i32 noundef %0) local_unnamed_addr #2 {
  %2 = tail call i32 @mutex_lock(ptr noundef nonnull @mutex)
  %3 = tail call ptr @tcp_pcb_get(i32 noundef %0)
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %7

4:                                                ; preds = %1
  %5 = load ptr, ptr @stderr, align 8, !tbaa !5
  %6 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %5, i32 noundef 69, ptr noundef nonnull @.str.1, i32 noundef 1360, ptr noundef nonnull @__func__.tcp_close, ptr noundef nonnull @.str.12) #3
  br label %30

7:                                                ; preds = %1
  %8 = load i32, ptr %3, align 8, !tbaa !9
  switch i32 %8, label %19 [
    i32 1, label %9
    i32 2, label %22
    i32 3, label %22
    i32 4, label %23
    i32 5, label %23
    i32 6, label %12
    i32 7, label %12
    i32 10, label %15
    i32 8, label %16
    i32 11, label %16
    i32 9, label %16
  ]

9:                                                ; preds = %7
  %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.1, i32 noundef 1366, ptr noundef nonnull @__func__.tcp_close, ptr noundef nonnull @.str.23) #3
  br label %30

12:                                               ; preds = %7, %7
  %13 = load ptr, ptr @stderr, align 8, !tbaa !5
  %14 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %13, i32 noundef 69, ptr noundef nonnull @.str.1, i32 noundef 1387, ptr noundef nonnull @__func__.tcp_close, ptr noundef nonnull @.str.27) #3
  br label %30

15:                                               ; preds = %7
  br label %23

16:                                               ; preds = %7, %7, %7
  %17 = load ptr, ptr @stderr, align 8, !tbaa !5
  %18 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %17, i32 noundef 69, ptr noundef nonnull @.str.1, i32 noundef 1398, ptr noundef nonnull @__func__.tcp_close, ptr noundef nonnull @.str.27) #3
  br label %30

19:                                               ; preds = %7
  %20 = load ptr, ptr @stderr, align 8, !tbaa !5
  %21 = tail call i32 (ptr, i32, ptr, i32, ptr, ptr, ...) @lprintf(ptr noundef %20, i32 noundef 69, ptr noundef nonnull @.str.1, i32 noundef 1402, ptr noundef nonnull @__func__.tcp_close, ptr noundef nonnull @.str.28, i32 noundef %8) #3
  br label %30

22:                                               ; preds = %7, %7
  store i32 1, ptr %3, align 8, !tbaa !9
  tail call void @tcp_pcb_release(ptr noundef nonnull %3)
  br label %30

23:                                               ; preds = %7, %7, %15
  %.sink = phi i32 [ 11, %15 ], [ 6, %7 ], [ 6, %7 ]
  %24 = tail call i64 @tcp_output(ptr noundef nonnull %3, i8 noundef zeroext 17, ptr noundef null, i64 noundef 0)
  %25 = getelementptr inbounds i8, ptr %3, i64 24
  %26 = load i32, ptr %25, align 8, !tbaa !20
  %27 = add i32 %26, 1
  store i32 %27, ptr %25, align 8, !tbaa !20
  store i32 %.sink, ptr %3, align 8, !tbaa !9
  %28 = getelementptr inbounds i8, ptr %3, i64 65600
  %29 = tail call i32 @sched_wakeup(ptr noundef nonnull %28) #3
  br label %30

30:                                               ; preds = %22, %23, %19, %16, %12, %9, %4
  %.0 = phi i32 [ -1, %19 ], [ -1, %16 ], [ -1, %12 ], [ -1, %9 ], [ -1, %4 ], [ 0, %23 ], [ 0, %22 ]
  %31 = tail call i32 @mutex_unlock(ptr noundef nonnull @mutex)
  ret i32 %.0
}

declare i32 @sched_wakeup(ptr noundef) local_unnamed_addr #0

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 = { 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 #2 = { 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 }

!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, !11, i64 0}
!10 = !{!"tcp_pcb", !11, i64 0, !11, i64 4, !12, i64 8, !12, i64 16, !14, i64 24, !11, i64 44, !15, i64 48, !11, i64 56, !13, i64 60, !13, i64 62, !7, i64 64, !16, i64 65600, !17, i64 65656, !18, i64 65680, !6, i64 65696, !17, i64 65704}
!11 = !{!"int", !7, i64 0}
!12 = !{!"ip_endpoint", !11, i64 0, !13, i64 4}
!13 = !{!"short", !7, i64 0}
!14 = !{!"", !11, i64 0, !11, i64 4, !13, i64 8, !13, i64 10, !11, i64 12, !11, i64 16}
!15 = !{!"", !11, i64 0, !13, i64 4, !13, i64 6}
!16 = !{!"sched_ctx", !7, i64 0, !11, i64 48, !11, i64 52}
!17 = !{!"queue_head", !6, i64 0, !6, i64 8, !11, i64 16}
!18 = !{!"timeval", !19, i64 0, !19, i64 8}
!19 = !{!"long", !7, i64 0}
!20 = !{!10, !11, i64 24}


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