; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex12 (beer)

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

@beers = external dso_local global i32, align 4
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [46 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @drink_lots() #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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [20 x i64], align 16
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 160, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %5 = load i32, ptr @beers, align 4, !tbaa !5
  %6 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %5)
  %7 = load i32, ptr @beers, align 4, !tbaa !5
  %8 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %7)
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %17, %0
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 20
  br i1 %11, label %12, label %20

12:                                               ; preds = %9
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [20 x i64], ptr %2, i64 0, i64 %14
  %16 = call i32 @pthread_create(ptr noundef %15, ptr noundef null, ptr noundef @drink_lots, ptr noundef null) #4
  br label %17

17:                                               ; preds = %12
  %18 = load i32, ptr %3, align 4, !tbaa !5
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %3, align 4, !tbaa !5
  br label %9, !llvm.loop !9

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

21:                                               ; preds = %30, %20
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = icmp slt i32 %22, 20
  br i1 %23, label %24, label %33

24:                                               ; preds = %21
  %25 = load i32, ptr %3, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [20 x i64], ptr %2, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !11
  %29 = call i32 @pthread_join(i64 noundef %28, ptr noundef %4)
  br label %30

30:                                               ; preds = %24
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %3, align 4, !tbaa !5
  br label %21, !llvm.loop !13

33:                                               ; preds = %21
  %34 = load i32, ptr @beers, align 4, !tbaa !5
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %34)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 160, ptr %2) #4
  ret i32 0
}

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

declare i32 @pthread_join(i64 noundef, 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !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"

@beers = external dso_local local_unnamed_addr global i32, align 4
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [46 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @drink_lots() #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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca [20 x i64], align 16
  %2 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 160, ptr nonnull %1) #5
  %3 = load i32, ptr @beers, align 4, !tbaa !5
  %4 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %3)
  %5 = load i32, ptr @beers, align 4, !tbaa !5
  %6 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef %5)
  %7 = call i32 @pthread_create(ptr noundef nonnull %1, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = call i32 @pthread_create(ptr noundef nonnull %8, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %10 = getelementptr inbounds i8, ptr %1, i64 16
  %11 = call i32 @pthread_create(ptr noundef nonnull %10, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %12 = getelementptr inbounds i8, ptr %1, i64 24
  %13 = call i32 @pthread_create(ptr noundef nonnull %12, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %14 = getelementptr inbounds i8, ptr %1, i64 32
  %15 = call i32 @pthread_create(ptr noundef nonnull %14, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %16 = getelementptr inbounds i8, ptr %1, i64 40
  %17 = call i32 @pthread_create(ptr noundef nonnull %16, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %18 = getelementptr inbounds i8, ptr %1, i64 48
  %19 = call i32 @pthread_create(ptr noundef nonnull %18, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %20 = getelementptr inbounds i8, ptr %1, i64 56
  %21 = call i32 @pthread_create(ptr noundef nonnull %20, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %22 = getelementptr inbounds i8, ptr %1, i64 64
  %23 = call i32 @pthread_create(ptr noundef nonnull %22, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %24 = getelementptr inbounds i8, ptr %1, i64 72
  %25 = call i32 @pthread_create(ptr noundef nonnull %24, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %26 = getelementptr inbounds i8, ptr %1, i64 80
  %27 = call i32 @pthread_create(ptr noundef nonnull %26, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %28 = getelementptr inbounds i8, ptr %1, i64 88
  %29 = call i32 @pthread_create(ptr noundef nonnull %28, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %30 = getelementptr inbounds i8, ptr %1, i64 96
  %31 = call i32 @pthread_create(ptr noundef nonnull %30, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %32 = getelementptr inbounds i8, ptr %1, i64 104
  %33 = call i32 @pthread_create(ptr noundef nonnull %32, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %34 = getelementptr inbounds i8, ptr %1, i64 112
  %35 = call i32 @pthread_create(ptr noundef nonnull %34, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %36 = getelementptr inbounds i8, ptr %1, i64 120
  %37 = call i32 @pthread_create(ptr noundef nonnull %36, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %38 = getelementptr inbounds i8, ptr %1, i64 128
  %39 = call i32 @pthread_create(ptr noundef nonnull %38, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %40 = getelementptr inbounds i8, ptr %1, i64 136
  %41 = call i32 @pthread_create(ptr noundef nonnull %40, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %42 = getelementptr inbounds i8, ptr %1, i64 144
  %43 = call i32 @pthread_create(ptr noundef nonnull %42, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  %44 = getelementptr inbounds i8, ptr %1, i64 152
  %45 = call i32 @pthread_create(ptr noundef nonnull %44, ptr noundef null, ptr noundef nonnull @drink_lots, ptr noundef null) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #5
  %46 = load i64, ptr %1, align 16, !tbaa !9
  %47 = call i32 @pthread_join(i64 noundef %46, ptr noundef nonnull %2) #5
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = call i32 @pthread_join(i64 noundef %48, ptr noundef nonnull %2) #5
  %50 = load i64, ptr %10, align 16, !tbaa !9
  %51 = call i32 @pthread_join(i64 noundef %50, ptr noundef nonnull %2) #5
  %52 = load i64, ptr %12, align 8, !tbaa !9
  %53 = call i32 @pthread_join(i64 noundef %52, ptr noundef nonnull %2) #5
  %54 = load i64, ptr %14, align 16, !tbaa !9
  %55 = call i32 @pthread_join(i64 noundef %54, ptr noundef nonnull %2) #5
  %56 = load i64, ptr %16, align 8, !tbaa !9
  %57 = call i32 @pthread_join(i64 noundef %56, ptr noundef nonnull %2) #5
  %58 = load i64, ptr %18, align 16, !tbaa !9
  %59 = call i32 @pthread_join(i64 noundef %58, ptr noundef nonnull %2) #5
  %60 = load i64, ptr %20, align 8, !tbaa !9
  %61 = call i32 @pthread_join(i64 noundef %60, ptr noundef nonnull %2) #5
  %62 = load i64, ptr %22, align 16, !tbaa !9
  %63 = call i32 @pthread_join(i64 noundef %62, ptr noundef nonnull %2) #5
  %64 = load i64, ptr %24, align 8, !tbaa !9
  %65 = call i32 @pthread_join(i64 noundef %64, ptr noundef nonnull %2) #5
  %66 = load i64, ptr %26, align 16, !tbaa !9
  %67 = call i32 @pthread_join(i64 noundef %66, ptr noundef nonnull %2) #5
  %68 = load i64, ptr %28, align 8, !tbaa !9
  %69 = call i32 @pthread_join(i64 noundef %68, ptr noundef nonnull %2) #5
  %70 = load i64, ptr %30, align 16, !tbaa !9
  %71 = call i32 @pthread_join(i64 noundef %70, ptr noundef nonnull %2) #5
  %72 = load i64, ptr %32, align 8, !tbaa !9
  %73 = call i32 @pthread_join(i64 noundef %72, ptr noundef nonnull %2) #5
  %74 = load i64, ptr %34, align 16, !tbaa !9
  %75 = call i32 @pthread_join(i64 noundef %74, ptr noundef nonnull %2) #5
  %76 = load i64, ptr %36, align 8, !tbaa !9
  %77 = call i32 @pthread_join(i64 noundef %76, ptr noundef nonnull %2) #5
  %78 = load i64, ptr %38, align 16, !tbaa !9
  %79 = call i32 @pthread_join(i64 noundef %78, ptr noundef nonnull %2) #5
  %80 = load i64, ptr %40, align 8, !tbaa !9
  %81 = call i32 @pthread_join(i64 noundef %80, ptr noundef nonnull %2) #5
  %82 = load i64, ptr %42, align 16, !tbaa !9
  %83 = call i32 @pthread_join(i64 noundef %82, ptr noundef nonnull %2) #5
  %84 = load i64, ptr %44, align 8, !tbaa !9
  %85 = call i32 @pthread_join(i64 noundef %84, ptr noundef nonnull %2) #5
  %86 = load i32, ptr @beers, align 4, !tbaa !5
  %87 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %86)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 160, ptr nonnull %1) #5
  ret i32 0
}

; Function Attrs: nounwind
declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

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

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 = { 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 #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 = { "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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}


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