; 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.addrinfo = type { i32, i32, i32, i32, i32, ptr, ptr, ptr }
%struct.mbedtls_net_context = type { i32 }

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

17:                                               ; preds = %4
  %18 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %18, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %84

19:                                               ; preds = %4
  call void @llvm.memset.p0.i64(ptr align 8 %11, i8 0, i64 48, i1 false)
  %20 = getelementptr inbounds %struct.addrinfo, ptr %11, i32 0, i32 1
  store i32 0, ptr %20, align 4, !tbaa !11
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = icmp eq i32 %21, 1
  %23 = zext i1 %22 to i64
  %24 = select i1 %22, i32 2, i32 1
  %25 = getelementptr inbounds %struct.addrinfo, ptr %11, i32 0, i32 2
  store i32 %24, ptr %25, align 8, !tbaa !13
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = icmp eq i32 %26, 1
  %28 = zext i1 %27 to i64
  %29 = select i1 %27, i32 17, i32 6
  %30 = getelementptr inbounds %struct.addrinfo, ptr %11, i32 0, i32 3
  store i32 %29, ptr %30, align 4, !tbaa !14
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = call i32 @getaddrinfo(ptr noundef %31, ptr noundef %32, ptr noundef %11, ptr noundef %12)
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %19
  store i32 -82, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %84

36:                                               ; preds = %19
  store i32 -82, ptr %10, align 4, !tbaa !9
  %37 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %37, ptr %13, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %77, %36
  %39 = load ptr, ptr %13, align 8, !tbaa !5
  %40 = icmp ne ptr %39, null
  br i1 %40, label %41, label %81

41:                                               ; preds = %38
  %42 = load ptr, ptr %13, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.addrinfo, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !11
  %45 = load ptr, ptr %13, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.addrinfo, ptr %45, i32 0, i32 2
  %47 = load i32, ptr %46, align 8, !tbaa !13
  %48 = load ptr, ptr %13, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.addrinfo, ptr %48, i32 0, i32 3
  %50 = load i32, ptr %49, align 4, !tbaa !14
  %51 = call i32 @socket(i32 noundef %44, i32 noundef %47, i32 noundef %50) #5
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.mbedtls_net_context, ptr %52, i32 0, i32 0
  store i32 %51, ptr %53, align 4, !tbaa !15
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mbedtls_net_context, ptr %54, i32 0, i32 0
  %56 = load i32, ptr %55, align 4, !tbaa !15
  %57 = icmp slt i32 %56, 0
  br i1 %57, label %58, label %59

58:                                               ; preds = %41
  store i32 -66, ptr %10, align 4, !tbaa !9
  br label %77

59:                                               ; preds = %41
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mbedtls_net_context, ptr %60, i32 0, i32 0
  %62 = load i32, ptr %61, align 4, !tbaa !15
  %63 = load ptr, ptr %13, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.addrinfo, ptr %63, i32 0, i32 5
  %65 = load ptr, ptr %64, align 8, !tbaa !17
  %66 = load ptr, ptr %13, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.addrinfo, ptr %66, i32 0, i32 4
  %68 = load i32, ptr %67, align 8, !tbaa !18
  %69 = call i32 @connect(i32 noundef %62, ptr noundef %65, i32 noundef %68)
  %70 = icmp eq i32 %69, 0
  br i1 %70, label %71, label %72

71:                                               ; preds = %59
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %81

72:                                               ; preds = %59
  %73 = load ptr, ptr %6, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.mbedtls_net_context, ptr %73, i32 0, i32 0
  %75 = load i32, ptr %74, align 4, !tbaa !15
  %76 = call i32 @close(i32 noundef %75)
  store i32 -68, ptr %10, align 4, !tbaa !9
  br label %77

77:                                               ; preds = %72, %58
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.addrinfo, ptr %78, i32 0, i32 7
  %80 = load ptr, ptr %79, align 8, !tbaa !19
  store ptr %80, ptr %13, align 8, !tbaa !5
  br label %38, !llvm.loop !20

81:                                               ; preds = %71, %38
  %82 = load ptr, ptr %12, align 8, !tbaa !5
  call void @freeaddrinfo(ptr noundef %82) #5
  %83 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %83, ptr %5, align 4
  store i32 1, ptr %14, align 4
  br label %84

84:                                               ; preds = %81, %35, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 48, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %85 = load i32, ptr %5, align 4
  ret i32 %85
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @net_prepare() #0

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

declare i32 @getaddrinfo(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #3

; Function Attrs: nounwind
declare i32 @socket(i32 noundef, i32 noundef, i32 noundef) #4

declare i32 @connect(i32 noundef, ptr noundef, i32 noundef) #3

declare i32 @close(i32 noundef) #3

; Function Attrs: nounwind
declare void @freeaddrinfo(ptr noundef) #4

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 "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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"addrinfo", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 12}
!15 = !{!16, !10, i64 0}
!16 = !{!"", !10, i64 0}
!17 = !{!12, !6, i64 24}
!18 = !{!12, !10, i64 16}
!19 = !{!12, !6, i64 40}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.addrinfo = type { i32, i32, i32, i32, i32, ptr, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_net_connect(ptr nocapture noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca %struct.addrinfo, align 8
  %6 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %5) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #5
  %7 = tail call i32 @net_prepare()
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %36

8:                                                ; preds = %4
  %9 = icmp eq i32 %3, 1
  %10 = select i1 %9, i32 2, i32 1
  %11 = getelementptr inbounds i8, ptr %5, i64 8
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %5, i8 0, i64 48, i1 false)
  store i32 %10, ptr %11, align 8, !tbaa !5
  %12 = select i1 %9, i32 17, i32 6
  %13 = getelementptr inbounds i8, ptr %5, i64 12
  store i32 %12, ptr %13, align 4, !tbaa !11
  %14 = call i32 @getaddrinfo(ptr noundef %1, ptr noundef %2, ptr noundef nonnull %5, ptr noundef nonnull %6) #5
  %.not21 = icmp eq i32 %14, 0
  br i1 %.not21, label %.preheader, label %36

.preheader:                                       ; preds = %8
  %.023 = load ptr, ptr %6, align 8, !tbaa !12
  %.not2224 = icmp eq ptr %.023, null
  br i1 %.not2224, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %33
  %.025 = phi ptr [ %.0, %33 ], [ %.023, %.preheader ]
  %15 = getelementptr inbounds i8, ptr %.025, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = getelementptr inbounds i8, ptr %.025, i64 8
  %18 = load i32, ptr %17, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.025, i64 12
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = call i32 @socket(i32 noundef %16, i32 noundef %18, i32 noundef %20) #5
  store i32 %21, ptr %0, align 4, !tbaa !14
  %22 = icmp slt i32 %21, 0
  br i1 %22, label %33, label %23

23:                                               ; preds = %.lr.ph
  %24 = getelementptr inbounds i8, ptr %.025, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = getelementptr inbounds i8, ptr %.025, i64 16
  %27 = load i32, ptr %26, align 8, !tbaa !17
  %28 = call i32 @connect(i32 noundef %21, ptr noundef %25, i32 noundef %27) #5
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %._crit_edge.loopexit, label %30

30:                                               ; preds = %23
  %31 = load i32, ptr %0, align 4, !tbaa !14
  %32 = call i32 @close(i32 noundef %31) #5
  br label %33

33:                                               ; preds = %.lr.ph, %30
  %.1 = phi i32 [ -68, %30 ], [ -66, %.lr.ph ]
  %34 = getelementptr inbounds i8, ptr %.025, i64 40
  %.0 = load ptr, ptr %34, align 8, !tbaa !12
  %.not22 = icmp eq ptr %.0, null
  br i1 %.not22, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !18

._crit_edge.loopexit:                             ; preds = %23, %33
  %.2.ph = phi i32 [ %.1, %33 ], [ 0, %23 ]
  %.pre = load ptr, ptr %6, align 8, !tbaa !12
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %35 = phi ptr [ null, %.preheader ], [ %.pre, %._crit_edge.loopexit ]
  %.2 = phi i32 [ -82, %.preheader ], [ %.2.ph, %._crit_edge.loopexit ]
  call void @freeaddrinfo(ptr noundef %35) #5
  br label %36

36:                                               ; preds = %8, %4, %._crit_edge
  %.018 = phi i32 [ %.2, %._crit_edge ], [ %7, %4 ], [ -82, %8 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #5
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %5) #5
  ret i32 %.018
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @net_prepare() local_unnamed_addr #0

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

declare i32 @getaddrinfo(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @socket(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #4

declare i32 @connect(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

declare i32 @close(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare void @freeaddrinfo(ptr noundef) local_unnamed_addr #4

; 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { "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 "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, !7, i64 8}
!6 = !{!"addrinfo", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 12}
!12 = !{!10, !10, i64 0}
!13 = !{!6, !7, i64 4}
!14 = !{!15, !7, i64 0}
!15 = !{!"", !7, i64 0}
!16 = !{!6, !10, i64 24}
!17 = !{!6, !7, i64 16}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass