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

/* Variables and functions */
int /*<<< orphan*/ *localtime_r(int /*<<< orphan*/ *, struct tm *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int /*<<< orphan*/);
size_t strftime(char *, size_t, char *, struct tm *);
size_t strlen(char *);
int /*<<< orphan*/ tzset();
char *timespec2str(struct timespec *ts, char *buf, size_t len) {
  size_t ret, pos;
  struct tm t;
  memset(buf, 0, len);
  tzset();
  if (localtime_r(&(ts->tv_sec), &t) == NULL)
    return buf;
  ret = strftime(buf, len, "%F %T", &t);
  if (ret == 0)
    return buf;
  len -= ret - 1;
  pos = strlen(buf);
  len -= pos;
  snprintf(&buf[pos], len, ".%09ld", ts->tv_nsec);
  return buf;
}


x86 assembly (clang, -O0):
<timespec2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    $0x0,%al
call   1070 <tzset@plt>
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
lea    -0x38(%rbp),%rsi
call   1030 <localtime_r@plt>
cmp    $0x0,%rax
jne    11b2 <timespec2str+0x52>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123b <timespec2str+0xdb>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xe3f(%rip),%rdx
lea    -0x38(%rbp),%rcx
call   1080 <strftime@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e6 <timespec2str+0x86>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    123b <timespec2str+0xdb>
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
lea    0xdd8(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timespec2str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
xor    %eax,%eax
call   1070 <tzset@plt>
lea    0x4(%r15),%rdi
mov    %rsp,%rsi
call   1030 <localtime_r@plt>
test   %rax,%rax
je     11d8 <timespec2str+0x78>
lea    0xe66(%rip),%rdx
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <strftime@plt>
test   %rax,%rax
je     11d8 <timespec2str+0x78>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rax,%rbx
sub    %rbx,%r12
add    $0x1,%r12
lea    (%rax,%r14,1),%rdi
mov    (%r15),%ecx
lea    0xe36(%rip),%rdx
mov    %r12,%rsi
call   1050 <snprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [6 x i8] c"%F %T\00"
@.str.1 = [7 x i8] c".%09ld\00"
define i8* @timespec2str(%struct.timespec* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.timespec*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca %struct.tm
store %struct.timespec* %0, %struct.timespec** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = call i32 @memset(i8* %11, i32 0, i64 %12)
%14 = call i32 (...) @tzset()
%15 = load %struct.timespec*, %struct.timespec** %5
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %15, i32 0, i32 1
%17 = call i32* @localtime_r(i32* %16, %struct.tm* %10)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i8*, i8** %6
store i8* %20, i8** %4
br label %48
21: ; preds = %3
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i64 @strftime(i8* %22, i64 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.tm* %10)
store i64 %24, i64* %8
%25 = load i64, i64* %8
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i8*, i8** %6
store i8* %28, i8** %4
br label %48
29: ; preds = %21
%30 = load i64, i64* %8
%31 = sub i64 %30, 1
%32 = load i64, i64* %7
%33 = sub i64 %32, %31
store i64 %33, i64* %7
%34 = load i8*, i8** %6
%35 = call i64 @strlen(i8* %34)
store i64 %35, i64* %9
%36 = load i64, i64* %9
%37 = load i64, i64* %7
%38 = sub i64 %37, %36
store i64 %38, i64* %7
%39 = load i8*, i8** %6
%40 = load i64, i64* %9
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i64, i64* %7
%43 = load %struct.timespec*, %struct.timespec** %5
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @snprintf(i8* %41, i64 %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = load i8*, i8** %6
store i8* %47, i8** %4
br label %48
48: ; preds = %29, %27, %19
%49 = load i8*, i8** %4
ret i8* %49
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.tm = type { i32 }
@.str = [6 x i8] c"%F %T\00"
@.str.1 = [7 x i8] c".%09ld\00"
define i8* @timespec2str(%struct.timespec* %0, i8* returned %1, i64 %2) {
%4 = alloca %struct.tm
%5 = bitcast %struct.tm* %4 to i8*
%6 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%7 = tail call i32 (...) @tzset()
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%9 = call i32* @localtime_r(i32* %8, %struct.tm* %4)
%10 = icmp eq i32* %9, null
br i1 %10, label %23, label %11
11: ; preds = %3
%12 = call i64 @strftime(i8* %1, i64 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.tm* %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = call i64 @strlen(i8* dereferenceable(1) %1)
%16 = add i64 %2, 1
%17 = add i64 %12, %15
%18 = sub i64 %16, %17
%19 = getelementptr inbounds i8, i8* %1, i64 %15
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @snprintf(i8* %19, i64 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %11, %3, %14
ret i8* %1
}