#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*/ isdigit(char);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int strlen(char *);
int Decode_date_year(char *text, int flag) {
  int ret, i;
  for (i = 0; text[i] != 0; i++)
    if (!isdigit(text[i]))
      return (-1);
  if (strlen(text) != 4)
    return (-2);
  sscanf(text, "%d", &ret);
  if (ret < 0 || ret > 3000)
    return (-2);
  return (ret);
}


x86 assembly (clang, -O0):
<Decode_date_year>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1194 <Decode_date_year+0x64>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1181 <Decode_date_year+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <Decode_date_year+0xbf>
jmp    1186 <Decode_date_year+0x56>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1146 <Decode_date_year+0x16>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x4,%eax
je     11b2 <Decode_date_year+0x82>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11ef <Decode_date_year+0xbf>
mov    -0x10(%rbp),%rdi
lea    0xe43(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <sscanf@plt>
cmpl   $0x0,-0x18(%rbp)
jl     11dd <Decode_date_year+0xad>
cmpl   $0xbb8,-0x18(%rbp)
jle    11e9 <Decode_date_year+0xb9>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11ef <Decode_date_year+0xbf>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Decode_date_year>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%al
test   %al,%al
je     1167 <Decode_date_year+0x37>
lea    0x1(%r14),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11a3 <Decode_date_year+0x73>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1150 <Decode_date_year+0x20>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
cmp    $0x4,%ecx
jne    11a8 <Decode_date_year+0x78>
lea    0xe7e(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %r14,%rdi
call   1050 <sscanf@plt>
mov    0x4(%rsp),%ecx
cmp    $0xbb9,%ecx
mov    $0xfffffffe,%eax
cmovb  %ecx,%eax
jmp    11a8 <Decode_date_year+0x78>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i32 @Decode_date_year(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %26, %2
%9 = load i8*, i8** %4
%10 = load i32, i32* %7
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %8
%17 = load i8*, i8** %4
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = call i32 @isdigit(i8 signext %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %16
store i32 -1, i32* %3
br label %45
25: ; preds = %16
br label %26
26: ; preds = %25
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %8
29: ; preds = %8
%30 = load i8*, i8** %4
%31 = call i32 @strlen(i8* %30)
%32 = icmp ne i32 %31, 4
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -2, i32* %3
br label %45
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %34
%40 = load i32, i32* %6
%41 = icmp sgt i32 %40, 3000
br i1 %41, label %42, label %43
42: ; preds = %39, %34
store i32 -2, i32* %3
br label %45
43: ; preds = %39
%44 = load i32, i32* %6
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %42, %33, %24
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @Decode_date_year(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %17, label %11
7: ; preds = %11
%8 = getelementptr inbounds i8, i8* %0, i64 %16
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %2, %7
%12 = phi i64 [ %16, %7 ], [ 0, %2 ]
%13 = phi i8 [ %9, %7 ], [ %5, %2 ]
%14 = tail call i32 @isdigit(i8 signext %13)
%15 = icmp eq i32 %14, 0
%16 = add nuw i64 %12, 1
br i1 %15, label %25, label %7
17: ; preds = %7, %2
%18 = tail call i32 @strlen(i8* %0)
%19 = icmp eq i32 %18, 4
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%22 = load i32, i32* %3
%23 = icmp ugt i32 %22, 3000
%24 = select i1 %23, i32 -2, i32 %22
br label %25
25: ; preds = %11, %20, %17
%26 = phi i32 [ -2, %17 ], [ %24, %20 ], [ -1, %11 ]
ret i32 %26
}