#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 /*<<< orphan*/ time_t;
struct tm {
  int tm_year;
};

/* Variables and functions */
scalar_t__ EOVERFLOW;
char *asctime_r(struct tm *, char *);
scalar_t__ errno;
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int do_test(void) {
  int result = 0;
  time_t t = time(NULL);
  struct tm *tp = localtime(&t);
  tp->tm_year = 10000 - 1900;
  char buf[1000];
  errno = 0;
  buf[26] = '\xff';
  char *s = asctime_r(tp, buf);
  if (s != NULL || errno != EOVERFLOW) {
    puts("asctime_r did not fail correctly");
    result = 1;
  }
  if (buf[26] != '\xff') {
    puts("asctime_r overwrote 27th byte in buffer");
    result = 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,-0x8(%rbp)
lea    -0x8(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x1fa4,(%rax)
mov    0x2e5c(%rip),%rax
movq   $0x0,(%rax)
movb   $0xff,-0x3e6(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1060 <asctime_r@plt>
mov    %rax,-0x408(%rbp)
cmpq   $0x0,-0x408(%rbp)
jne    11c9 <do_test+0x89>
mov    0x2e22(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
je     11dc <do_test+0x9c>
lea    0xe30(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
movsbl -0x3e6(%rbp),%eax
cmp    $0xffffffff,%eax
je     11ff <do_test+0xbf>
lea    0xe2e(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %r14
push   %rbx
sub    $0x3f8,%rsp
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
movl   $0x1fa4,(%rax)
mov    0x2e6a(%rip),%r14
movq   $0x0,(%r14)
movb   $0xff,0x2a(%rsp)
lea    0x10(%rsp),%rsi
mov    %rax,%rdi
call   1060 <asctime_r@plt>
test   %rax,%rax
jne    119b <do_test+0x5b>
mov    (%r14),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
je     11ac <do_test+0x6c>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
cmpb   $0xff,0x2a(%rsp)
je     11c4 <do_test+0x84>
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@errno = global i64 0
@EOVERFLOW = global i64 0
@.str = [33 x i8] c"asctime_r did not fail correctly\00"
@.str.1 = [40 x i8] c"asctime_r overwrote 27th byte in buffer\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.tm*
%4 = alloca [1000 x i8]
%5 = alloca i8*
store i32 0, i32* %1
%6 = call i32 @time(i32* null)
store i32 %6, i32* %2
%7 = call %struct.tm* @localtime(i32* %2)
store %struct.tm* %7, %struct.tm** %3
%8 = load %struct.tm*, %struct.tm** %3
%9 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 0
store i32 8100, i32* %9
store i64 0, i64* @errno
%10 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 26
store i8 -1, i8* %10
%11 = load %struct.tm*, %struct.tm** %3
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%13 = call i8* @asctime_r(%struct.tm* %11, i8* %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %0
%17 = load i64, i64* @errno
%18 = load i64, i64* @EOVERFLOW
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16, %0
%21 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %22
22: ; preds = %20, %16
%23 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 26
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %29
29: ; preds = %27, %22
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@errno = global i64 0
@EOVERFLOW = global i64 0
@.str = [33 x i8] c"asctime_r did not fail correctly\00"
@.str.1 = [40 x i8] c"asctime_r overwrote 27th byte in buffer\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = bitcast i32* %1 to i8*
%4 = tail call i32 @time(i32* null)
store i32 %4, i32* %1
%5 = call %struct.tm* @localtime(i32* %1)
%6 = getelementptr inbounds %struct.tm, %struct.tm* %5, i64 0, i32 0
store i32 8100, i32* %6
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
store i64 0, i64* @errno
%8 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 26
store i8 -1, i8* %8
%9 = call i8* @asctime_r(%struct.tm* %5, i8* %7)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i64, i64* @errno
%13 = load i64, i64* @EOVERFLOW
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %11, %0
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = phi i32 [ 1, %15 ], [ 0, %11 ]
%19 = load i8, i8* %8
%20 = icmp eq i8 %19, -1
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = phi i32 [ 1, %21 ], [ %18, %17 ]
ret i32 %24
}