; LLVM IR from rweather/lightweight-crypto :: src/combined (aead-random)

; 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.timeval = type { i64, i64 }

@.str = external hidden unnamed_addr constant [13 x i8], align 1
@aead_chacha_state = external hidden global <{ i32, i32, i32, i32, [12 x i32] }>, align 16

; Function Attrs: nounwind uwtable
define dso_local void @aead_random_reseed() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca %struct.timeval, align 8
  %4 = call i64 (i64, ...) @syscall(i64 noundef 318, ptr noundef getelementptr inbounds (i32, ptr @aead_chacha_state, i64 4), i32 noundef 32, i32 noundef 0) #4
  %5 = icmp ne i64 %4, 32
  br i1 %5, label %6, label %34

6:                                                ; preds = %0
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %7 = call i32 (ptr, i32, ...) @open(ptr noundef @.str, i32 noundef 0)
  store i32 %7, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = icmp sge i32 %8, 0
  br i1 %9, label %10, label %18

10:                                               ; preds = %6
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = call i64 @read(i32 noundef %11, ptr noundef getelementptr inbounds (i32, ptr @aead_chacha_state, i64 4), i64 noundef 32)
  %13 = icmp eq i64 %12, 32
  br i1 %13, label %14, label %15

14:                                               ; preds = %10
  store i32 1, ptr %1, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %14, %10
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = call i32 @close(i32 noundef %16)
  br label %18

18:                                               ; preds = %15, %6
  %19 = load i32, ptr %1, align 4, !tbaa !5
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %33, label %21

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #4
  %22 = call i32 @gettimeofday(ptr noundef %3, ptr noundef null) #4
  %23 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 0
  %24 = load i64, ptr %23, align 8, !tbaa !9
  %25 = trunc i64 %24 to i32
  %26 = load i32, ptr getelementptr inbounds ([16 x i32], ptr @aead_chacha_state, i64 0, i64 4), align 16, !tbaa !5
  %27 = xor i32 %26, %25
  store i32 %27, ptr getelementptr inbounds ([16 x i32], ptr @aead_chacha_state, i64 0, i64 4), align 16, !tbaa !5
  %28 = getelementptr inbounds %struct.timeval, ptr %3, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !12
  %30 = trunc i64 %29 to i32
  %31 = load i32, ptr getelementptr inbounds ([16 x i32], ptr @aead_chacha_state, i64 0, i64 5), align 4, !tbaa !5
  %32 = xor i32 %31, %30
  store i32 %32, ptr getelementptr inbounds ([16 x i32], ptr @aead_chacha_state, i64 0, i64 5), align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #4
  br label %33

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

34:                                               ; preds = %33, %0
  call void @aead_chacha_rekey()
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @aead_chacha_rekey() #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
declare i64 @syscall(i64 noundef, ...) #2

declare i32 @open(ptr noundef, i32 noundef, ...) #3

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #3

declare i32 @close(i32 noundef) #3

; Function Attrs: nounwind
declare i32 @gettimeofday(ptr 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 = { 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 = { "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, !11, i64 0}
!10 = !{!"timeval", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}


; 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.timeval = type { i64, i64 }

@.str = external hidden unnamed_addr constant [13 x i8], align 1
@aead_chacha_state = external hidden global <{ i32, i32, i32, i32, [12 x i32] }>, align 16

; Function Attrs: nounwind uwtable
define dso_local void @aead_random_reseed() local_unnamed_addr #0 {
  %1 = alloca %struct.timeval, align 16
  %2 = tail call i64 (i64, ...) @syscall(i64 noundef 318, ptr noundef nonnull getelementptr inbounds (i8, ptr @aead_chacha_state, i64 16), i32 noundef 32, i32 noundef 0) #6
  %.not = icmp eq i64 %2, 32
  br i1 %.not, label %14, label %3

3:                                                ; preds = %0
  %4 = tail call i32 (ptr, i32, ...) @open(ptr noundef nonnull @.str, i32 noundef 0) #6
  %5 = icmp sgt i32 %4, -1
  br i1 %5, label %6, label %.critedge

6:                                                ; preds = %3
  %7 = tail call i64 @read(i32 noundef %4, ptr noundef nonnull getelementptr inbounds (i8, ptr @aead_chacha_state, i64 16), i64 noundef 32) #6
  %.not6 = icmp eq i64 %7, 32
  %8 = tail call i32 @close(i32 noundef %4) #6
  br i1 %.not6, label %14, label %.critedge

.critedge:                                        ; preds = %3, %6
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %1) #6
  %9 = call i32 @gettimeofday(ptr noundef nonnull %1, ptr noundef null) #6
  %10 = load <2 x i64>, ptr %1, align 16, !tbaa !5
  %11 = trunc <2 x i64> %10 to <2 x i32>
  %12 = load <2 x i32>, ptr getelementptr inbounds (i8, ptr @aead_chacha_state, i64 16), align 16, !tbaa !9
  %13 = xor <2 x i32> %12, %11
  store <2 x i32> %13, ptr getelementptr inbounds (i8, ptr @aead_chacha_state, i64 16), align 16, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %1) #6
  br label %14

14:                                               ; preds = %6, %.critedge, %0
  tail call void @aead_chacha_rekey()
  ret void
}

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

; Function Attrs: nounwind
declare i64 @syscall(i64 noundef, ...) local_unnamed_addr #2

; Function Attrs: nofree
declare noundef i32 @open(ptr nocapture noundef readonly, i32 noundef, ...) local_unnamed_addr #3

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #3

declare i32 @close(i32 noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i32 @gettimeofday(ptr nocapture noundef, ptr nocapture noundef) 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 = { 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 = { nofree "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 = { 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass