#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)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     11eb <flu_parse_tstamp+0x3b>
movl   $0x1,-0x14(%rbp)
jmp    1246 <flu_parse_tstamp+0x96>
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    1227 <flu_parse_tstamp+0x77>
movq   $0x0,-0x8(%rbp)
jmp    13a8 <flu_parse_tstamp+0x1f8>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1241 <flu_parse_tstamp+0x91>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1246 <flu_parse_tstamp+0x96>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <ptime@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     126d <flu_parse_tstamp+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    13a8 <flu_parse_tstamp+0x1f8>
movq   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12ae <flu_parse_tstamp+0xfe>
lea    0xd7a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
lea    0xd6a(%rip),%rdi
lea    0xd66(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   10a0 <tzset@plt>
lea    -0x18(%rbp),%rdi
call   10c0 <mktime@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12fc <flu_parse_tstamp+0x14c>
cmpq   $0x0,-0x40(%rbp)
jne    12e0 <flu_parse_tstamp+0x130>
lea    0xd2a(%rip),%rdi
call   10d0 <unsetenv@plt>
jmp    12f5 <flu_parse_tstamp+0x145>
mov    -0x40(%rbp),%rsi
lea    0xd15(%rip),%rdi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    $0x0,%al
call   10a0 <tzset@plt>
mov    $0x1,%edi
mov    $0x8,%esi
call   1080 <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     13a0 <flu_parse_tstamp+0x1f0>
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   1090 <strtoll@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x3,-0x58(%rbp)
jne    137b <flu_parse_tstamp+0x1cb>
mov    -0x50(%rbp),%rax
imul   $0x3e8,(%rax),%eax
imul   $0x3e8,%eax,%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
jmp    139b <flu_parse_tstamp+0x1eb>
cmpq   $0x6,-0x58(%rbp)
jne    1396 <flu_parse_tstamp+0x1e6>
mov    -0x50(%rbp),%rax
imul   $0x3e8,(%rax),%ecx
mov    -0x50(%rbp),%rax
mov    %ecx,(%rax)
jmp    139b <flu_parse_tstamp+0x1eb>
jmp    13a0 <flu_parse_tstamp+0x1f0>
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
mov    $0x2d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     123f <flu_parse_tstamp+0x8f>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10b0 <ptime@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    1303 <flu_parse_tstamp+0x153>
xor    %r14d,%r14d
lea    0xe0e(%rip),%rbp
mov    %rbp,%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xdff(%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    0xdd5(%rip),%rdi
test   %rbx,%rbx
je     1287 <flu_parse_tstamp+0xd7>
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
jmp    128c <flu_parse_tstamp+0xdc>
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     1303 <flu_parse_tstamp+0x153>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   10b0 <ptime@plt>
test   %eax,%eax
je     12e2 <flu_parse_tstamp+0x132>
xor    %ebx,%ebx
jmp    1303 <flu_parse_tstamp+0x153>
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     1303 <flu_parse_tstamp+0x153>
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     12f9 <flu_parse_tstamp+0x149>
cmp    $0x6,%rbp
jne    1303 <flu_parse_tstamp+0x153>
mov    $0x3e8,%ecx
jmp    12fe <flu_parse_tstamp+0x14e>
test   %r15d,%r15d
jne    11eb <flu_parse_tstamp+0x3b>
lea    0x8(%rsp),%rdi
call   10c0 <mktime@plt>
mov    %eax,%ebp
jmp    1293 <flu_parse_tstamp+0xe3>
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
store i8* null, i8** %7
%15 = load i8*, i8** %4
%16 = call i8* @strchr(i8* %15, i8 signext 45)
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 1, i32* %5
br label %35
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = call i8* @strchr(i8* %20, i8 signext 46)
store i8* %21, i8** %8
%22 = load i8*, i8** %4
%23 = call i8* @strrchr(i8* %22, i8 signext 46)
store i8* %23, i8** %9
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %19
store %struct.timespec* null, %struct.timespec** %3
br label %102
27: ; preds = %19
%28 = load i8*, i8** %8
%29 = load i8*, i8** %9
%30 = icmp ne i8* %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i8*, i8** %9
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %7
br label %34
34: ; preds = %31, %27
br label %35
35: ; preds = %34, %18
%36 = load i8*, i8** %4
%37 = call i32 @ptime(i8* %36, %struct.tm* %6)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
store %struct.timespec* null, %struct.timespec** %3
br label %102
41: ; preds = %35
store i8* null, i8** %11
%42 = load i32, i32* %5
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = call i8* @getenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %45, i8** %11
%46 = 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)
%47 = call i32 (...) @tzset()
br label %48
48: ; preds = %44, %41
%49 = call i32 @mktime(%struct.tm* %6)
store i32 %49, i32* %12
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %62
52: ; preds = %48
%53 = load i8*, i8** %11
%54 = icmp ne i8* %53, null
br i1 %54, label %57, label %55
55: ; preds = %52
%56 = call i32 @unsetenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %60
57: ; preds = %52
%58 = load i8*, i8** %11
%59 = call i32 @setenv(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %58, i32 1)
br label %60
60: ; preds = %57, %55
%61 = call i32 (...) @tzset()
br label %62
62: ; preds = %60, %48
%63 = call %struct.timespec* @calloc(i32 1, i32 8)
store %struct.timespec* %63, %struct.timespec** %13
%64 = load i32, i32* %12
%65 = load %struct.timespec*, %struct.timespec** %13
%66 = getelementptr inbounds %struct.timespec, %struct.timespec* %65, i32 0, i32 1
store i32 %64, i32* %66
%67 = load %struct.timespec*, %struct.timespec** %13
%68 = getelementptr inbounds %struct.timespec, %struct.timespec* %67, i32 0, i32 0
store i32 0, i32* %68
%69 = load i8*, i8** %7
%70 = icmp ne i8* %69, null
br i1 %70, label %71, label %100
71: ; preds = %62
%72 = load i8*, i8** %7
%73 = call i64 @strlen(i8* %72)
store i64 %73, i64* %14
%74 = load i8*, i8** %7
%75 = call i32 @strtoll(i8* %74, i32* null, i32 10)
%76 = load %struct.timespec*, %struct.timespec** %13
%77 = getelementptr inbounds %struct.timespec, %struct.timespec* %76, i32 0, i32 0
store i32 %75, i32* %77
%78 = load i64, i64* %14
%79 = icmp eq i64 %78, 3
br i1 %79, label %80, label %88
80: ; preds = %71
%81 = load %struct.timespec*, %struct.timespec** %13
%82 = getelementptr inbounds %struct.timespec, %struct.timespec* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = mul nsw i32 %83, 1000
%85 = mul nsw i32 %84, 1000
%86 = load %struct.timespec*, %struct.timespec** %13
%87 = getelementptr inbounds %struct.timespec, %struct.timespec* %86, i32 0, i32 0
store i32 %85, i32* %87
br label %99
88: ; preds = %71
%89 = load i64, i64* %14
%90 = icmp eq i64 %89, 6
br i1 %90, label %91, label %98
91: ; preds = %88
%92 = load %struct.timespec*, %struct.timespec** %13
%93 = getelementptr inbounds %struct.timespec, %struct.timespec* %92, i32 0, i32 0
%94 = load i32, i32* %93
%95 = mul nsw i32 %94, 1000
%96 = load %struct.timespec*, %struct.timespec** %13
%97 = getelementptr inbounds %struct.timespec, %struct.timespec* %96, i32 0, i32 0
store i32 %95, i32* %97
br label %98
98: ; preds = %91, %88
br label %99
99: ; preds = %98, %80
br label %100
100: ; preds = %99, %62
%101 = load %struct.timespec*, %struct.timespec** %13
store %struct.timespec* %101, %struct.timespec** %3
br label %102
102: ; preds = %100, %40, %26
%103 = load %struct.timespec*, %struct.timespec** %3
ret %struct.timespec* %103
}

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