; LLVM IR from Chasmiccoder/Algos-for-Amigos :: Computer-Science/4-Cybersecurity/2-PicoCTF/4-Stonks (vuln)

; 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.Stonks = type { i32, [5 x i8], ptr }

; 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

; Function Attrs: nounwind uwtable
define dso_local ptr @pick_symbol_with_AI(i32 noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 1
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  store ptr null, ptr %2, align 8
  br label %50

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %11 = call noalias ptr @malloc(i64 noundef 24) #5
  store ptr %11, ptr %4, align 8, !tbaa !9
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.Stonks, ptr %13, i32 0, i32 0
  store i32 %12, ptr %14, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %15 = call i32 @rand() #4
  %16 = srem i32 %15, 4
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %43, %10
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = icmp sle i32 %19, 4
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %46

22:                                               ; preds = %18
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = icmp slt i32 %23, %24
  br i1 %25, label %26, label %36

26:                                               ; preds = %22
  %27 = call i32 @rand() #4
  %28 = srem i32 %27, 26
  %29 = add nsw i32 65, %28
  %30 = trunc i32 %29 to i8
  %31 = load ptr, ptr %4, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.Stonks, ptr %31, i32 0, i32 1
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [5 x i8], ptr %32, i64 0, i64 %34
  store i8 %30, ptr %35, align 1, !tbaa !13
  br label %42

36:                                               ; preds = %22
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.Stonks, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [5 x i8], ptr %38, i64 0, i64 %40
  store i8 0, ptr %41, align 1, !tbaa !13
  br label %42

42:                                               ; preds = %36, %26
  br label %43

43:                                               ; preds = %42
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %18, !llvm.loop !14

46:                                               ; preds = %21
  %47 = load ptr, ptr %4, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.Stonks, ptr %47, i32 0, i32 2
  store ptr null, ptr %48, align 8, !tbaa !16
  %49 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %49, ptr %2, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %50

50:                                               ; preds = %46, %9
  %51 = load ptr, ptr %2, align 8
  ret ptr %51
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind
declare i32 @rand() #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind allocsize(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 "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 allocsize(0) }

!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}
!11 = !{!12, !6, i64 0}
!12 = !{!"Stonks", !6, i64 0, !7, i64 4, !10, i64 16}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!12, !10, i64 16}


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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @pick_symbol_with_AI(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 1
  br i1 %2, label %36, label %3

3:                                                ; preds = %1
  %4 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  store i32 %0, ptr %4, align 8, !tbaa !5
  %5 = tail call i32 @rand() #4
  %6 = srem i32 %5, 4
  %7 = getelementptr inbounds i8, ptr %4, i64 4
  %.not = icmp slt i32 %6, 0
  br i1 %.not, label %.thread, label %8

.thread:                                          ; preds = %3
  store i8 0, ptr %7, align 1, !tbaa !11
  br label %.thread19

8:                                                ; preds = %3
  %9 = tail call i32 @rand() #4
  %10 = srem i32 %9, 26
  %11 = trunc nsw i32 %10 to i8
  %12 = add nsw i8 %11, 65
  store i8 %12, ptr %7, align 1, !tbaa !11
  %.not.1 = icmp eq i32 %6, 0
  br i1 %.not.1, label %.thread19, label %14

.thread19:                                        ; preds = %8, %.thread
  %13 = getelementptr inbounds i8, ptr %4, i64 5
  store i8 0, ptr %13, align 1, !tbaa !11
  br label %.thread21

14:                                               ; preds = %8
  %15 = tail call i32 @rand() #4
  %16 = srem i32 %15, 26
  %17 = trunc nsw i32 %16 to i8
  %18 = add nsw i8 %17, 65
  %19 = getelementptr inbounds i8, ptr %4, i64 5
  store i8 %18, ptr %19, align 1, !tbaa !11
  %.not.2 = icmp eq i32 %6, 1
  br i1 %.not.2, label %.thread21, label %21

.thread21:                                        ; preds = %14, %.thread19
  %20 = getelementptr inbounds i8, ptr %4, i64 6
  store i8 0, ptr %20, align 1, !tbaa !11
  br label %32

21:                                               ; preds = %14
  %22 = tail call i32 @rand() #4
  %23 = srem i32 %22, 26
  %24 = trunc nsw i32 %23 to i8
  %25 = add nsw i8 %24, 65
  %26 = getelementptr inbounds i8, ptr %4, i64 6
  store i8 %25, ptr %26, align 1, !tbaa !11
  %.not.3 = icmp ult i32 %6, 3
  br i1 %.not.3, label %32, label %27

27:                                               ; preds = %21
  %28 = tail call i32 @rand() #4
  %29 = srem i32 %28, 26
  %30 = trunc nsw i32 %29 to i8
  %31 = add nsw i8 %30, 65
  br label %32

32:                                               ; preds = %21, %.thread21, %27
  %.sink = phi i8 [ %31, %27 ], [ 0, %.thread21 ], [ 0, %21 ]
  %33 = getelementptr inbounds i8, ptr %4, i64 7
  store i8 %.sink, ptr %33, align 1, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %4, i64 8
  store i8 0, ptr %34, align 1, !tbaa !11
  %35 = getelementptr inbounds i8, ptr %4, i64 16
  store ptr null, ptr %35, align 8, !tbaa !12
  br label %36

36:                                               ; preds = %1, %32
  %.013 = phi ptr [ %4, %32 ], [ null, %1 ]
  ret ptr %.013
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare i32 @rand() 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 "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 allocsize(0) }
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, !7, i64 0}
!6 = !{!"Stonks", !7, i64 0, !8, i64 4, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{!6, !10, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass