; LLVM IR from uNetworking/uSockets :: examples (hammer_test)

; 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.http_socket = type { double, i32, double, i32, [128 x i8] }

@long_length = external dso_local global i32, align 4
@websocket_context = external dso_local global ptr, align 8
@http_context = external dso_local global ptr, align 8
@long_buffer = external dso_local global ptr, align 8

; 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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @perform_random_operation(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = call i32 @rand() #4
  %7 = srem i32 %6, 5
  switch i32 %7, label %54 [
    i32 0, label %8
    i32 1, label %11
    i32 2, label %39
    i32 3, label %46
    i32 4, label %49
  ]

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call ptr @us_socket_close(i32 noundef 1, ptr noundef %9, i32 noundef 0, ptr noundef null)
  store ptr %10, ptr %2, align 8
  br label %56

11:                                               ; preds = %1
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = call i32 @us_socket_is_closed(i32 noundef 1, ptr noundef %12)
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %36, label %15

15:                                               ; preds = %11
  %16 = call i32 @rand() #4
  %17 = srem i32 %16, 2
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %27

19:                                               ; preds = %15
  %20 = load ptr, ptr @websocket_context, align 8, !tbaa !5
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = call ptr @us_socket_context_adopt_socket(i32 noundef 1, ptr noundef %20, ptr noundef %21, i32 noundef 1056)
  store ptr %22, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = call ptr @us_socket_ext(i32 noundef 1, ptr noundef %23)
  store ptr %24, ptr %4, align 8, !tbaa !5
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.http_socket, ptr %25, i32 0, i32 1
  store i32 0, ptr %26, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %35

27:                                               ; preds = %15
  %28 = load ptr, ptr @http_context, align 8, !tbaa !5
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = call ptr @us_socket_context_adopt_socket(i32 noundef 1, ptr noundef %28, ptr noundef %29, i32 noundef 160)
  store ptr %30, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = call ptr @us_socket_ext(i32 noundef 1, ptr noundef %31)
  store ptr %32, ptr %5, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.http_socket, ptr %33, i32 0, i32 1
  store i32 1, ptr %34, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %35

35:                                               ; preds = %27, %19
  br label %36

36:                                               ; preds = %35, %11
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = call ptr @perform_random_operation(ptr noundef %37)
  store ptr %38, ptr %2, align 8
  br label %56

39:                                               ; preds = %1
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load ptr, ptr @long_buffer, align 8, !tbaa !5
  %42 = call i32 @rand() #4
  %43 = load i32, ptr @long_length, align 4, !tbaa !13
  %44 = urem i32 %42, %43
  %45 = call i32 @us_socket_write(i32 noundef 1, ptr noundef %40, ptr noundef %41, i32 noundef %44, i32 noundef 0)
  br label %54

46:                                               ; preds = %1
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  call void @us_socket_shutdown(i32 noundef 1, ptr noundef %47)
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  call void @us_socket_timeout(i32 noundef 1, ptr noundef %48, i32 noundef 16)
  br label %54

49:                                               ; preds = %1
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  call void @us_socket_timeout(i32 noundef 1, ptr noundef %50, i32 noundef 4)
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = call ptr @us_socket_context(i32 noundef 1, ptr noundef %51)
  %53 = call ptr @us_socket_context_loop(i32 noundef 1, ptr noundef %52)
  call void @us_wakeup_loop(ptr noundef %53)
  br label %54

54:                                               ; preds = %49, %46, %39, %1
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %55, ptr %2, align 8
  br label %56

56:                                               ; preds = %54, %36, %8
  %57 = load ptr, ptr %2, align 8
  ret ptr %57
}

; Function Attrs: nounwind
declare i32 @rand() #3

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

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

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

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

declare void @us_socket_shutdown(i32 noundef, ptr noundef) #1

declare void @us_socket_timeout(i32 noundef, ptr noundef, i32 noundef) #1

declare void @us_wakeup_loop(ptr noundef) #1

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

declare ptr @us_socket_context(i32 noundef, 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 = { 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 "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 8}
!10 = !{!"http_socket", !11, i64 0, !12, i64 8, !11, i64 16, !12, i64 24, !7, i64 28}
!11 = !{!"double", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!12, !12, 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"

@long_length = external dso_local local_unnamed_addr global i32, align 4
@websocket_context = external dso_local local_unnamed_addr global ptr, align 8
@http_context = external dso_local local_unnamed_addr global ptr, align 8
@long_buffer = external dso_local local_unnamed_addr global ptr, align 8

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

; Function Attrs: nounwind uwtable
define dso_local ptr @perform_random_operation(ptr noundef %0) local_unnamed_addr #1 {
  br label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %.sink.split, %1
  %.tr.ph = phi ptr [ %11, %.sink.split ], [ %0, %1 ]
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse.outer, %6
  %2 = tail call i32 @rand() #3
  %3 = srem i32 %2, 5
  switch i32 %3, label %.loopexit [
    i32 0, label %4
    i32 1, label %6
    i32 2, label %14
    i32 3, label %20
    i32 4, label %21
  ]

4:                                                ; preds = %tailrecurse
  %5 = tail call ptr @us_socket_close(i32 noundef 1, ptr noundef %.tr.ph, i32 noundef 0, ptr noundef null) #3
  br label %.loopexit

6:                                                ; preds = %tailrecurse
  %7 = tail call i32 @us_socket_is_closed(i32 noundef 1, ptr noundef %.tr.ph) #3
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %.sink.split, label %tailrecurse

.sink.split:                                      ; preds = %6
  %8 = tail call i32 @rand() #3
  %9 = and i32 %8, 1
  %.not17 = icmp eq i32 %9, 0
  %. = select i1 %.not17, i32 160, i32 1056
  %.35 = xor i32 %9, 1
  %http_context.val = load ptr, ptr @http_context, align 8
  %websocket_context.val = load ptr, ptr @websocket_context, align 8
  %10 = select i1 %.not17, ptr %http_context.val, ptr %websocket_context.val
  %11 = tail call ptr @us_socket_context_adopt_socket(i32 noundef 1, ptr noundef %10, ptr noundef %.tr.ph, i32 noundef %.) #3
  %12 = tail call ptr @us_socket_ext(i32 noundef 1, ptr noundef %11) #3
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store i32 %.35, ptr %13, align 8, !tbaa !5
  br label %tailrecurse.outer

14:                                               ; preds = %tailrecurse
  %15 = load ptr, ptr @long_buffer, align 8, !tbaa !11
  %16 = tail call i32 @rand() #3
  %17 = load i32, ptr @long_length, align 4, !tbaa !13
  %18 = urem i32 %16, %17
  %19 = tail call i32 @us_socket_write(i32 noundef 1, ptr noundef %.tr.ph, ptr noundef %15, i32 noundef %18, i32 noundef 0) #3
  br label %.loopexit

20:                                               ; preds = %tailrecurse
  tail call void @us_socket_shutdown(i32 noundef 1, ptr noundef %.tr.ph) #3
  tail call void @us_socket_timeout(i32 noundef 1, ptr noundef %.tr.ph, i32 noundef 16) #3
  br label %.loopexit

21:                                               ; preds = %tailrecurse
  tail call void @us_socket_timeout(i32 noundef 1, ptr noundef %.tr.ph, i32 noundef 4) #3
  %22 = tail call ptr @us_socket_context(i32 noundef 1, ptr noundef %.tr.ph) #3
  %23 = tail call ptr @us_socket_context_loop(i32 noundef 1, ptr noundef %22) #3
  tail call void @us_wakeup_loop(ptr noundef %23) #3
  br label %.loopexit

.loopexit:                                        ; preds = %tailrecurse, %14, %20, %21, %4
  %.0 = phi ptr [ %5, %4 ], [ %.tr.ph, %21 ], [ %.tr.ph, %20 ], [ %.tr.ph, %14 ], [ %.tr.ph, %tailrecurse ]
  ret ptr %.0
}

; Function Attrs: nounwind
declare i32 @rand() local_unnamed_addr #2

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

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

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

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

declare void @us_socket_shutdown(i32 noundef, ptr noundef) local_unnamed_addr #0

declare void @us_socket_timeout(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @us_wakeup_loop(ptr noundef) local_unnamed_addr #0

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

declare ptr @us_socket_context(i32 noundef, 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 = { 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 "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, !10, i64 8}
!6 = !{!"http_socket", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !8, i64 28}
!7 = !{!"double", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !8, i64 0}
!13 = !{!10, !10, i64 0}


; Effective O3 passes: GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass