; LLVM IR from aws/s2n-tls :: docs/examples (s2n_recv)

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

@s2n_errno = external thread_local global i32, align 4
@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [15 x i8], align 1
@stdout = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @s2n_example_recv(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %57, %55, %3
  %13 = load i32, ptr %9, align 4, !tbaa !11
  %14 = sext i32 %13 to i64
  %15 = load i64, ptr %7, align 8, !tbaa !9
  %16 = icmp ult i64 %14, %15
  br i1 %16, label %17, label %58

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load i32, ptr %9, align 4, !tbaa !11
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = load i32, ptr %9, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = sub i64 %23, %25
  %27 = call i64 @s2n_recv(ptr noundef %18, ptr noundef %22, i64 noundef %26, ptr noundef %8)
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %10, align 4, !tbaa !11
  %29 = load i32, ptr %10, align 4, !tbaa !11
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %31, label %35

31:                                               ; preds = %17
  %32 = load i32, ptr %10, align 4, !tbaa !11
  %33 = load i32, ptr %9, align 4, !tbaa !11
  %34 = add nsw i32 %33, %32
  store i32 %34, ptr %9, align 4, !tbaa !11
  br label %54

35:                                               ; preds = %17
  %36 = load i32, ptr %10, align 4, !tbaa !11
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %35
  store i32 3, ptr %11, align 4
  br label %55

39:                                               ; preds = %35
  %40 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  %41 = load i32, ptr %40, align 4, !tbaa !11
  %42 = call i32 @s2n_error_get_type(i32 noundef %41)
  %43 = icmp eq i32 %42, 3
  br i1 %43, label %44, label %45

44:                                               ; preds = %39
  store i32 2, ptr %11, align 4
  br label %55, !llvm.loop !13

45:                                               ; preds = %39
  %46 = load ptr, ptr @stderr, align 8, !tbaa !5
  %47 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  %48 = load i32, ptr %47, align 4, !tbaa !11
  %49 = call ptr @s2n_strerror(i32 noundef %48, ptr noundef null)
  %50 = call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  %51 = load i32, ptr %50, align 4, !tbaa !11
  %52 = call ptr @s2n_strerror_debug(i32 noundef %51, ptr noundef null)
  %53 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %46, ptr noundef @.str, ptr noundef %49, ptr noundef %52) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %55

54:                                               ; preds = %31
  store i32 0, ptr %11, align 4
  br label %55

55:                                               ; preds = %54, %45, %44, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %56 = load i32, ptr %11, align 4
  switch i32 %56, label %63 [
    i32 0, label %57
    i32 3, label %58
    i32 2, label %12
  ]

57:                                               ; preds = %55
  br label %12, !llvm.loop !13

58:                                               ; preds = %55, %12
  %59 = load ptr, ptr @stdout, align 8, !tbaa !5
  %60 = load i32, ptr %9, align 4, !tbaa !11
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %59, ptr noundef @.str.1, i32 noundef %60, ptr noundef %61) #5
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %63

63:                                               ; preds = %58, %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %64 = load i32, ptr %4, align 4
  ret i32 %64
}

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

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

declare i32 @s2n_error_get_type(i32 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #3

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #4

declare ptr @s2n_strerror(i32 noundef, ptr noundef) #2

declare ptr @s2n_strerror_debug(i32 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 #5 = { 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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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

@s2n_errno = external thread_local local_unnamed_addr global i32, align 4
@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [15 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @s2n_example_recv(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #5
  store i32 0, ptr %4, align 4, !tbaa !5
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %5 = tail call align 4 ptr @llvm.threadlocal.address.p0(ptr align 4 @s2n_errno)
  br label %6

6:                                                ; preds = %.lr.ph, %27
  %7 = phi i64 [ 0, %.lr.ph ], [ %28, %27 ]
  %.01528 = phi i32 [ 0, %.lr.ph ], [ %.116, %27 ]
  %8 = getelementptr inbounds i8, ptr %1, i64 %7
  %9 = sub i64 %2, %7
  %10 = call i64 @s2n_recv(ptr noundef %0, ptr noundef %8, i64 noundef %9, ptr noundef nonnull %4) #5
  %11 = trunc i64 %10 to i32
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %6
  %14 = add nuw nsw i32 %.01528, %11
  br label %27

15:                                               ; preds = %6
  %16 = icmp eq i32 %11, 0
  br i1 %16, label %.thread, label %17

17:                                               ; preds = %15
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = call i32 @s2n_error_get_type(i32 noundef %18) #5
  %20 = icmp eq i32 %19, 3
  br i1 %20, label %27, label %.thread24, !llvm.loop !9

.thread24:                                        ; preds = %17
  %21 = load ptr, ptr @stderr, align 8, !tbaa !11
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = call ptr @s2n_strerror(i32 noundef %22, ptr noundef null) #5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = call ptr @s2n_strerror_debug(i32 noundef %24, ptr noundef null) #5
  %26 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %21, ptr noundef nonnull @.str, ptr noundef %23, ptr noundef %25) #6
  br label %32

27:                                               ; preds = %17, %13
  %.116 = phi i32 [ %14, %13 ], [ %.01528, %17 ]
  %28 = zext nneg i32 %.116 to i64
  %29 = icmp ult i64 %28, %2
  br i1 %29, label %6, label %.thread

.thread:                                          ; preds = %27, %15, %3
  %.015.lcssa = phi i32 [ 0, %3 ], [ %.01528, %15 ], [ %.116, %27 ]
  %30 = load ptr, ptr @stdout, align 8, !tbaa !11
  %31 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %30, ptr noundef nonnull @.str.1, i32 noundef %.015.lcssa, ptr noundef %1) #5
  br label %32

32:                                               ; preds = %.thread24, %.thread
  %.2 = phi i32 [ 0, %.thread ], [ -1, %.thread24 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #5
  ret i32 %.2
}

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

declare i64 @s2n_recv(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

declare i32 @s2n_error_get_type(i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #3

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #4

declare ptr @s2n_strerror(i32 noundef, ptr noundef) local_unnamed_addr #2

declare ptr @s2n_strerror_debug(i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nofree 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 #5 = { nounwind }
attributes #6 = { cold 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}


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