#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 dummy;
};
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
struct timespec *calloc(int, int);
char *getenv(char *);
int /*<<< orphan*/ mktime(struct tm *);
int ptime(char *, struct tm *);
int /*<<< orphan*/ setenv(char *, char *, int);
char *strchr(char *, char);
size_t strlen(char *);
char *strrchr(char *, char);
int strtoll(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ tzset();
int /*<<< orphan*/ unsetenv(char *);
struct timespec *flu_parse_tstamp(char *s, int utc) {
  struct tm tm = {};
  char *subseconds = NULL;
  if (strchr(s, '-')) {
    utc = 1;
  } else {
    char *a = strchr(s, '.');
    char *b = strrchr(s, '.');
    if (a == NULL)
      return NULL;
    if (a != b)
      subseconds = b + 1;
  }
  int r = ptime(s, &tm);
  if (r != 0)
    return NULL;
  char *tz = NULL;
  if (utc) {
    tz = getenv("TZ");
    setenv("TZ", "UTC", 1);
    tzset();
  }
  time_t t = mktime(&tm);
  if (utc) {
    if (!tz)
      unsetenv("TZ");
    else
      setenv("TZ", tz, 1);
    tzset();
  }
  struct timespec *ts = calloc(1, sizeof(struct timespec));
  ts->tv_sec = t;
  ts->tv_nsec = 0;
  if (subseconds) {
    size_t st = strlen(subseconds);
    ts->tv_nsec = strtoll(subseconds, NULL, 10);
    if (st == 3)
      ts->tv_nsec = ts->tv_nsec * 1000 * 1000;
    else if (st == 6)
      ts->tv_nsec = ts->tv_nsec * 1000;
  }
  return ts;
}


x86 assembly (clang, -O0):
<flu_parse_tstamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     120b <flu_parse_tstamp+0x4b>
movl   $0x1,-0x14(%rbp)
jmp    1266 <flu_parse_tstamp+0xa6>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1247 <flu_parse_tstamp+0x87>
movq   $0x0,-0x8(%rbp)
jmp    13c8 <flu_parse_tstamp+0x208>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1261 <flu_parse_tstamp+0xa1>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1266 <flu_parse_tstamp+0xa6>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10c0 <ptime@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     128d <flu_parse_tstamp+0xcd>
movq   $0x0,-0x8(%rbp)
jmp    13c8 <flu_parse_tstamp+0x208>
movq   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12ce <flu_parse_tstamp+0x10e>
lea    0xd5a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
lea    0xd4a(%rip),%rdi
lea    0xd46(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   10b0 <tzset@plt>
lea    -0x18(%rbp),%rdi
call   10d0 <mktime@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     131c <flu_parse_tstamp+0x15c>
cmpq   $0x0,-0x40(%rbp)
jne    1300 <flu_parse_tstamp+0x140>
lea    0xd0a(%rip),%rdi
call   10e0 <unsetenv@plt>
jmp    1315 <flu_parse_tstamp+0x155>
mov    -0x40(%rbp),%rsi
lea    0xcf5(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   10b0 <tzset@plt>
mov    $0x1,%edi
mov    $0x8,%esi
call   1090 <calloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x44(%rbp),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x50(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     13c0 <flu_parse_tstamp+0x200>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10a0 <strtoll@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x3,-0x58(%rbp)
jne    139b <flu_parse_tstamp+0x1db>
mov    -0x50(%rbp),%rax
imul   $0x3e8,(%rax),%eax
imul   $0x3e8,%eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
jmp    13bb <flu_parse_tstamp+0x1fb>
cmpq   $0x6,-0x58(%rbp)
jne    13b6 <flu_parse_tstamp+0x1f6>
mov    -0x50(%rbp),%rax
imul   $0x3e8,(%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
jmp    13bb <flu_parse_tstamp+0x1fb>
jmp    13c0 <flu_parse_tstamp+0x200>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flu_parse_tstamp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%rbp
movl   $0x0,0x8(%rsp)
mov    $0x2d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1247 <flu_parse_tstamp+0x97>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10b0 <ptime@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    130b <flu_parse_tstamp+0x15b>
xor    %r14d,%r14d
lea    0xe06(%rip),%rbp
mov    %rbp,%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xdf7(%rip),%rsi
mov    %rbp,%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
xor    %eax,%eax
call   10a0 <tzset@plt>
lea    0x8(%rsp),%rdi
call   10c0 <mktime@plt>
mov    %eax,%ebp
lea    0xdcd(%rip),%rdi
test   %rbx,%rbx
je     128f <flu_parse_tstamp+0xdf>
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
jmp    1294 <flu_parse_tstamp+0xe4>
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
mov    %rax,%r12
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%r14
xor    %ebx,%ebx
cmp    %rax,%r12
cmove  %rbx,%r14
test   %r12,%r12
je     130b <flu_parse_tstamp+0x15b>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10b0 <ptime@plt>
test   %eax,%eax
je     12ea <flu_parse_tstamp+0x13a>
xor    %ebx,%ebx
jmp    130b <flu_parse_tstamp+0x15b>
call   10d0 <unsetenv@plt>
xor    %eax,%eax
call   10a0 <tzset@plt>
mov    $0x1,%edi
mov    $0x8,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %ebp,0x4(%rax)
movl   $0x0,(%rax)
test   %r14,%r14
je     130b <flu_parse_tstamp+0x15b>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtoll@plt>
mov    %eax,(%rbx)
cmp    $0x3,%rbp
je     1301 <flu_parse_tstamp+0x151>
cmp    $0x6,%rbp
jne    130b <flu_parse_tstamp+0x15b>
mov    $0x3e8,%ecx
jmp    1306 <flu_parse_tstamp+0x156>
test   %r15d,%r15d
jne    11f3 <flu_parse_tstamp+0x43>
lea    0x8(%rsp),%rdi
call   10c0 <mktime@plt>
mov    %eax,%ebp
jmp    129b <flu_parse_tstamp+0xeb>
mov    $0xf4240,%ecx
imul   %ecx,%eax
mov    %eax,(%rbx)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [3 x i8] c"TZ\00"
@.str.1 = [4 x i8] c"UTC\00"
define %struct.timespec* @flu_parse_tstamp(i8* %0, i32 %1) {
%3 = alloca %struct.timespec*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.tm
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca %struct.timespec*
%14 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = bitcast %struct.tm* %6 to i8*
store i8* null, i8** %7
%16 = load i8*, i8** %4
%17 = call i8* @strchr(i8* %16, i8 signext 45)
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 1, i32* %5
br label %36
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i8* @strchr(i8* %21, i8 signext 46)
store i8* %22, i8** %8
%23 = load i8*, i8** %4
%24 = call i8* @strrchr(i8* %23, i8 signext 46)
store i8* %24, i8** %9
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %20
store %struct.timespec* null, %struct.timespec** %3
br label %103
28: ; preds = %20
%29 = load i8*, i8** %8
%30 = load i8*, i8** %9
%31 = icmp ne i8* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %9
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8* %34, i8** %7
br label %35
35: ; preds = %32, %28
br label %36
36: ; preds = %35, %19
%37 = load i8*, i8** %4
%38 = call i32 @ptime(i8* %37, %struct.tm* %6)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
store %struct.timespec* null, %struct.timespec** %3
br label %103
42: ; preds = %36
store i8* null, i8** %11
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = call i8* @getenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %46, i8** %11
%47 = call i32 @setenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 1)
%48 = call i32 (...) @tzset()
br label %49
49: ; preds = %45, %42
%50 = call i32 @mktime(%struct.tm* %6)
store i32 %50, i32* %12
%51 = load i32, i32* %5
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %63
53: ; preds = %49
%54 = load i8*, i8** %11
%55 = icmp ne i8* %54, null
br i1 %55, label %58, label %56
56: ; preds = %53
%57 = call i32 @unsetenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %61
58: ; preds = %53
%59 = load i8*, i8** %11
%60 = call i32 @setenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %59, i32 1)
br label %61
61: ; preds = %58, %56
%62 = call i32 (...) @tzset()
br label %63
63: ; preds = %61, %49
%64 = call %struct.timespec* @calloc(i32 1, i32 8)
store %struct.timespec* %64, %struct.timespec** %13
%65 = load i32, i32* %12
%66 = load %struct.timespec*, %struct.timespec** %13
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %66, i32 0, i32 1
store i32 %65, i32* %67
%68 = load %struct.timespec*, %struct.timespec** %13
%69 = getelementptr inbounds %struct.timespec, %struct.timespec* %68, i32 0, i32 0
store i32 0, i32* %69
%70 = load i8*, i8** %7
%71 = icmp ne i8* %70, null
br i1 %71, label %72, label %101
72: ; preds = %63
%73 = load i8*, i8** %7
%74 = call i64 @strlen(i8* %73)
store i64 %74, i64* %14
%75 = load i8*, i8** %7
%76 = call i32 @strtoll(i8* %75, i32* null, i32 10)
%77 = load %struct.timespec*, %struct.timespec** %13
%78 = getelementptr inbounds %struct.timespec, %struct.timespec* %77, i32 0, i32 0
store i32 %76, i32* %78
%79 = load i64, i64* %14
%80 = icmp eq i64 %79, 3
br i1 %80, label %81, label %89
81: ; preds = %72
%82 = load %struct.timespec*, %struct.timespec** %13
%83 = getelementptr inbounds %struct.timespec, %struct.timespec* %82, i32 0, i32 0
%84 = load i32, i32* %83
%85 = mul nsw i32 %84, 1000
%86 = mul nsw i32 %85, 1000
%87 = load %struct.timespec*, %struct.timespec** %13
%88 = getelementptr inbounds %struct.timespec, %struct.timespec* %87, i32 0, i32 0
store i32 %86, i32* %88
br label %100
89: ; preds = %72
%90 = load i64, i64* %14
%91 = icmp eq i64 %90, 6
br i1 %91, label %92, label %99
92: ; preds = %89
%93 = load %struct.timespec*, %struct.timespec** %13
%94 = getelementptr inbounds %struct.timespec, %struct.timespec* %93, i32 0, i32 0
%95 = load i32, i32* %94
%96 = mul nsw i32 %95, 1000
%97 = load %struct.timespec*, %struct.timespec** %13
%98 = getelementptr inbounds %struct.timespec, %struct.timespec* %97, i32 0, i32 0
store i32 %96, i32* %98
br label %99
99: ; preds = %92, %89
br label %100
100: ; preds = %99, %81
br label %101
101: ; preds = %100, %63
%102 = load %struct.timespec*, %struct.timespec** %13
store %struct.timespec* %102, %struct.timespec** %3
br label %103
103: ; preds = %101, %41, %27
%104 = load %struct.timespec*, %struct.timespec** %3
ret %struct.timespec* %104
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [3 x i8] c"TZ\00"
@.str.1 = [4 x i8] c"UTC\00"
define noalias %struct.timespec* @flu_parse_tstamp(i8* %0, i32 %1) {
%3 = alloca %struct.tm
%4 = bitcast %struct.tm* %3 to i8*
%5 = getelementptr inbounds %struct.tm, %struct.tm* %3, i64 0, i32 0
store i32 0, i32* %5
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 45)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %18
8: ; preds = %2
%9 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 46)
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%11 = icmp eq i8* %9, null
%12 = icmp eq i8* %9, %10
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = select i1 %12, i8* null, i8* %13
br i1 %11, label %52, label %15
15: ; preds = %8
%16 = call i32 @ptime(i8* %0, %struct.tm* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %52
18: ; preds = %2
%19 = call i32 @ptime(i8* %0, %struct.tm* %3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %52
21: ; preds = %15
%22 = icmp eq i32 %1, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = call i32 @mktime(%struct.tm* %3)
br label %38
25: ; preds = %18, %21
%26 = phi i8* [ %14, %21 ], [ null, %18 ]
%27 = call i8* @getenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @setenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 1)
%29 = call i32 (...) @tzset()
%30 = call i32 @mktime(%struct.tm* %3)
%31 = icmp eq i8* %27, null
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @unsetenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %36
34: ; preds = %25
%35 = call i32 @setenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %27, i32 1)
br label %36
36: ; preds = %34, %32
%37 = call i32 (...) @tzset()
br label %38
38: ; preds = %23, %36
%39 = phi i32 [ %24, %23 ], [ %30, %36 ]
%40 = phi i8* [ %14, %23 ], [ %26, %36 ]
%41 = call %struct.timespec* @calloc(i32 1, i32 8)
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %41, i64 0, i32 1
store i32 %39, i32* %42
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %41, i64 0, i32 0
store i32 0, i32* %43
%44 = icmp eq i8* %40, null
br i1 %44, label %52, label %45
45: ; preds = %38
%46 = call i64 @strlen(i8* %40)
%47 = call i32 @strtoll(i8* nocapture %40, i32* null, i32 10)
store i32 %47, i32* %43
switch i64 %46, label %52 [
i64 3, label %49
i64 6, label %48
]
48: ; preds = %45
br label %49
49: ; preds = %45, %48
%50 = phi i32 [ 1000, %48 ], [ 1000000, %45 ]
%51 = mul i32 %47, %50
store i32 %51, i32* %43
br label %52
52: ; preds = %49, %18, %15, %45, %38, %8
%53 = phi %struct.timespec* [ null, %8 ], [ null, %15 ], [ %41, %45 ], [ %41, %38 ], [ null, %18 ], [ %41, %49 ]
ret %struct.timespec* %53
}