; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (net_sockets)

; 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.timeval = type { i64, i64 }
%struct.fd_set = type { [16 x i64] }
%struct.mbedtls_net_context = type { i32 }

; 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 i32 @select(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_net_recv(ptr noundef, ptr noundef, i64 noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_net_recv_timeout(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.timeval, align 8
  %12 = alloca %struct.fd_set, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 128, ptr %12) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mbedtls_net_context, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 4, !tbaa !13
  store i32 %19, ptr %13, align 4, !tbaa !11
  %20 = load i32, ptr %13, align 4, !tbaa !11
  %21 = icmp slt i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %4
  store i32 -69, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %87

23:                                               ; preds = %4
  br label %24

24:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  store ptr %12, ptr %16, align 8, !tbaa !5
  store i32 0, ptr %15, align 4, !tbaa !11
  br label %25

25:                                               ; preds = %35, %24
  %26 = load i32, ptr %15, align 4, !tbaa !11
  %27 = zext i32 %26 to i64
  %28 = icmp ult i64 %27, 16
  br i1 %28, label %29, label %38

29:                                               ; preds = %25
  %30 = load ptr, ptr %16, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.fd_set, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %15, align 4, !tbaa !11
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [16 x i64], ptr %31, i64 0, i64 %33
  store i64 0, ptr %34, align 8, !tbaa !9
  br label %35

35:                                               ; preds = %29
  %36 = load i32, ptr %15, align 4, !tbaa !11
  %37 = add i32 %36, 1
  store i32 %37, ptr %15, align 4, !tbaa !11
  br label %25, !llvm.loop !15

38:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %39

39:                                               ; preds = %38
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %13, align 4, !tbaa !11
  %42 = srem i32 %41, 64
  %43 = zext i32 %42 to i64
  %44 = shl i64 1, %43
  %45 = getelementptr inbounds %struct.fd_set, ptr %12, i32 0, i32 0
  %46 = load i32, ptr %13, align 4, !tbaa !11
  %47 = sdiv i32 %46, 64
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [16 x i64], ptr %45, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = or i64 %50, %44
  store i64 %51, ptr %49, align 8, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !11
  %53 = udiv i32 %52, 1000
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds %struct.timeval, ptr %11, i32 0, i32 0
  store i64 %54, ptr %55, align 8, !tbaa !17
  %56 = load i32, ptr %9, align 4, !tbaa !11
  %57 = urem i32 %56, 1000
  %58 = mul i32 %57, 1000
  %59 = zext i32 %58 to i64
  %60 = getelementptr inbounds %struct.timeval, ptr %11, i32 0, i32 1
  store i64 %59, ptr %60, align 8, !tbaa !19
  %61 = load i32, ptr %13, align 4, !tbaa !11
  %62 = add nsw i32 %61, 1
  %63 = load i32, ptr %9, align 4, !tbaa !11
  %64 = icmp eq i32 %63, 0
  br i1 %64, label %65, label %66

65:                                               ; preds = %40
  br label %67

66:                                               ; preds = %40
  br label %67

67:                                               ; preds = %66, %65
  %68 = phi ptr [ null, %65 ], [ %11, %66 ]
  %69 = call i32 @select(i32 noundef %62, ptr noundef %12, ptr noundef null, ptr noundef null, ptr noundef %68)
  store i32 %69, ptr %10, align 4, !tbaa !11
  %70 = load i32, ptr %10, align 4, !tbaa !11
  %71 = icmp eq i32 %70, 0
  br i1 %71, label %72, label %73

72:                                               ; preds = %67
  store i32 -26624, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %87

73:                                               ; preds = %67
  %74 = load i32, ptr %10, align 4, !tbaa !11
  %75 = icmp slt i32 %74, 0
  br i1 %75, label %76, label %82

76:                                               ; preds = %73
  %77 = call ptr @__errno_location() #5
  %78 = load i32, ptr %77, align 4, !tbaa !11
  %79 = icmp eq i32 %78, 4
  br i1 %79, label %80, label %81

80:                                               ; preds = %76
  store i32 -26880, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %87

81:                                               ; preds = %76
  store i32 -76, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %87

82:                                               ; preds = %73
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = load i64, ptr %8, align 8, !tbaa !9
  %86 = call i32 @mbedtls_net_recv(ptr noundef %83, ptr noundef %84, i64 noundef %85)
  store i32 %86, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %87

87:                                               ; preds = %82, %81, %80, %72, %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 128, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %88 = load i32, ptr %5, align 4
  ret i32 %88
}

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 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 #4 = { nounwind }
attributes #5 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !10, i64 0}
!18 = !{!"timeval", !10, i64 0, !10, i64 8}
!19 = !{!18, !10, 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"

%struct.timeval = type { i64, i64 }
%struct.fd_set = type { [16 x i64] }

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

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

declare i32 @select(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_net_recv(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_net_recv_timeout(ptr noundef %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #2 {
  %5 = alloca %struct.timeval, align 8
  %6 = alloca %struct.fd_set, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %6) #5
  %7 = load i32, ptr %0, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %36, label %.preheader.preheader

.preheader.preheader:                             ; preds = %4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %6, i8 0, i64 128, i1 false), !tbaa !10
  %9 = and i32 %7, 63
  %10 = zext nneg i32 %9 to i64
  %11 = shl nuw i64 1, %10
  %12 = lshr i32 %7, 6
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds [16 x i64], ptr %6, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !10
  %16 = or i64 %15, %11
  store i64 %16, ptr %14, align 8, !tbaa !10
  %17 = udiv i32 %3, 1000
  %18 = zext nneg i32 %17 to i64
  store i64 %18, ptr %5, align 8, !tbaa !12
  %19 = urem i32 %3, 1000
  %20 = mul nuw nsw i32 %19, 1000
  %21 = zext nneg i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %5, i64 8
  store i64 %21, ptr %22, align 8, !tbaa !14
  %23 = add nuw nsw i32 %7, 1
  %24 = icmp eq i32 %3, 0
  %25 = select i1 %24, ptr null, ptr %5
  %26 = call i32 @select(i32 noundef %23, ptr noundef nonnull %6, ptr noundef null, ptr noundef null, ptr noundef %25) #5
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %36, label %28

28:                                               ; preds = %.preheader.preheader
  %29 = icmp slt i32 %26, 0
  br i1 %29, label %30, label %34

30:                                               ; preds = %28
  %31 = tail call ptr @__errno_location() #6
  %32 = load i32, ptr %31, align 4, !tbaa !15
  %33 = icmp eq i32 %32, 4
  %. = select i1 %33, i32 -26880, i32 -76
  br label %36

34:                                               ; preds = %28
  %35 = call i32 @mbedtls_net_recv(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2)
  br label %36

36:                                               ; preds = %30, %.preheader.preheader, %4, %34
  %.0 = phi i32 [ %35, %34 ], [ -69, %4 ], [ -26624, %.preheader.preheader ], [ %., %30 ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %5) #5
  ret i32 %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

attributes #0 = { mustprogress 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 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!13, !11, i64 0}
!13 = !{!"timeval", !11, i64 0, !11, i64 8}
!14 = !{!13, !11, i64 8}
!15 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass