; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 30. CPP Programs (Reader_writer)

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

%union.sem_t = type { i64, [24 x i8] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@wrt = external dso_local global %union.sem_t, align 8
@mutex = external dso_local global %union.pthread_mutex_t, align 8
@__const.main.a = external hidden unnamed_addr constant [10 x i32], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [10 x i64], align 16
  %3 = alloca [5 x i64], align 16
  %4 = alloca [10 x i32], align 16
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 80, ptr %2) #5
  call void @llvm.lifetime.start.p0(i64 40, ptr %3) #5
  %9 = call i32 @pthread_mutex_init(ptr noundef @mutex, ptr noundef null) #5
  %10 = call i32 @sem_init(ptr noundef @wrt, i32 noundef 0, i32 noundef 1) #5
  call void @llvm.lifetime.start.p0(i64 40, ptr %4) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %4, ptr align 16 @__const.main.a, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %11

11:                                               ; preds = %23, %0
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 10
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %26

15:                                               ; preds = %11
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %17
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [10 x i32], ptr %4, i64 0, i64 %20
  %22 = call i32 @pthread_create(ptr noundef %18, ptr noundef null, ptr noundef @reader, ptr noundef %21) #5
  br label %23

23:                                               ; preds = %15
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %5, align 4, !tbaa !5
  br label %11, !llvm.loop !9

26:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %39, %26
  %28 = load i32, ptr %6, align 4, !tbaa !5
  %29 = icmp slt i32 %28, 5
  br i1 %29, label %31, label %30

30:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %42

31:                                               ; preds = %27
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [5 x i64], ptr %3, i64 0, i64 %33
  %35 = load i32, ptr %6, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [10 x i32], ptr %4, i64 0, i64 %36
  %38 = call i32 @pthread_create(ptr noundef %34, ptr noundef null, ptr noundef @writer, ptr noundef %37) #5
  br label %39

39:                                               ; preds = %31
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !5
  br label %27, !llvm.loop !11

42:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %53, %42
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = icmp slt i32 %44, 10
  br i1 %45, label %47, label %46

46:                                               ; preds = %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %56

47:                                               ; preds = %43
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [10 x i64], ptr %2, i64 0, i64 %49
  %51 = load i64, ptr %50, align 8, !tbaa !12
  %52 = call i32 @pthread_join(i64 noundef %51, ptr noundef null)
  br label %53

53:                                               ; preds = %47
  %54 = load i32, ptr %7, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %7, align 4, !tbaa !5
  br label %43, !llvm.loop !14

56:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %57

57:                                               ; preds = %67, %56
  %58 = load i32, ptr %8, align 4, !tbaa !5
  %59 = icmp slt i32 %58, 5
  br i1 %59, label %61, label %60

60:                                               ; preds = %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %70

61:                                               ; preds = %57
  %62 = load i32, ptr %8, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [5 x i64], ptr %3, i64 0, i64 %63
  %65 = load i64, ptr %64, align 8, !tbaa !12
  %66 = call i32 @pthread_join(i64 noundef %65, ptr noundef null)
  br label %67

67:                                               ; preds = %61
  %68 = load i32, ptr %8, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %8, align 4, !tbaa !5
  br label %57, !llvm.loop !15

70:                                               ; preds = %60
  %71 = call i32 @pthread_mutex_destroy(ptr noundef @mutex) #5
  %72 = call i32 @sem_destroy(ptr noundef @wrt) #5
  call void @llvm.lifetime.end.p0(i64 40, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 40, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 80, ptr %2) #5
  ret i32 0
}

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

; Function Attrs: nounwind
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind
declare i32 @sem_init(ptr noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr 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

declare i32 @pthread_join(i64 noundef, ptr noundef) #4

; Function Attrs: nounwind
declare i32 @pthread_mutex_destroy(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @sem_destroy(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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


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

%union.sem_t = type { i64, [24 x i8] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }

@wrt = external dso_local global %union.sem_t, align 8
@mutex = external dso_local global %union.pthread_mutex_t, align 8
@__const.main.a = external hidden unnamed_addr constant [10 x i32], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
.preheader18.preheader:
  %0 = alloca [10 x i64], align 16
  %1 = alloca [5 x i64], align 16
  %2 = alloca [10 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %0) #5
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #5
  %3 = tail call i32 @pthread_mutex_init(ptr noundef nonnull @mutex, ptr noundef null) #5
  %4 = tail call i32 @sem_init(ptr noundef nonnull @wrt, i32 noundef 0, i32 noundef 1) #5
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) %2, ptr noundef nonnull align 16 dereferenceable(40) @__const.main.a, i64 40, i1 false)
  %5 = call i32 @pthread_create(ptr noundef nonnull %0, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %2) #5
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = getelementptr inbounds i8, ptr %2, i64 4
  %8 = call i32 @pthread_create(ptr noundef nonnull %6, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %7) #5
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = getelementptr inbounds i8, ptr %2, i64 8
  %11 = call i32 @pthread_create(ptr noundef nonnull %9, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %10) #5
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  %13 = getelementptr inbounds i8, ptr %2, i64 12
  %14 = call i32 @pthread_create(ptr noundef nonnull %12, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %13) #5
  %15 = getelementptr inbounds i8, ptr %0, i64 32
  %16 = getelementptr inbounds i8, ptr %2, i64 16
  %17 = call i32 @pthread_create(ptr noundef nonnull %15, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %16) #5
  %18 = getelementptr inbounds i8, ptr %0, i64 40
  %19 = getelementptr inbounds i8, ptr %2, i64 20
  %20 = call i32 @pthread_create(ptr noundef nonnull %18, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %19) #5
  %21 = getelementptr inbounds i8, ptr %0, i64 48
  %22 = getelementptr inbounds i8, ptr %2, i64 24
  %23 = call i32 @pthread_create(ptr noundef nonnull %21, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %22) #5
  %24 = getelementptr inbounds i8, ptr %0, i64 56
  %25 = getelementptr inbounds i8, ptr %2, i64 28
  %26 = call i32 @pthread_create(ptr noundef nonnull %24, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %25) #5
  %27 = getelementptr inbounds i8, ptr %0, i64 64
  %28 = getelementptr inbounds i8, ptr %2, i64 32
  %29 = call i32 @pthread_create(ptr noundef nonnull %27, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %28) #5
  %30 = getelementptr inbounds i8, ptr %0, i64 72
  %31 = getelementptr inbounds i8, ptr %2, i64 36
  %32 = call i32 @pthread_create(ptr noundef nonnull %30, ptr noundef null, ptr noundef nonnull @reader, ptr noundef nonnull %31) #5
  %33 = call i32 @pthread_create(ptr noundef nonnull %1, ptr noundef null, ptr noundef nonnull @writer, ptr noundef nonnull %2) #5
  %34 = getelementptr inbounds i8, ptr %1, i64 8
  %35 = call i32 @pthread_create(ptr noundef nonnull %34, ptr noundef null, ptr noundef nonnull @writer, ptr noundef nonnull %7) #5
  %36 = getelementptr inbounds i8, ptr %1, i64 16
  %37 = call i32 @pthread_create(ptr noundef nonnull %36, ptr noundef null, ptr noundef nonnull @writer, ptr noundef nonnull %10) #5
  %38 = getelementptr inbounds i8, ptr %1, i64 24
  %39 = call i32 @pthread_create(ptr noundef nonnull %38, ptr noundef null, ptr noundef nonnull @writer, ptr noundef nonnull %13) #5
  %40 = getelementptr inbounds i8, ptr %1, i64 32
  %41 = call i32 @pthread_create(ptr noundef nonnull %40, ptr noundef null, ptr noundef nonnull @writer, ptr noundef nonnull %16) #5
  %42 = load i64, ptr %0, align 16, !tbaa !5
  %43 = call i32 @pthread_join(i64 noundef %42, ptr noundef null) #5
  %44 = load i64, ptr %6, align 8, !tbaa !5
  %45 = call i32 @pthread_join(i64 noundef %44, ptr noundef null) #5
  %46 = load i64, ptr %9, align 16, !tbaa !5
  %47 = call i32 @pthread_join(i64 noundef %46, ptr noundef null) #5
  %48 = load i64, ptr %12, align 8, !tbaa !5
  %49 = call i32 @pthread_join(i64 noundef %48, ptr noundef null) #5
  %50 = load i64, ptr %15, align 16, !tbaa !5
  %51 = call i32 @pthread_join(i64 noundef %50, ptr noundef null) #5
  %52 = load i64, ptr %18, align 8, !tbaa !5
  %53 = call i32 @pthread_join(i64 noundef %52, ptr noundef null) #5
  %54 = load i64, ptr %21, align 16, !tbaa !5
  %55 = call i32 @pthread_join(i64 noundef %54, ptr noundef null) #5
  %56 = load i64, ptr %24, align 8, !tbaa !5
  %57 = call i32 @pthread_join(i64 noundef %56, ptr noundef null) #5
  %58 = load i64, ptr %27, align 16, !tbaa !5
  %59 = call i32 @pthread_join(i64 noundef %58, ptr noundef null) #5
  %60 = load i64, ptr %30, align 8, !tbaa !5
  %61 = call i32 @pthread_join(i64 noundef %60, ptr noundef null) #5
  %62 = load i64, ptr %1, align 16, !tbaa !5
  %63 = call i32 @pthread_join(i64 noundef %62, ptr noundef null) #5
  %64 = load i64, ptr %34, align 8, !tbaa !5
  %65 = call i32 @pthread_join(i64 noundef %64, ptr noundef null) #5
  %66 = load i64, ptr %36, align 16, !tbaa !5
  %67 = call i32 @pthread_join(i64 noundef %66, ptr noundef null) #5
  %68 = load i64, ptr %38, align 8, !tbaa !5
  %69 = call i32 @pthread_join(i64 noundef %68, ptr noundef null) #5
  %70 = load i64, ptr %40, align 16, !tbaa !5
  %71 = call i32 @pthread_join(i64 noundef %70, ptr noundef null) #5
  %72 = call i32 @pthread_mutex_destroy(ptr noundef nonnull @mutex) #5
  %73 = call i32 @sem_destroy(ptr noundef nonnull @wrt) #5
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %1) #5
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %0) #5
  ret i32 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
declare i32 @pthread_mutex_init(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @sem_init(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr 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

declare i32 @pthread_join(i64 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @pthread_mutex_destroy(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @sem_destroy(ptr noundef) local_unnamed_addr #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { "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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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