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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int sprintf(char *, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void result(int out, int size, int bufsize, char *buffer) {
  struct timespec cur_time;
  if (bufsize - size > 40) {
    clock_gettime(CLOCK_REALTIME, &cur_time);
    int added = sprintf(&buffer[size], "Time: %ld.%09ld\n---\n",
                        cur_time.tv_sec, cur_time.tv_nsec);
    write(out, buffer, size + added);
  } else {
    write(2, "Buffer Overrun\n", sizeof("Buffer Overrun\n"));
  }
}


x86 assembly (clang, -O0):
<result>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
cmp    $0x28,%eax
jle    119d <result+0x6d>
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x18(%rbp),%rdi
movslq -0x8(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe82(%rip),%rsi
call   1050 <sprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
add    -0x24(%rbp),%edx
call   1040 <write@plt>
jmp    11b3 <result+0x83>
mov    $0x2,%edi
lea    0xe6c(%rip),%rsi
mov    $0x10,%edx
call   1040 <write@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<result>:
sub    %esi,%edx
cmp    $0x29,%edx
jl     118c <result+0x5c>
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %esi,%ebp
mov    %edi,%r14d
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
movslq %ebp,%rdi
add    %rbx,%rdi
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
lea    0xe91(%rip),%rsi
call   1050 <sprintf@plt>
add    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe82(%rip),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
jmp    1040 <write@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [21 x i8] c"Time: %ld.%09ld\0A---\0A\00"
@.str.1 = [16 x i8] c"Buffer Overrun\0A\00"
define void @result(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.timespec
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = sub nsw i32 %11, %12
%14 = icmp sgt i32 %13, 40
br i1 %14, label %15, label %33
15: ; preds = %4
%16 = load i32, i32* @CLOCK_REALTIME
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %9)
%18 = load i8*, i8** %8
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %25)
store i32 %26, i32* %10
%27 = load i32, i32* %5
%28 = load i8*, i8** %8
%29 = load i32, i32* %6
%30 = load i32, i32* %10
%31 = add nsw i32 %29, %30
%32 = call i32 @write(i32 %27, i8* %28, i32 %31)
br label %35
33: ; preds = %4
%34 = call i32 @write(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 16)
br label %35
35: ; preds = %33, %15
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [21 x i8] c"Time: %ld.%09ld\0A---\0A\00"
@.str.1 = [16 x i8] c"Buffer Overrun\0A\00"
define void @result(i32 %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = alloca %struct.timespec
%6 = bitcast %struct.timespec* %5 to i8*
%7 = sub nsw i32 %2, %1
%8 = icmp sgt i32 %7, 40
br i1 %8, label %9, label %21
9: ; preds = %4
%10 = load i32, i32* @CLOCK_REALTIME
%11 = call i32 @clock_gettime(i32 %10, %struct.timespec* %5)
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i8, i8* %3, i64 %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17)
%19 = add nsw i32 %18, %1
%20 = call i32 @write(i32 %0, i8* %3, i32 %19)
br label %23
21: ; preds = %4
%22 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 16)
br label %23
23: ; preds = %21, %9
ret void
}