#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 tm {
  int tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};
struct timeval {
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
scalar_t__ exists(char *);
struct tm *gmtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int, int, int, int, int, int,
                           int, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void mkfname(char *fname, char *nm, char *ext, struct timeval *t) {
  time_t tt = (t == NULL) ? time(NULL) : t->tv_sec;
  struct tm *tm = gmtime(&tt);
  int num = 0;
  do {
    sprintf(fname, "%s.%04d.%02d.%02d.%02d.%02d.%02d.%01d.%s", nm,
            tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, tm->tm_hour,
            tm->tm_min, tm->tm_sec, num, ext);
  } while (exists(fname) && num++ < 10);
  return;
}


x86 assembly (clang, -O0):
<mkfname>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
mov    %rcx,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1177 <mkfname+0x37>
xor    %eax,%eax
mov    %eax,%edi
call   1030 <time@plt>
mov    %eax,-0x48(%rbp)
jmp    1180 <mkfname+0x40>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x34(%rbp),%rdi
call   1050 <gmtime@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x40(%rbp),%rax
mov    (%rax),%ecx
add    $0x76c,%ecx
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%r14d
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%ebx
mov    -0x40(%rbp),%rax
mov    0x14(%rax),%r11d
mov    -0x44(%rbp),%r10d
mov    -0x28(%rbp),%rax
lea    0xe1c(%rip),%rsi
mov    %r14d,(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r11d,0x10(%rsp)
mov    %r10d,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <exists@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x49(%rbp)
je     122f <mkfname+0xef>
mov    -0x44(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x44(%rbp)
cmp    $0xa,%eax
setl   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    119a <mkfname+0x5a>
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkfname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
test   %rcx,%rcx
je     115d <mkfname+0x1d>
mov    (%rcx),%eax
jmp    1164 <mkfname+0x24>
xor    %edi,%edi
call   1030 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1050 <gmtime@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0xe82(%rip),%r13
xchg   %ax,%ax
mov    (%rbx),%ecx
mov    $0x76c,%eax
add    %eax,%ecx
mov    0x4(%rbx),%r8d
mov    0x8(%rbx),%r9d
mov    0xc(%rbx),%r10d
mov    0x10(%rbx),%r11d
mov    0x14(%rbx),%eax
sub    $0x8,%rsp
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
push   %r14
push   %rbp
push   %rax
push   %r11
push   %r10
call   1060 <sprintf@plt>
add    $0x30,%rsp
mov    %r12,%rdi
call   1040 <exists@plt>
cmp    $0x9,%ebp
ja     11cf <mkfname+0x8f>
add    $0x1,%ebp
test   %rax,%rax
jne    1180 <mkfname+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [41 x i8] c"%s.%04d.%02d.%02d.%02d.%02d.%02d.%01d.%s\00"
define void @mkfname(i8* %0, i8* %1, i8* %2, %struct.timeval* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.timeval*
%9 = alloca i32
%10 = alloca %struct.tm*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store %struct.timeval* %3, %struct.timeval** %8
%12 = load %struct.timeval*, %struct.timeval** %8
%13 = icmp eq %struct.timeval* %12, null
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = call i32 @time(i32* null)
br label %20
16: ; preds = %4
%17 = load %struct.timeval*, %struct.timeval** %8
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 0
%19 = load i32, i32* %18
br label %20
20: ; preds = %16, %14
%21 = phi i32 [ %15, %14 ], [ %19, %16 ]
store i32 %21, i32* %9
%22 = call %struct.tm* @gmtime(i32* %9)
store %struct.tm* %22, %struct.tm** %10
store i32 0, i32* %11
br label %23
23: ; preds = %56, %20
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = load %struct.tm*, %struct.tm** %10
%27 = getelementptr inbounds %struct.tm, %struct.tm* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = add nsw i32 %28, 1900
%30 = load %struct.tm*, %struct.tm** %10
%31 = getelementptr inbounds %struct.tm, %struct.tm* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.tm*, %struct.tm** %10
%34 = getelementptr inbounds %struct.tm, %struct.tm* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = load %struct.tm*, %struct.tm** %10
%37 = getelementptr inbounds %struct.tm, %struct.tm* %36, i32 0, i32 3
%38 = load i32, i32* %37
%39 = load %struct.tm*, %struct.tm** %10
%40 = getelementptr inbounds %struct.tm, %struct.tm* %39, i32 0, i32 4
%41 = load i32, i32* %40
%42 = load %struct.tm*, %struct.tm** %10
%43 = getelementptr inbounds %struct.tm, %struct.tm* %42, i32 0, i32 5
%44 = load i32, i32* %43
%45 = load i32, i32* %11
%46 = load i8*, i8** %7
%47 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %29, i32 %32, i32 %35, i32 %38, i32 %41, i32 %44, i32 %45, i8* %46)
br label %48
48: ; preds = %23
%49 = load i8*, i8** %5
%50 = call i64 @exists(i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
%55 = icmp slt i32 %53, 10
br label %56
56: ; preds = %52, %48
%57 = phi i1 [ false, %48 ], [ %55, %52 ]
br i1 %57, label %23, label %58
58: ; preds = %56
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@.str = [41 x i8] c"%s.%04d.%02d.%02d.%02d.%02d.%02d.%01d.%s\00"
define void @mkfname(i8* %0, i8* %1, i8* %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = icmp eq %struct.timeval* %3, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @time(i32* null)
br label %13
10: ; preds = %4
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%12 = load i32, i32* %11
br label %13
13: ; preds = %10, %8
%14 = phi i32 [ %9, %8 ], [ %12, %10 ]
store i32 %14, i32* %5
%15 = call %struct.tm* @gmtime(i32* %5)
%16 = getelementptr inbounds %struct.tm, %struct.tm* %15, i64 0, i32 0
%17 = getelementptr inbounds %struct.tm, %struct.tm* %15, i64 0, i32 1
%18 = getelementptr inbounds %struct.tm, %struct.tm* %15, i64 0, i32 2
%19 = getelementptr inbounds %struct.tm, %struct.tm* %15, i64 0, i32 3
%20 = getelementptr inbounds %struct.tm, %struct.tm* %15, i64 0, i32 4
%21 = getelementptr inbounds %struct.tm, %struct.tm* %15, i64 0, i32 5
br label %22
22: ; preds = %22, %13
%23 = phi i32 [ 0, %13 ], [ %34, %22 ]
%24 = load i32, i32* %16
%25 = add nsw i32 %24, 1900
%26 = load i32, i32* %17
%27 = load i32, i32* %18
%28 = load i32, i32* %19
%29 = load i32, i32* %20
%30 = load i32, i32* %21
%31 = call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %25, i32 %26, i32 %27, i32 %28, i32 %29, i32 %30, i32 %23, i8* %2)
%32 = call i64 @exists(i8* %0)
%33 = icmp ne i64 %32, 0
%34 = add nuw nsw i32 %23, 1
%35 = icmp ult i32 %23, 10
%36 = and i1 %35, %33
br i1 %36, label %22, label %37
37: ; preds = %22
ret void
}