#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*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
void gotcha_strlen(void) {
  char ro[] = "asdf";
  printf("\"%s\" has %zd chars, ", ro, strlen(ro));
  printf("but we need 5 bytes to store the");
  printf("\nterminating '\\0' character.\n");
  printf("\n");
  char buffer[50];
  memset(buffer, 'G', 50);
  buffer[49] = '\0';
  strncpy(buffer, ro, strlen(ro));
  printf("buffer without terminating char:\n");
  printf("%s\n", buffer);
  printf("\n");
  strncpy(buffer, ro, strlen(ro) + 1);
  printf("copying the null character:\n");
  printf("%s\n", buffer);
  printf("\n");
}


x86 assembly (clang, -O0):
<gotcha_strlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0xeb2(%rip),%eax
mov    %eax,-0x5(%rbp)
mov    0xead(%rip),%al
mov    %al,-0x1(%rbp)
lea    -0x5(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x5(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x68(%rbp),%rsi
mov    %rax,%rdx
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    $0x47,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
movb   $0x0,-0xf(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x5(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x5(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x5(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x5(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gotcha_strlen>:
push   %r14
push   %rbx
sub    $0x48,%rsp
movb   $0x0,0xc(%rsp)
movl   $0x66647361,0x8(%rsp)
66
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0xe7d(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe91(%rip),%rdi
call   1050 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x47,%esi
mov    $0x32,%edx
call   1080 <memset@plt>
movb   $0x0,0x41(%rsp)
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <strncpy@plt>
lea    0xe66(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strncpy@plt>
lea    0xe52(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.gotcha_strlen.ro = [5 x i8] c"asdf\00"
@.str = [21 x i8] c"\22%s\22 has %zd chars, \00"
@.str.1 = [33 x i8] c"but we need 5 bytes to store the\00"
@.str.2 = [30 x i8] c"\0Aterminating '\\0' character.\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [34 x i8] c"buffer without terminating char:\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [29 x i8] c"copying the null character:\0A\00"
define void @gotcha_strlen() {
%1 = alloca [5 x i8]
%2 = alloca [50 x i8]
%3 = bitcast [5 x i8]* %1 to i8*
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%6 = call i64 @strlen(i8* %5)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %4, i64 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i8 signext 71, i32 50)
%13 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 49
store i8 0, i8* %13
%14 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%17 = call i64 @strlen(i8* %16)
%18 = call i32 @strncpy(i8* %14, i8* %15, i64 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%24 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%26 = call i64 @strlen(i8* %25)
%27 = add nsw i64 %26, 1
%28 = call i32 @strncpy(i8* %23, i8* %24, i64 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%30 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@__const.gotcha_strlen.ro = [5 x i8] c"asdf\00"
@.str = [21 x i8] c"\22%s\22 has %zd chars, \00"
@.str.1 = [33 x i8] c"but we need 5 bytes to store the\00"
@str = [29 x i8] c"\0Aterminating '\\0' character.\00"
@str.7 = [33 x i8] c"buffer without terminating char:\00"
@str.8 = [28 x i8] c"copying the null character:\00"
define void @gotcha_strlen() {
%1 = alloca [5 x i8]
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%4 = call i64 @strlen(i8* %3)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %3, i64 %4)
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%8 = call i32 @putchar(i32 10)
%9 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i8 signext 71, i32 50)
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 49
store i8 0, i8* %11
%12 = call i64 @strlen(i8* %3)
%13 = call i32 @strncpy(i8* %9, i8* %3, i64 %12)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%15 = call i32 @puts(i8* %9)
%16 = call i32 @putchar(i32 10)
%17 = call i64 @strlen(i8* %3)
%18 = add nsw i64 %17, 1
%19 = call i32 @strncpy(i8* %9, i8* %3, i64 %18)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
%21 = call i32 @puts(i8* %9)
%22 = call i32 @putchar(i32 10)
ret void
}