#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 */

/* Variables and functions */
char *alloca(int);
char *realloc(char *, int);
int /*<<< orphan*/ sink(char *);
void warn_realloc_alloca(int n, int i) {
  char *p, *q;
  {
    p = alloca(n);
    sink(p);
    q = realloc(p, n);
    sink(q);
  }
  {
    p = (char *)alloca(n + 1);
    sink(p);
    q = realloc(p, n);
    sink(q);
  }
  {
    p = (char *)alloca(n + 2) + i;
    sink(p);
    q = realloc(p, n);
    sink(q);
  }
}


x86 assembly (clang, -O0):
<warn_realloc_alloca>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <alloca@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sink@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <sink@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1030 <alloca@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sink@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <sink@plt>
mov    -0x4(%rbp),%edi
add    $0x2,%edi
call   1030 <alloca@plt>
movslq -0x8(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <sink@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <sink@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<warn_realloc_alloca>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebx
call   1030 <alloca@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <sink@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
mov    %rax,%rdi
call   1040 <sink@plt>
lea    0x1(%rbx),%edi
call   1030 <alloca@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <sink@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
mov    %rax,%rdi
call   1040 <sink@plt>
lea    0x2(%rbx),%edi
call   1030 <alloca@plt>
movslq %r14d,%rbp
add    %rax,%rbp
mov    %rbp,%rdi
call   1040 <sink@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <realloc@plt>
mov    %rax,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <sink@plt>

LLVM IR (-O0):
define void @warn_realloc_alloca(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = call i8* @alloca(i32 %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @sink(i8* %9)
%11 = load i8*, i8** %5
%12 = load i32, i32* %3
%13 = call i8* @realloc(i8* %11, i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i32 @sink(i8* %14)
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
%18 = call i8* @alloca(i32 %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = call i32 @sink(i8* %19)
%21 = load i8*, i8** %5
%22 = load i32, i32* %3
%23 = call i8* @realloc(i8* %21, i32 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @sink(i8* %24)
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 2
%28 = call i8* @alloca(i32 %27)
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = call i32 @sink(i8* %32)
%34 = load i8*, i8** %5
%35 = load i32, i32* %3
%36 = call i8* @realloc(i8* %34, i32 %35)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = call i32 @sink(i8* %37)
ret void
}

LLVM IR (-O2):
define void @warn_realloc_alloca(i32 %0, i32 %1) {
%3 = tail call i8* @alloca(i32 %0)
%4 = tail call i32 @sink(i8* %3)
%5 = tail call i8* @realloc(i8* %3, i32 %0)
%6 = tail call i32 @sink(i8* %5)
%7 = add nsw i32 %0, 1
%8 = tail call i8* @alloca(i32 %7)
%9 = tail call i32 @sink(i8* %8)
%10 = tail call i8* @realloc(i8* %8, i32 %0)
%11 = tail call i32 @sink(i8* %10)
%12 = add nsw i32 %0, 2
%13 = tail call i8* @alloca(i32 %12)
%14 = sext i32 %1 to i64
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = tail call i32 @sink(i8* %15)
%17 = tail call i8* @realloc(i8* %15, i32 %0)
%18 = tail call i32 @sink(i8* %17)
ret void
}