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

/* Variables and functions */
struct tm *gmtime(int *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strftime(char *, int, char *, struct tm *);
int Mqtt_FormatTime(int64_t ts, char *out) {
  int64_t millisecond = ts % 1000;
  struct tm *t;
  time_t tt = (time_t)(ts) / 1000;
  t = gmtime(&tt);
  if (!t) {
    return 0;
  }
  if (0 == strftime(out, 24, "%Y-%m-%d %H:%M:%S", t)) {
    return 0;
  }
  sprintf(out + 19, ".%03d", (int)millisecond);
  return 23;
}


x86 assembly (clang, -O0):
<Mqtt_FormatTime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <gmtime@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117f <Mqtt_FormatTime+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <Mqtt_FormatTime+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x18,%esi
lea    0xe6d(%rip),%rdx
call   1030 <strftime@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11b2 <Mqtt_FormatTime+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <Mqtt_FormatTime+0xa0>
mov    -0x10(%rbp),%rdi
add    $0x13,%rdi
mov    -0x14(%rbp),%edx
lea    0xe4e(%rip),%rsi
call   1050 <sprintf@plt>
movl   $0x17,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mqtt_FormatTime>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x26,%rbx
add    %eax,%ebx
mov    %ebx,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1040 <gmtime@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11a9 <Mqtt_FormatTime+0x79>
lea    0xe90(%rip),%rdx
mov    %r14,%rdi
mov    $0x18,%esi
mov    %rax,%rcx
call   1030 <strftime@plt>
test   %rax,%rax
je     11a9 <Mqtt_FormatTime+0x79>
imul   $0x3e8,%ebx,%eax
sub    %eax,%r15d
add    $0x13,%r14
lea    0xe79(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
call   1050 <sprintf@plt>
mov    $0x17,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%03d\00"
define i32 @Mqtt_FormatTime(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.tm*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = srem i32 %9, 1000
store i32 %10, i32* %6
%11 = load i32, i32* %4
%12 = sdiv i32 %11, 1000
store i32 %12, i32* %8
%13 = call %struct.tm* @gmtime(i32* %8)
store %struct.tm* %13, %struct.tm** %7
%14 = load %struct.tm*, %struct.tm** %7
%15 = icmp ne %struct.tm* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 0, i32* %3
br label %28
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = load %struct.tm*, %struct.tm** %7
%20 = call i64 @strftime(i8* %18, i32 24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %19)
%21 = icmp eq i64 0, %20
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 0, i32* %3
br label %28
23: ; preds = %17
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 19
%26 = load i32, i32* %6
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 23, i32* %3
br label %28
28: ; preds = %23, %22, %16
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [18 x i8] c"%Y-%m-%d %H:%M:%S\00"
@.str.1 = [6 x i8] c".%03d\00"
define i32 @Mqtt_FormatTime(i32 %0, i8* %1) {
%3 = alloca i32
%4 = srem i32 %0, 1000
%5 = bitcast i32* %3 to i8*
%6 = sdiv i32 %0, 1000
store i32 %6, i32* %3
%7 = call %struct.tm* @gmtime(i32* %3)
%8 = icmp eq %struct.tm* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = call i64 @strftime(i8* %1, i32 24, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %struct.tm* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %1, i64 19
%14 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %15
15: ; preds = %9, %2, %12
%16 = phi i32 [ 23, %12 ], [ 0, %2 ], [ 0, %9 ]
ret i32 %16
}