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

/* Variables and functions */
size_t strftime(int /*<<< orphan*/, size_t, char const *, struct tm const *);
scalar_t__ vmfile_resize_by(struct vmfile *, unsigned long long);
size_t vmfile_wavail(struct vmfile *);
int /*<<< orphan*/ vmfile_wloc(struct vmfile *);
int /*<<< orphan*/ vmfile_wseek(struct vmfile *, size_t);
int vmfile_strftime(struct vmfile *vmb, const char *format,
                    const struct tm *tm) {
  size_t n;
  for (;;) {
    size_t wav = vmfile_wavail(vmb);
    n = strftime(vmfile_wloc(vmb), wav, format, tm);
    if (n > 0)
      break;
    if (0 > vmfile_resize_by(vmb, 4096ULL))
      return -1;
  }
  vmfile_wseek(vmb, n);
  return 0;
}


x86 assembly (clang, -O0):
<vmfile_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   1050 <vmfile_wavail@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <vmfile_wloc@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <strftime@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11a1 <vmfile_strftime+0x51>
jmp    11ce <vmfile_strftime+0x7e>
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1040 <vmfile_resize_by@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    11c9 <vmfile_strftime+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <vmfile_strftime+0x92>
jmp    1164 <vmfile_strftime+0x14>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <vmfile_wseek@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmfile_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   1050 <vmfile_wavail@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <vmfile_wloc@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1060 <strftime@plt>
test   %rax,%rax
jne    11b1 <vmfile_strftime+0x61>
mov    $0x1000,%esi
mov    %r12,%rdi
call   1040 <vmfile_resize_by@plt>
test   %rax,%rax
jns    1170 <vmfile_strftime+0x20>
mov    $0xffffffff,%eax
jmp    11be <vmfile_strftime+0x6e>
mov    %r12,%rdi
mov    %rax,%rsi
call   1070 <vmfile_wseek@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.vmfile = type { i32 }
%struct.tm = type { i32 }
define i32 @vmfile_strftime(%struct.vmfile* %0, i8* %1, %struct.tm* %2) {
%4 = alloca i32
%5 = alloca %struct.vmfile*
%6 = alloca i8*
%7 = alloca %struct.tm*
%8 = alloca i64
%9 = alloca i64
store %struct.vmfile* %0, %struct.vmfile** %5
store i8* %1, i8** %6
store %struct.tm* %2, %struct.tm** %7
br label %10
10: ; preds = %27, %3
%11 = load %struct.vmfile*, %struct.vmfile** %5
%12 = call i64 @vmfile_wavail(%struct.vmfile* %11)
store i64 %12, i64* %9
%13 = load %struct.vmfile*, %struct.vmfile** %5
%14 = call i32 @vmfile_wloc(%struct.vmfile* %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.vmfile*, %struct.vmfile** %5
%24 = call i64 @vmfile_resize_by(%struct.vmfile* %23, i64 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.vmfile*, %struct.vmfile** %5
%30 = load i64, i64* %8
%31 = call i32 @vmfile_wseek(%struct.vmfile* %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.vmfile = type { i32 }
%struct.tm = type { i32 }
define i32 @vmfile_strftime(%struct.vmfile* %0, i8* %1, %struct.tm* %2) {
br label %4
4: ; preds = %9, %3
%5 = tail call i64 @vmfile_wavail(%struct.vmfile* %0)
%6 = tail call i32 @vmfile_wloc(%struct.vmfile* %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 @vmfile_resize_by(%struct.vmfile* %0, i64 4096)
%11 = icmp slt i64 %10, 0
br i1 %11, label %14, label %4
12: ; preds = %4
%13 = tail call i32 @vmfile_wseek(%struct.vmfile* %0, i64 %7)
br label %14
14: ; preds = %9, %12
%15 = phi i32 [ 0, %12 ], [ -1, %9 ]
ret i32 %15
}