#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 */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void bufferAccessOutOfBounds(void) {
  char a[5];
  fgets(a, 5, stdin);
  fgets(a, 6, stdin);
  sprintf(a, "ab%s", "cd");
  sprintf(a, "ab%s", "cde");
  snprintf(a, 5, "abcde%i", 1);
  snprintf(a, 6, "abcde%i", 1);
  strcpy(a, "abcd");
  strcpy(a, "abcde");
  strncpy(a, "abcde", 5);
  strncpy(a, "abcde", 6);
  fread(a, 1, 5, stdin);
  fread(a, 1, 6, stdin);
  fwrite(a, 1, 5, stdout);
  fread(a, 1, 6, stdout);
}


x86 assembly (clang, -O0):
<bufferAccessOutOfBounds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x5(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1070 <fgets@plt>
lea    -0x5(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
call   1070 <fgets@plt>
lea    -0x5(%rbp),%rdi
lea    0xe4f(%rip),%rsi
lea    0xe57(%rip),%rdx
call   1080 <sprintf@plt>
lea    -0x5(%rbp),%rdi
lea    0xe38(%rip),%rsi
lea    0xe45(%rip),%rdx
call   1080 <sprintf@plt>
lea    -0x5(%rbp),%rdi
mov    $0x5,%esi
lea    0xe21(%rip),%rdx
mov    $0x1,%ecx
call   1060 <snprintf@plt>
lea    -0x5(%rbp),%rdi
mov    $0x6,%esi
lea    0xe07(%rip),%rdx
mov    $0x1,%ecx
call   1060 <snprintf@plt>
lea    -0x5(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x5(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x5(%rbp),%rdi
lea    0xddf(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
lea    -0x5(%rbp),%rdi
lea    0xdca(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
lea    -0x5(%rbp),%rdi
mov    0x2d8b(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x5,%edx
call   1050 <fread@plt>
lea    -0x5(%rbp),%rdi
mov    0x2d6f(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x6,%edx
call   1050 <fread@plt>
lea    -0x5(%rbp),%rdi
mov    0x2d43(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x5,%edx
call   1090 <fwrite@plt>
lea    -0x5(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x6,%edx
call   1050 <fread@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufferAccessOutOfBounds>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r15
mov    (%r15),%edx
lea    0xb(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1070 <fgets@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x6,%esi
call   1070 <fgets@plt>
lea    0xe54(%rip),%r14
lea    0xe5c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0xe4f(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0xe2e(%rip),%r14
mov    %rbx,%rdi
mov    $0x5,%esi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1060 <snprintf@plt>
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xdfb(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
call   1050 <fread@plt>
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   1050 <fread@plt>
mov    0x2d65(%rip),%r14
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
call   1090 <fwrite@plt>
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   1050 <fread@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [5 x i8] c"ab%s\00"
@.str.1 = [3 x i8] c"cd\00"
@.str.2 = [4 x i8] c"cde\00"
@.str.3 = [8 x i8] c"abcde%i\00"
@.str.4 = [5 x i8] c"abcd\00"
@.str.5 = [6 x i8] c"abcde\00"
@stdout = global i32 0
define void @bufferAccessOutOfBounds() {
%1 = alloca [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32 @fgets(i8* %2, i32 5, i32 %3)
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %5, i32 6, i32 %6)
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%11 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%13 = call i32 @snprintf(i8* %12, i32 5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1)
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%15 = call i32 @snprintf(i8* %14, i32 6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1)
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%17 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%21 = call i32 @strncpy(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 5)
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%23 = call i32 @strncpy(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 6)
%24 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%25 = load i32, i32* @stdin
%26 = call i32 @fread(i8* %24, i32 1, i32 5, i32 %25)
%27 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%28 = load i32, i32* @stdin
%29 = call i32 @fread(i8* %27, i32 1, i32 6, i32 %28)
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%31 = load i32, i32* @stdout
%32 = call i32 @fwrite(i8* %30, i32 1, i32 5, i32 %31)
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%34 = load i32, i32* @stdout
%35 = call i32 @fread(i8* %33, i32 1, i32 6, i32 %34)
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [5 x i8] c"ab%s\00"
@.str.1 = [3 x i8] c"cd\00"
@.str.2 = [4 x i8] c"cde\00"
@.str.3 = [8 x i8] c"abcde%i\00"
@.str.4 = [5 x i8] c"abcd\00"
@.str.5 = [6 x i8] c"abcde\00"
@stdout = global i32 0
define void @bufferAccessOutOfBounds() {
%1 = alloca [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32 @fgets(i8* %2, i32 5, i32 %3)
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %2, i32 6, i32 %5)
%7 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @snprintf(i8* %2, i32 5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1)
%10 = call i32 @snprintf(i8* %2, i32 6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 1)
%11 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%12 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%13 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 5)
%14 = call i32 @strncpy(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 6)
%15 = load i32, i32* @stdin
%16 = call i32 @fread(i8* %2, i32 1, i32 5, i32 %15)
%17 = load i32, i32* @stdin
%18 = call i32 @fread(i8* %2, i32 1, i32 6, i32 %17)
%19 = load i32, i32* @stdout
%20 = call i32 @fwrite(i8* %2, i32 1, i32 5, i32 %19)
%21 = load i32, i32* @stdout
%22 = call i32 @fread(i8* %2, i32 1, i32 6, i32 %21)
ret void
}