#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*/ sprintf(char *, char *, int, int, int);
int sscanf(char const *, char *, int *, int *, int *);
int /*<<< orphan*/ strcpy(char *, char *);
void date_to_isodate(const char *datestr, char *isodatestr) {
  int year, month, day;
  if ((sscanf(datestr, "%d/%d/%d", &year, &month, &day) != 3) &&
      (sscanf(datestr, "%d-%d-%d", &year, &month, &day) != 3)) {
    strcpy("INVALID", isodatestr);
    return;
  }
  if (day > 31) {
    int tmp = year;
    year = day;
    day = tmp;
  } else if (year > 31)
    ;
  else {
    strcpy("INVALID", isodatestr);
    return;
  }
  if (year < 1949)
    year += (year > 50) ? 1900 : 2000;
  sprintf(isodatestr, "%04d-%02d-%02d", year, month, day);
}


x86 assembly (clang, -O0):
<date_to_isodate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb5(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <sscanf@plt>
cmp    $0x3,%eax
je     119f <date_to_isodate+0x6f>
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <sscanf@plt>
cmp    $0x3,%eax
je     119f <date_to_isodate+0x6f>
mov    -0x10(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1030 <strcpy@plt>
jmp    1229 <date_to_isodate+0xf9>
cmpl   $0x1f,-0x1c(%rbp)
jle    11c0 <date_to_isodate+0x90>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11e9 <date_to_isodate+0xb9>
cmpl   $0x1f,-0x14(%rbp)
jle    11cf <date_to_isodate+0x9f>
jmp    11e4 <date_to_isodate+0xb4>
mov    -0x10(%rbp),%rsi
lea    0xe38(%rip),%rdi
call   1030 <strcpy@plt>
jmp    1229 <date_to_isodate+0xf9>
jmp    11e9 <date_to_isodate+0xb9>
cmpl   $0x79d,-0x14(%rbp)
jge    120f <date_to_isodate+0xdf>
mov    -0x14(%rbp),%edx
mov    $0x7d0,%eax
mov    $0x76c,%ecx
cmp    $0x32,%edx
cmovg  %ecx,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xdf6(%rip),%rsi
call   1050 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<date_to_isodate>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xebc(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1040 <sscanf@plt>
cmp    $0x3,%eax
je     1180 <date_to_isodate+0x50>
lea    0xea5(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
call   1040 <sscanf@plt>
cmp    $0x3,%eax
jne    11d9 <date_to_isodate+0xa9>
mov    0x10(%rsp),%eax
mov    0xc(%rsp),%edx
cmp    $0x20,%eax
jl     119c <date_to_isodate+0x6c>
mov    %eax,0xc(%rsp)
mov    %edx,0x10(%rsp)
mov    %edx,%r8d
mov    %eax,%edx
jmp    11a4 <date_to_isodate+0x74>
mov    %eax,%r8d
cmp    $0x1f,%edx
jle    11d9 <date_to_isodate+0xa9>
cmp    $0x79c,%edx
ja     11c4 <date_to_isodate+0x94>
lea    0x7d0(%rdx),%eax
cmp    $0x33,%edx
lea    0x76c(%rdx),%ecx
cmovb  %eax,%ecx
mov    %ecx,0xc(%rsp)
mov    %ecx,%edx
mov    0x14(%rsp),%ecx
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
call   1050 <sprintf@plt>
jmp    11e8 <date_to_isodate+0xb8>
lea    0xe32(%rip),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"%d/%d/%d\00"
@.str.1 = [9 x i8] c"%d-%d-%d\00"
@.str.2 = [8 x i8] c"INVALID\00"
@.str.3 = [15 x i8] c"%04d-%02d-%02d\00"
define void @date_to_isodate(i8* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%10 = icmp eq i32 %9, 3
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4, i32* %5)
%13 = icmp eq i32 %12, 3
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @strcpy(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %37
16: ; preds = %11, %2
%17 = load i32, i32* %5
%18 = icmp sgt i32 %17, 31
%19 = load i32, i32* %3
br i1 %18, label %20, label %21
20: ; preds = %16
store i32 %17, i32* %3
store i32 %19, i32* %5
br label %25
21: ; preds = %16
%22 = icmp sgt i32 %19, 31
br i1 %22, label %25, label %23
23: ; preds = %21
%24 = call i32 @strcpy(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %37
25: ; preds = %21, %20
%26 = phi i32 [ %17, %21 ], [ %19, %20 ]
%27 = phi i32 [ %19, %21 ], [ %17, %20 ]
%28 = icmp ult i32 %27, 1949
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = icmp ugt i32 %27, 50
%31 = select i1 %30, i32 1900, i32 2000
%32 = add nuw nsw i32 %31, %27
store i32 %32, i32* %3
br label %33
33: ; preds = %29, %25
%34 = phi i32 [ %32, %29 ], [ %27, %25 ]
%35 = load i32, i32* %4
%36 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %35, i32 %26)
br label %37
37: ; preds = %33, %23, %14
ret void
}