#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*/ tmp;
typedef int time_t;
struct tm {
  int /*<<< orphan*/ tm_year;
};
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int clock_settime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
struct tm *localtime(int *);
int /*<<< orphan*/ memset(struct tm *, int /*<<< orphan*/, int);
int mktime(struct tm *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
char *strptime(char *, char *, struct tm *);
int /*<<< orphan*/ time(int *);
int set_time(char *date, char **argv) {
  size_t len = strlen(date);
  struct tm tmp;
  char *ret = NULL;
  time_t now;
  time(&now);
  struct tm *local = localtime(&now);
  memset(&tmp, 0, sizeof(tmp));
  if (len == 12) {
    ret = strptime(date, "%m%d%H%M%Y", &tmp);
  } else if (len == 10) {
    ret = strptime(date, "%m%d%H%M%y", &tmp);
  } else if (len == 8) {
    tmp.tm_year = local->tm_year;
    ret = strptime(date, "%m%d%H%M", &tmp);
  }
  if (ret == NULL) {
    fputs("Invalid date string\nSee 'date --help' for usage information.\n",
          stderr);
    return 1;
  }
  if (ret[0] != '\0') {
    fputs("Somehow gave extra data to strptime()? (This shouldn't happen!)\n",
          stderr);
    return 1;
  }
  struct timespec ts;
  time_t tval = mktime(&tmp);
  if (tval == -1) {
    fputs("Error calling mktime().\n", stderr);
    return 1;
  }
  struct tm *local_ts = localtime(&tval);
  tval = mktime(local_ts);
  if (tval == -1) {
    fputs("Error calling mktime(). (Second call.)\n", stderr);
    return 1;
  }
  ts.tv_sec = tval;
  ts.tv_nsec = 0;
  if (clock_settime(CLOCK_REALTIME, &ts) == -1) {
    perror(argv[0]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
call   1070 <time@plt>
lea    -0x34(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x40(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
cmpq   $0xc,-0x20(%rbp)
jne    1203 <set_time+0x73>
mov    -0x10(%rbp),%rdi
lea    0xe0f(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <strptime@plt>
mov    %rax,-0x30(%rbp)
jmp    1261 <set_time+0xd1>
cmpq   $0xa,-0x20(%rbp)
jne    122b <set_time+0x9b>
mov    -0x10(%rbp),%rdi
lea    0xdf2(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <strptime@plt>
mov    %rax,-0x30(%rbp)
jmp    125c <set_time+0xcc>
cmpq   $0x8,-0x20(%rbp)
jne    1257 <set_time+0xc7>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdcc(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <strptime@plt>
mov    %rax,-0x30(%rbp)
jmp    125c <set_time+0xcc>
jmp    1261 <set_time+0xd1>
cmpq   $0x0,-0x30(%rbp)
jne    128d <set_time+0xfd>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xda3(%rip),%rdi
call   1050 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    1381 <set_time+0x1f1>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12be <set_time+0x12e>
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    0xdb0(%rip),%rdi
call   1050 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    1381 <set_time+0x1f1>
lea    -0x28(%rbp),%rdi
call   1090 <mktime@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    12f5 <set_time+0x165>
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    0xdba(%rip),%rdi
call   1050 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    1381 <set_time+0x1f1>
lea    -0x54(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
call   1090 <mktime@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1339 <set_time+0x1a9>
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
lea    0xd8f(%rip),%rdi
call   1050 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    1381 <set_time+0x1f1>
mov    -0x54(%rbp),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x48(%rbp)
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   1080 <clock_settime@plt>
cmp    $0xffffffff,%eax
jne    137a <set_time+0x1ea>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1381 <set_time+0x1f1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_time>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    0x14(%rsp),%r12
mov    %r12,%rdi
call   1070 <time@plt>
mov    %r12,%rdi
call   1030 <localtime@plt>
mov    %rax,%r12
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
cmp    $0x8,%rbx
je     11ed <set_time+0x5d>
cmp    $0xc,%rbx
je     11fe <set_time+0x6e>
cmp    $0xa,%rbx
jne    122e <set_time+0x9e>
lea    0xe20(%rip),%rsi
jmp    1205 <set_time+0x75>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
lea    0xe1a(%rip),%rsi
jmp    1205 <set_time+0x75>
lea    0xdfb(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
call   10b0 <strptime@plt>
test   %rax,%rax
je     122e <set_time+0x9e>
cmpb   $0x0,(%rax)
je     1254 <set_time+0xc4>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
jmp    123e <set_time+0xae>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    0xde1(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0x8(%rsp),%rdi
call   1090 <mktime@plt>
mov    %eax,0x10(%rsp)
cmp    $0xffffffff,%eax
je     12b5 <set_time+0x125>
lea    0x10(%rsp),%rdi
call   1030 <localtime@plt>
mov    %rax,%rdi
call   1090 <mktime@plt>
mov    %eax,0x10(%rsp)
cmp    $0xffffffff,%eax
je     12ca <set_time+0x13a>
mov    %eax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0x18(%rsp),%rsi
call   1080 <clock_settime@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1248 <set_time+0xb8>
mov    (%r14),%rdi
call   10a0 <perror@plt>
jmp    1243 <set_time+0xb3>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
lea    0xdd9(%rip),%rdi
jmp    123e <set_time+0xae>
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
jmp    123e <set_time+0xae>

LLVM IR (-O0):
%struct.tm = type { i32 }
%struct.timespec = type { i32, i64 }
@.str = [11 x i8] c"%m%d%H%M%Y\00"
@.str.1 = [11 x i8] c"%m%d%H%M%y\00"
@.str.2 = [9 x i8] c"%m%d%H%M\00"
@.str.3 = [62 x i8] c"Invalid date string\0ASee 'date --help' for usage information.\0A\00"
@stderr = global i32 0
@.str.4 = [65 x i8] c"Somehow gave extra data to strptime()? (This shouldn't happen!)\0A\00"
@.str.5 = [25 x i8] c"Error calling mktime().\0A\00"
@.str.6 = [40 x i8] c"Error calling mktime(). (Second call.)\0A\00"
@CLOCK_REALTIME = global i32 0
define i32 @set_time(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca %struct.tm
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.tm*
%11 = alloca %struct.timespec
%12 = alloca i32
%13 = alloca %struct.tm*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %6
store i8* null, i8** %8
%16 = call i32 @time(i32* %9)
%17 = call %struct.tm* @localtime(i32* %9)
store %struct.tm* %17, %struct.tm** %10
%18 = call i32 @memset(%struct.tm* %7, i32 0, i32 4)
%19 = load i64, i64* %6
%20 = icmp eq i64 %19, 12
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = call i8* @strptime(i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.tm* %7)
store i8* %23, i8** %8
br label %42
24: ; preds = %2
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, 10
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %4
%29 = call i8* @strptime(i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %7)
store i8* %29, i8** %8
br label %41
30: ; preds = %24
%31 = load i64, i64* %6
%32 = icmp eq i64 %31, 8
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load %struct.tm*, %struct.tm** %10
%35 = getelementptr inbounds %struct.tm, %struct.tm* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.tm, %struct.tm* %7, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i8*, i8** %4
%39 = call i8* @strptime(i8* %38, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %7)
store i8* %39, i8** %8
br label %40
40: ; preds = %33, %30
br label %41
41: ; preds = %40, %27
br label %42
42: ; preds = %41, %21
%43 = load i8*, i8** %8
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fputs(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i32 %46)
store i32 1, i32* %3
br label %86
48: ; preds = %42
%49 = load i8*, i8** %8
%50 = getelementptr inbounds i8, i8* %49, i64 0
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = call i32 @fputs(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0), i32 %55)
store i32 1, i32* %3
br label %86
57: ; preds = %48
%58 = call i32 @mktime(%struct.tm* %7)
store i32 %58, i32* %12
%59 = load i32, i32* %12
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %62)
store i32 1, i32* %3
br label %86
64: ; preds = %57
%65 = call %struct.tm* @localtime(i32* %12)
store %struct.tm* %65, %struct.tm** %13
%66 = load %struct.tm*, %struct.tm** %13
%67 = call i32 @mktime(%struct.tm* %66)
store i32 %67, i32* %12
%68 = load i32, i32* %12
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load i32, i32* @stderr
%72 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %71)
store i32 1, i32* %3
br label %86
73: ; preds = %64
%74 = load i32, i32* %12
%75 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
store i32 %74, i32* %75
%76 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
store i64 0, i64* %76
%77 = load i32, i32* @CLOCK_REALTIME
%78 = call i32 @clock_settime(i32 %77, %struct.timespec* %11)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %85
80: ; preds = %73
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 0
%83 = load i8*, i8** %82
%84 = call i32 @perror(i8* %83)
store i32 1, i32* %3
br label %86
85: ; preds = %73
store i32 0, i32* %3
br label %86
86: ; preds = %85, %80, %70, %61, %54, %45
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.tm = type { i32 }
%struct.timespec = type { i32, i64 }
@.str = [11 x i8] c"%m%d%H%M%Y\00"
@.str.1 = [11 x i8] c"%m%d%H%M%y\00"
@.str.2 = [9 x i8] c"%m%d%H%M\00"
@.str.3 = [62 x i8] c"Invalid date string\0ASee 'date --help' for usage information.\0A\00"
@stderr = global i32 0
@.str.4 = [65 x i8] c"Somehow gave extra data to strptime()? (This shouldn't happen!)\0A\00"
@.str.5 = [25 x i8] c"Error calling mktime().\0A\00"
@.str.6 = [40 x i8] c"Error calling mktime(). (Second call.)\0A\00"
@CLOCK_REALTIME = global i32 0
define i32 @set_time(i8* %0, i8** nocapture %1) {
%3 = alloca %struct.tm
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca i32
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = bitcast %struct.tm* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = call i32 @time(i32* %4)
%11 = call %struct.tm* @localtime(i32* %4)
%12 = call i32 @memset(%struct.tm* %3, i32 0, i32 4)
switch i64 %7, label %22 [
i64 12, label %18
i64 10, label %13
i64 8, label %14
]
13: ; preds = %2
br label %18
14: ; preds = %2
%15 = getelementptr inbounds %struct.tm, %struct.tm* %11, i64 0, i32 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 0
store i32 %16, i32* %17
br label %18
18: ; preds = %2, %13, %14
%19 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %14 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %2 ]
%20 = call i8* @strptime(i8* %0, i8* %19, %struct.tm* %3)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %2, %18
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %57
25: ; preds = %18
%26 = load i8, i8* %20
%27 = icmp eq i8 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 @fputs(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %57
31: ; preds = %25
%32 = bitcast %struct.timespec* %5 to i8*
%33 = bitcast i32* %6 to i8*
%34 = call i32 @mktime(%struct.tm* %3)
store i32 %34, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %37)
br label %55
39: ; preds = %31
%40 = call %struct.tm* @localtime(i32* %6)
%41 = call i32 @mktime(%struct.tm* %40)
store i32 %41, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %44)
br label %55
46: ; preds = %39
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 %41, i32* %47
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i64 0, i64* %48
%49 = load i32, i32* @CLOCK_REALTIME
%50 = call i32 @clock_settime(i32 %49, %struct.timespec* %5)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %46
%53 = load i8*, i8** %1
%54 = call i32 @perror(i8* %53)
br label %55
55: ; preds = %43, %52, %46, %36
%56 = phi i32 [ 1, %36 ], [ 1, %43 ], [ 1, %52 ], [ 0, %46 ]
br label %57
57: ; preds = %55, %28, %22
%58 = phi i32 [ 1, %22 ], [ 1, %28 ], [ %56, %55 ]
ret i32 %58
}