#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint32_t;
typedef int int32_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ memset_s(int *, int, int /*<<< orphan*/, int);
int pow(double, float);
int rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/);
uint32_t gen_noise(int16_t *dst, uint32_t max_length, uint32_t sample_rate,
                   uint32_t time_ms, float dbValue) {
  uint32_t gen_length = 0;
  uint32_t i;
  int32_t tmp;
  if (dst == NULL) {
    (void)0;
    return 0;
  }
  if (sample_rate == 0 || time_ms == 0) {
    (void)0;
    return 0;
  }
  gen_length = sample_rate * time_ms / 1000;
  if (gen_length > max_length) {
    (void)0;
    return 0;
  }
  memset_s(dst, max_length, 0, gen_length);
  srand((unsigned int)time(0));
  for (i = 0; i < gen_length; ++i) {
    tmp =
        (int32_t)((rand() % 65537 - 32768) * 1.23 * pow(10.0, dbValue / 20.0));
    if (tmp > 32767)
      dst[i] = 32767;
    if (tmp < -32768)
      dst[i] = -32768;
    dst[i] = (int16_t)tmp;
  }
  return 1;
}


x86 assembly (clang, -O0):
<gen_noise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movss  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1188 <gen_noise+0x38>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <gen_noise+0x173>
cmpl   $0x0,-0x18(%rbp)
je     119c <gen_noise+0x4c>
cmpl   $0x0,-0x1c(%rbp)
jne    11a8 <gen_noise+0x58>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <gen_noise+0x173>
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11d2 <gen_noise+0x82>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <gen_noise+0x173>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%ecx
xor    %edx,%edx
call   1060 <memset_s@plt>
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12bc <gen_noise+0x16c>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x10001,%ecx
cltd
idiv   %ecx
sub    $0x8000,%edx
cvtsi2sd %edx,%xmm0
movsd  0xdeb(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdc9(%rip),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm1
movsd  0xdb1(%rip),%xmm0
call   1050 <pow@plt>
movsd  -0x38(%rbp),%xmm0
cvtsi2sd %eax,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x7fff,-0x2c(%rbp)
jle    1284 <gen_noise+0x134>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movl   $0x7fff,(%rax,%rcx,4)
cmpl   $0xffff8000,-0x2c(%rbp)
jge    12a0 <gen_noise+0x150>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
movl   $0xffff8000,(%rax,%rcx,4)
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11f8 <gen_noise+0xa8>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_noise>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %r14d,%r14d
test   %rdi,%rdi
je     1262 <gen_noise+0x112>
test   %edx,%edx
je     1262 <gen_noise+0x112>
mov    %ecx,%r15d
test   %ecx,%ecx
je     1262 <gen_noise+0x112>
imul   %edx,%r15d
movslq %r15d,%rax
imul   $0x10624dd3,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x26,%rbp
add    %eax,%ebp
cmp    %esi,%ebp
jg     1262 <gen_noise+0x112>
mov    %rdi,%rbx
movss  %xmm0,0xc(%rsp)
xor    %edx,%edx
mov    %ebp,%ecx
call   1060 <memset_s@plt>
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,%edi
call   1030 <srand@plt>
mov    $0x1,%r14d
cmp    $0x3e8,%r15d
jl     1262 <gen_noise+0x112>
movss  0xc(%rsp),%xmm0
divss  0xe23(%rip),%xmm0
movss  %xmm0,0xc(%rsp)
mov    %ebp,%r15d
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <rand@plt>
cltq
imul   $0x7fff8001,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x2f,%rcx
add    %edx,%ecx
mov    %ecx,%edx
shl    $0x10,%edx
add    %ecx,%edx
neg    %edx
add    %edx,%eax
add    $0xffff8000,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0xddc(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0xdd6(%rip),%xmm0
movss  0xc(%rsp),%xmm1
call   1050 <pow@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0x10(%rsp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11f0 <gen_noise+0xa0>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gen_noise(i32* %0, i32 %1, i32 %2, i32 %3, float %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca float
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store float %4, float* %11
store i32 0, i32* %12
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %5
store i32 0, i32* %6
br label %85
18: ; preds = %5
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21, %18
store i32 0, i32* %6
br label %85
25: ; preds = %21
%26 = load i32, i32* %9
%27 = load i32, i32* %10
%28 = mul nsw i32 %26, %27
%29 = sdiv i32 %28, 1000
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = load i32, i32* %8
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 0, i32* %6
br label %85
34: ; preds = %25
%35 = load i32*, i32** %7
%36 = load i32, i32* %8
%37 = load i32, i32* %12
%38 = call i32 @memset_s(i32* %35, i32 %36, i32 0, i32 %37)
%39 = call i64 @time(i32 0)
%40 = trunc i64 %39 to i32
%41 = call i32 @srand(i32 %40)
store i32 0, i32* %13
br label %42
42: ; preds = %81, %34
%43 = load i32, i32* %13
%44 = load i32, i32* %12
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %84
46: ; preds = %42
%47 = call i32 (...) @rand()
%48 = srem i32 %47, 65537
%49 = sub nsw i32 %48, 32768
%50 = sitofp i32 %49 to double
%51 = fmul double %50, 1.230000e+00
%52 = load float, float* %11
%53 = fpext float %52 to double
%54 = fdiv double %53, 2.000000e+01
%55 = fptrunc double %54 to float
%56 = call i32 @pow(double 1.000000e+01, float %55)
%57 = sitofp i32 %56 to double
%58 = fmul double %51, %57
%59 = fptosi double %58 to i32
store i32 %59, i32* %14
%60 = load i32, i32* %14
%61 = icmp sgt i32 %60, 32767
br i1 %61, label %62, label %67
62: ; preds = %46
%63 = load i32*, i32** %7
%64 = load i32, i32* %13
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
store i32 32767, i32* %66
br label %67
67: ; preds = %62, %46
%68 = load i32, i32* %14
%69 = icmp slt i32 %68, -32768
br i1 %69, label %70, label %75
70: ; preds = %67
%71 = load i32*, i32** %7
%72 = load i32, i32* %13
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
store i32 -32768, i32* %74
br label %75
75: ; preds = %70, %67
%76 = load i32, i32* %14
%77 = load i32*, i32** %7
%78 = load i32, i32* %13
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
store i32 %76, i32* %80
br label %81
81: ; preds = %75
%82 = load i32, i32* %13
%83 = add nsw i32 %82, 1
store i32 %83, i32* %13
br label %42
84: ; preds = %42
store i32 1, i32* %6
br label %85
85: ; preds = %84, %33, %24, %17
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
define i32 @gen_noise(i32* %0, i32 %1, i32 %2, i32 %3, float %4) {
%6 = icmp eq i32* %0, null
br i1 %6, label %38, label %7
7: ; preds = %5
%8 = icmp eq i32 %2, 0
%9 = icmp eq i32 %3, 0
%10 = or i1 %8, %9
br i1 %10, label %38, label %11
11: ; preds = %7
%12 = mul nsw i32 %3, %2
%13 = sdiv i32 %12, 1000
%14 = icmp sgt i32 %13, %1
br i1 %14, label %38, label %15
15: ; preds = %11
%16 = tail call i32 @memset_s(i32* %0, i32 %1, i32 0, i32 %13)
%17 = tail call i64 @time(i32 0)
%18 = trunc i64 %17 to i32
%19 = tail call i32 @srand(i32 %18)
%20 = icmp sgt i32 %12, 999
br i1 %20, label %21, label %38
21: ; preds = %15
%22 = fdiv float %4, 2.000000e+01
%23 = zext i32 %13 to i64
br label %24
24: ; preds = %21, %24
%25 = phi i64 [ 0, %21 ], [ %36, %24 ]
%26 = tail call i32 (...) @rand()
%27 = srem i32 %26, 65537
%28 = add nsw i32 %27, -32768
%29 = sitofp i32 %28 to double
%30 = fmul double %29, 1.230000e+00
%31 = tail call i32 @pow(double 1.000000e+01, float %22)
%32 = sitofp i32 %31 to double
%33 = fmul double %30, %32
%34 = fptosi double %33 to i32
%35 = getelementptr inbounds i32, i32* %0, i64 %25
store i32 %34, i32* %35
%36 = add nuw nsw i64 %25, 1
%37 = icmp eq i64 %36, %23
br i1 %37, label %38, label %24
38: ; preds = %24, %15, %11, %7, %5
%39 = phi i32 [ 0, %5 ], [ 0, %7 ], [ 0, %11 ], [ 1, %15 ], [ 1, %24 ]
ret i32 %39
}