#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;
};
struct iovec {
  char *iov_base;
  int iov_len;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ perror(char *);
int sprintf(char *, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int writev(int, struct iovec *, int);
int result(int out, int size, int bufsize, char *buffer) {
  struct timespec cur_time;
  struct iovec iov[3];
  int err = 0;
  char mtime[40];
  int added = 0;
  clock_gettime(CLOCK_REALTIME, &cur_time);
  added =
      sprintf(mtime, "Time: %ld.%09ld\n", cur_time.tv_sec, cur_time.tv_nsec);
  iov[0].iov_base = mtime;
  iov[0].iov_len = added;
  iov[1].iov_base = buffer;
  iov[1].iov_len = size;
  iov[2].iov_base = "---\n";
  iov[2].iov_len = sizeof("---\n") - 1;
  if (bufsize - size > 40) {
    if ((err = writev(out, iov, 3) == -1)) {
      perror("Write failed - out of disk?");
    }
  } else {
    write(2, "Buffer Overrun\n", sizeof("Buffer Overrun\n"));
  }
  return err;
}


x86 assembly (clang, -O0):
<result>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x84(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <clock_gettime@plt>
lea    -0x80(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe64(%rip),%rsi
call   1070 <sprintf@plt>
mov    %eax,-0x84(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x84(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    0xe44(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x28(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
cmp    $0x28,%eax
jle    1227 <result+0xd7>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
sete   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x54(%rbp)
test   $0x1,%al
jne    1216 <result+0xc6>
jmp    1222 <result+0xd2>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
jmp    123d <result+0xed>
mov    $0x2,%edi
lea    0xdff(%rip),%rsi
mov    $0x10,%edx
call   1050 <write@plt>
mov    -0x54(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<result>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %rcx,%r15
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
lea    0xe78(%rip),%rsi
lea    0x40(%rsp),%r12
mov    %r12,%rdi
call   1070 <sprintf@plt>
mov    %r12,0x10(%rsp)
mov    %eax,0x18(%rsp)
mov    %r15,0x20(%rsp)
mov    %ebx,0x28(%rsp)
lea    0xe63(%rip),%rax
mov    %rax,0x30(%rsp)
movl   $0x4,0x38(%rsp)
sub    %ebx,%ebp
cmp    $0x29,%ebp
jl     11f0 <result+0xa0>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1208 <result+0xb8>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    1208 <result+0xb8>
lea    0xe3b(%rip),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1050 <write@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.iovec = type { i8*, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [17 x i8] c"Time: %ld.%09ld\0A\00"
@.str.1 = [5 x i8] c"---\0A\00"
@.str.2 = [28 x i8] c"Write failed - out of disk?\00"
@.str.3 = [16 x i8] c"Buffer Overrun\0A\00"
define i32 @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 [3 x %struct.iovec]
%11 = alloca i32
%12 = alloca [40 x i8]
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
store i32 0, i32* %11
store i32 0, i32* %13
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %9)
%16 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20)
store i32 %21, i32* %13
%22 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%23 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 0
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %23, i32 0, i32 0
store i8* %22, i8** %24
%25 = load i32, i32* %13
%26 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 0
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i32 0, i32 1
store i32 %25, i32* %27
%28 = load i8*, i8** %8
%29 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 1
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 0
store i8* %28, i8** %30
%31 = load i32, i32* %6
%32 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 1
%33 = getelementptr inbounds %struct.iovec, %struct.iovec* %32, i32 0, i32 1
store i32 %31, i32* %33
%34 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 2
%35 = getelementptr inbounds %struct.iovec, %struct.iovec* %34, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %35
%36 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 2
%37 = getelementptr inbounds %struct.iovec, %struct.iovec* %36, i32 0, i32 1
store i32 4, i32* %37
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = sub nsw i32 %38, %39
%41 = icmp sgt i32 %40, 40
br i1 %41, label %42, label %51
42: ; preds = %4
%43 = load i32, i32* %5
%44 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %10, i64 0, i64 0
%45 = call i32 @writev(i32 %43, %struct.iovec* %44, i32 3)
%46 = icmp eq i32 %45, -1
%47 = zext i1 %46 to i32
store i32 %47, i32* %11
br i1 %46, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
br label %53
51: ; preds = %4
%52 = call i32 @write(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 16)
br label %53
53: ; preds = %51, %50
%54 = load i32, i32* %11
ret i32 %54
}

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