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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/, long);
char *nanotime() {
  time_t sec;
  long nsec;
  struct timespec tp;
  char *out = (char *)malloc(20 * sizeof(char));
  if (clock_gettime(CLOCK_REALTIME, &tp)) {
    return NULL;
  }
  sec = tp.tv_sec;
  nsec = tp.tv_nsec;
  sprintf(out, "%ld%09ld", sec, nsec);
  return out;
}


x86 assembly (clang, -O0):
<nanotime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x14,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     116f <nanotime+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    119c <nanotime+0x6c>
mov    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
lea    0xe71(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nanotime>:
push   %rbx
sub    $0x10,%rsp
mov    $0x14,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     115c <nanotime+0x2c>
xor    %ebx,%ebx
jmp    1173 <nanotime+0x43>
mov    0x8(%rsp),%edx
mov    (%rsp),%rcx
lea    0xe95(%rip),%rsi
mov    %rbx,%rdi
call   1050 <sprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [9 x i8] c"%ld%09ld\00"
define i8* @nanotime() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.timespec
%5 = alloca i8*
%6 = call i64 @malloc(i32 20)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %5
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i64 @clock_gettime(i32 %8, %struct.timespec* %4)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i8* null, i8** %1
br label %22
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%14 = load i32, i32* %13
store i32 %14, i32* %2
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%16 = load i64, i64* %15
store i64 %16, i64* %3
%17 = load i8*, i8** %5
%18 = load i32, i32* %2
%19 = load i64, i64* %3
%20 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18, i64 %19)
%21 = load i8*, i8** %5
store i8* %21, i8** %1
br label %22
22: ; preds = %12, %11
%23 = load i8*, i8** %1
ret i8* %23
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [9 x i8] c"%ld%09ld\00"
define i8* @nanotime() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = tail call i64 @malloc(i32 20)
%4 = inttoptr i64 %3 to i8*
%5 = load i32, i32* @CLOCK_REALTIME
%6 = call i64 @clock_gettime(i32 %5, %struct.timespec* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%12 = load i64, i64* %11
%13 = call i32 @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10, i64 %12)
br label %14
14: ; preds = %0, %8
%15 = phi i8* [ %4, %8 ], [ null, %0 ]
ret i8* %15
}