#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 vmbuf {
  int dummy;
};
struct tm {
  int dummy;
};

/* Variables and functions */
size_t strftime(int /*<<< orphan*/, size_t, char const *, struct tm const *);
scalar_t__ vmbuf_resize_by(struct vmbuf *, int);
size_t vmbuf_wavail(struct vmbuf *);
int /*<<< orphan*/ vmbuf_wloc(struct vmbuf *);
int /*<<< orphan*/ vmbuf_wseek(struct vmbuf *, size_t);
int vmbuf_strftime(struct vmbuf *vmbuf, const char *format,
                   const struct tm *tm) {
  size_t n;
  for (;;) {
    size_t wav = vmbuf_wavail(vmbuf);
    n = strftime(vmbuf_wloc(vmbuf), wav, format, tm);
    if (n > 0)
      break;
    if (0 > vmbuf_resize_by(vmbuf, 4096))
      return -1;
  }
  vmbuf_wseek(vmbuf, n);
  return 0;
}


x86 assembly (clang, -O0):
<vmbuf_strftime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <vmbuf_wavail@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <vmbuf_wloc@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <strftime@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11a1 <vmbuf_strftime+0x51>
jmp    11ce <vmbuf_strftime+0x7e>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <vmbuf_resize_by@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11c9 <vmbuf_strftime+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <vmbuf_strftime+0x92>
jmp    1164 <vmbuf_strftime+0x14>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <vmbuf_wseek@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmbuf_strftime>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <vmbuf_wavail@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <vmbuf_wloc@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <strftime@plt>
test   %rax,%rax
jne    11b1 <vmbuf_strftime+0x61>
mov    %r12,%rdi
mov    $0x1000,%esi
call   1060 <vmbuf_resize_by@plt>
test   %rax,%rax
jns    1170 <vmbuf_strftime+0x20>
mov    $0xffffffff,%eax
jmp    11be <vmbuf_strftime+0x6e>
mov    %r12,%rdi
mov    %rax,%rsi
call   1070 <vmbuf_wseek@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.vmbuf = type { i32 }
%struct.tm = type { i32 }
define i32 @vmbuf_strftime(%struct.vmbuf* %0, i8* %1, %struct.tm* %2) {
%4 = alloca i32
%5 = alloca %struct.vmbuf*
%6 = alloca i8*
%7 = alloca %struct.tm*
%8 = alloca i64
%9 = alloca i64
store %struct.vmbuf* %0, %struct.vmbuf** %5
store i8* %1, i8** %6
store %struct.tm* %2, %struct.tm** %7
br label %10
10: ; preds = %27, %3
%11 = load %struct.vmbuf*, %struct.vmbuf** %5
%12 = call i64 @vmbuf_wavail(%struct.vmbuf* %11)
store i64 %12, i64* %9
%13 = load %struct.vmbuf*, %struct.vmbuf** %5
%14 = call i32 @vmbuf_wloc(%struct.vmbuf* %13)
%15 = load i64, i64* %9
%16 = load i8*, i8** %6
%17 = load %struct.tm*, %struct.tm** %7
%18 = call i64 @strftime(i32 %14, i64 %15, i8* %16, %struct.tm* %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %10
br label %28
22: ; preds = %10
%23 = load %struct.vmbuf*, %struct.vmbuf** %5
%24 = call i64 @vmbuf_resize_by(%struct.vmbuf* %23, i32 4096)
%25 = icmp sgt i64 0, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %4
br label %32
27: ; preds = %22
br label %10
28: ; preds = %21
%29 = load %struct.vmbuf*, %struct.vmbuf** %5
%30 = load i64, i64* %8
%31 = call i32 @vmbuf_wseek(%struct.vmbuf* %29, i64 %30)
store i32 0, i32* %4
br label %32
32: ; preds = %28, %26
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
%struct.vmbuf = type { i32 }
%struct.tm = type { i32 }
define i32 @vmbuf_strftime(%struct.vmbuf* %0, i8* %1, %struct.tm* %2) {
br label %4
4: ; preds = %9, %3
%5 = tail call i64 @vmbuf_wavail(%struct.vmbuf* %0)
%6 = tail call i32 @vmbuf_wloc(%struct.vmbuf* %0)
%7 = tail call i64 @strftime(i32 %6, i64 %5, i8* %1, %struct.tm* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i64 @vmbuf_resize_by(%struct.vmbuf* %0, i32 4096)
%11 = icmp slt i64 %10, 0
br i1 %11, label %14, label %4
12: ; preds = %4
%13 = tail call i32 @vmbuf_wseek(%struct.vmbuf* %0, i64 %7)
br label %14
14: ; preds = %9, %12
%15 = phi i32 [ 0, %12 ], [ -1, %9 ]
ret i32 %15
}