; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex11 (ikkp-server)

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

@listener_d = external dso_local global i32, align 4
@intro_msg = external dso_local global [52 x i8], align 16
@.str.9 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [49 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @error(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @catch_signal(i32 noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @handle_shutdown(i32 noundef) #0

declare i32 @close(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @open_listener_socket() #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @open_client_socket() #0

; Function Attrs: nounwind uwtable
declare dso_local void @bind_to_port(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @say(i32 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @read_in(i32 noundef, ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca [255 x i8], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 255, ptr %6) #4
  %10 = call i32 @catch_signal(i32 noundef 2, ptr noundef @handle_shutdown)
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  call void @error(ptr noundef @.str.9)
  br label %13

13:                                               ; preds = %12, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 30000, ptr %7, align 4, !tbaa !5
  %14 = call i32 @open_listener_socket()
  store i32 %14, ptr @listener_d, align 4, !tbaa !5
  %15 = load i32, ptr @listener_d, align 4, !tbaa !5
  %16 = load i32, ptr %7, align 4, !tbaa !5
  call void @bind_to_port(i32 noundef %15, i32 noundef %16)
  %17 = load i32, ptr @listener_d, align 4, !tbaa !5
  %18 = call i32 @listen(i32 noundef %17, i32 noundef 10) #4
  %19 = icmp eq i32 %18, -1
  br i1 %19, label %20, label %21

20:                                               ; preds = %13
  call void @error(ptr noundef @.str.10)
  br label %21

21:                                               ; preds = %20, %13
  br label %22

22:                                               ; preds = %58, %56, %21
  br label %23

23:                                               ; preds = %22
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = call i32 (ptr, ...) @printf(ptr noundef @.str.11, i32 noundef %24)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %26 = call i32 @open_client_socket()
  store i32 %26, ptr %8, align 4, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !5
  %28 = call i32 @say(i32 noundef %27, ptr noundef @intro_msg)
  %29 = icmp eq i32 %28, -1
  br i1 %29, label %30, label %33

30:                                               ; preds = %23
  %31 = load i32, ptr %8, align 4, !tbaa !5
  %32 = call i32 @close(i32 noundef %31)
  store i32 2, ptr %9, align 4
  br label %56

33:                                               ; preds = %23
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = getelementptr inbounds [255 x i8], ptr %6, i64 0, i64 0
  %36 = call i32 @read_in(i32 noundef %34, ptr noundef %35, i32 noundef 255)
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = call i32 @say(i32 noundef %37, ptr noundef @.str.12)
  %39 = icmp eq i32 %38, -1
  br i1 %39, label %40, label %43

40:                                               ; preds = %33
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = call i32 @close(i32 noundef %41)
  store i32 2, ptr %9, align 4
  br label %56

43:                                               ; preds = %33
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = getelementptr inbounds [255 x i8], ptr %6, i64 0, i64 0
  %46 = call i32 @read_in(i32 noundef %44, ptr noundef %45, i32 noundef 255)
  %47 = load i32, ptr %8, align 4, !tbaa !5
  %48 = call i32 @say(i32 noundef %47, ptr noundef @.str.13)
  %49 = icmp eq i32 %48, -1
  br i1 %49, label %50, label %53

50:                                               ; preds = %43
  %51 = load i32, ptr %8, align 4, !tbaa !5
  %52 = call i32 @close(i32 noundef %51)
  store i32 2, ptr %9, align 4
  br label %56

53:                                               ; preds = %43
  %54 = load i32, ptr %8, align 4, !tbaa !5
  %55 = call i32 @close(i32 noundef %54)
  store i32 0, ptr %9, align 4
  br label %56

56:                                               ; preds = %53, %50, %40, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %57 = load i32, ptr %9, align 4
  switch i32 %57, label %59 [
    i32 0, label %58
    i32 2, label %22
  ]

58:                                               ; preds = %56
  br label %22

59:                                               ; preds = %56
  unreachable
}

; Function Attrs: nounwind
declare i32 @listen(i32 noundef, i32 noundef) #3

declare i32 @printf(ptr noundef, ...) #2

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 = { 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 = !{!"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}


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

@listener_d = external dso_local local_unnamed_addr global i32, align 4
@intro_msg = external dso_local global [52 x i8], align 16
@.str.9 = external hidden unnamed_addr constant [26 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.13 = external hidden unnamed_addr constant [49 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @error(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @catch_signal(i32 noundef, ptr noundef) local_unnamed_addr #0

; 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 dso_local void @handle_shutdown(i32 noundef) #0

declare i32 @close(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @open_listener_socket() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @open_client_socket() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @bind_to_port(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @say(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @read_in(i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: noreturn nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #3 {
  %3 = alloca [255 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 255, ptr nonnull %3) #6
  %4 = tail call i32 @catch_signal(i32 noundef 2, ptr noundef nonnull @handle_shutdown)
  %5 = icmp eq i32 %4, -1
  br i1 %5, label %6, label %7

6:                                                ; preds = %2
  tail call void @error(ptr noundef nonnull @.str.9)
  br label %7

7:                                                ; preds = %6, %2
  %8 = tail call i32 @open_listener_socket()
  store i32 %8, ptr @listener_d, align 4, !tbaa !5
  tail call void @bind_to_port(i32 noundef %8, i32 noundef 30000)
  %9 = load i32, ptr @listener_d, align 4, !tbaa !5
  %10 = tail call i32 @listen(i32 noundef %9, i32 noundef 10) #6
  %11 = icmp eq i32 %10, -1
  br i1 %11, label %12, label %.preheader

12:                                               ; preds = %7
  tail call void @error(ptr noundef nonnull @.str.10)
  br label %.preheader

.preheader:                                       ; preds = %12, %7
  br label %13

13:                                               ; preds = %.preheader, %25
  %14 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.11, i32 noundef 30000)
  %15 = call i32 @open_client_socket()
  %16 = call i32 @say(i32 noundef %15, ptr noundef nonnull @intro_msg)
  %17 = icmp eq i32 %16, -1
  br i1 %17, label %25, label %18

18:                                               ; preds = %13
  %19 = call i32 @read_in(i32 noundef %15, ptr noundef nonnull %3, i32 noundef 255)
  %20 = call i32 @say(i32 noundef %15, ptr noundef nonnull @.str.12)
  %21 = icmp eq i32 %20, -1
  br i1 %21, label %25, label %22

22:                                               ; preds = %18
  %23 = call i32 @read_in(i32 noundef %15, ptr noundef nonnull %3, i32 noundef 255)
  %24 = call i32 @say(i32 noundef %15, ptr noundef nonnull @.str.13)
  br label %25

25:                                               ; preds = %18, %13, %22
  %26 = call i32 @close(i32 noundef %15) #6
  br label %13
}

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

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

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 = { noreturn 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 "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 = { 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 #6 = { 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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass