#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*/ printf(char *, ...);
int timlib_strcpy(char *, char *, int);
void test_strcpy() {
  char whatever[1024];
  char overflow[8];
  char righton[8];
  int return_value;
  return_value = timlib_strcpy(whatever, "whatever", 1024);
  if (return_value)
    printf("Good: should be \"whatever\", is \"%s\".\n", whatever);
  else
    printf("buffer overflow.\n");
  return_value = timlib_strcpy(overflow, "overflow", 8);
  if (!return_value)
    printf("Good: buffer overflow.\n");
  else
    printf("should not happen.\n");
  return_value = timlib_strcpy(righton, "righton", 8);
  if (return_value)
    printf("Good: should be \"righton\", is \"%s\".\n", righton);
  else
    printf("should not happen.\n");
}


x86 assembly (clang, -O0):
<test_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    -0x400(%rbp),%rdi
lea    0xec7(%rip),%rsi
mov    $0x400,%edx
call   1040 <timlib_strcpy@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
je     1170 <test_strcpy+0x50>
lea    -0x400(%rbp),%rsi
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    117e <test_strcpy+0x5e>
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x408(%rbp),%rdi
lea    0xea3(%rip),%rsi
mov    $0x8,%edx
call   1040 <timlib_strcpy@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jne    11bc <test_strcpy+0x9c>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ca <test_strcpy+0xaa>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe8c(%rip),%rsi
mov    $0x8,%edx
call   1040 <timlib_strcpy@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
je     120f <test_strcpy+0xef>
lea    -0x410(%rbp),%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    121d <test_strcpy+0xfd>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_strcpy>:
sub    $0x418,%rsp
lea    0xec2(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
call   1050 <timlib_strcpy@plt>
test   %eax,%eax
je     1166 <test_strcpy+0x36>
lea    0xeb1(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1172 <test_strcpy+0x42>
lea    0xefe(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb6(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    $0x8,%edx
call   1050 <timlib_strcpy@plt>
test   %eax,%eax
lea    0xed4(%rip),%rax
lea    0xee4(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0xe90(%rip),%rsi
mov    %rsp,%rdi
mov    $0x8,%edx
call   1050 <timlib_strcpy@plt>
test   %eax,%eax
je     11d2 <test_strcpy+0xa2>
lea    0xe80(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x418,%rsp
ret
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
add    $0x418,%rsp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"whatever\00"
@.str.1 = [38 x i8] c"Good: should be \22whatever\22, is \22%s\22.\0A\00"
@.str.2 = [18 x i8] c"buffer overflow.\0A\00"
@.str.3 = [9 x i8] c"overflow\00"
@.str.4 = [24 x i8] c"Good: buffer overflow.\0A\00"
@.str.5 = [20 x i8] c"should not happen.\0A\00"
@.str.6 = [8 x i8] c"righton\00"
@.str.7 = [37 x i8] c"Good: should be \22righton\22, is \22%s\22.\0A\00"
define void @test_strcpy() {
%1 = alloca [1024 x i8]
%2 = alloca [8 x i8]
%3 = alloca [8 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%6 = call i32 @timlib_strcpy(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 1024)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%16 = call i32 @timlib_strcpy(i8* %15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
%24 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%25 = call i32 @timlib_strcpy(i8* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 8)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %29)
br label %33
31: ; preds = %23
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"whatever\00"
@.str.1 = [38 x i8] c"Good: should be \22whatever\22, is \22%s\22.\0A\00"
@.str.3 = [9 x i8] c"overflow\00"
@.str.6 = [8 x i8] c"righton\00"
@.str.7 = [37 x i8] c"Good: should be \22righton\22, is \22%s\22.\0A\00"
@str = [17 x i8] c"buffer overflow.\00"
@str.8 = [23 x i8] c"Good: buffer overflow.\00"
@str.10 = [19 x i8] c"should not happen.\00"
define void @test_strcpy() {
%1 = alloca [1024 x i8]
%2 = alloca [8 x i8]
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%7 = call i32 @timlib_strcpy(i8* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 1024)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %13
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %9
%14 = call i32 @timlib_strcpy(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%15 = icmp eq i32 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0)
%17 = call i32 @puts(i8* dereferenceable(1) %16)
%18 = call i32 @timlib_strcpy(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 8)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %6)
br label %24
22: ; preds = %13
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %24
24: ; preds = %22, %20
ret void
}