#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
scalar_t__ floor(double);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
double round(double);
int to_tc_byte(int);
uint32_t add_timecode(double framerate, int frame, uint8_t *buffer,
                      uint32_t *data_offset) {
  uint32_t result = *data_offset;
  memset(buffer + *data_offset, 0, 8);
  int hours, minutes, seconds, frames;
  double time = framerate == 0
                    ? 0
                    : frame / (framerate > 1 ? round(framerate) : framerate);
  hours = (int)floor(time / 3600);
  minutes = ((int)floor(time / 60)) % 60;
  seconds = ((int)floor(time)) % 60;
  frames = framerate > 1 ? (frame % ((int)round(framerate))) : 0;
  buffer[*data_offset] = to_tc_byte(frames) & 0x3F;
  buffer[*data_offset + 1] = to_tc_byte(seconds) & 0x7F;
  buffer[*data_offset + 2] = to_tc_byte(minutes) & 0x7F;
  buffer[*data_offset + 3] = to_tc_byte(hours) & 0x3F;
  *data_offset += 8;
  return result;
}


x86 assembly (clang, -O0):
<add_timecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
add    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11a4 <add_timecode+0x64>
jp     11a4 <add_timecode+0x64>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    11f6 <add_timecode+0xb6>
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xe45(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11d9 <add_timecode+0x99>
movsd  -0x8(%rbp),%xmm0
call   1050 <round@plt>
movsd  %xmm0,-0x58(%rbp)
jmp    11e3 <add_timecode+0xa3>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xe03(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1060 <floor@plt>
mov    %eax,-0x28(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xde2(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1060 <floor@plt>
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2c(%rbp)
movsd  -0x40(%rbp),%xmm0
call   1060 <floor@plt>
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x30(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xda4(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1288 <add_timecode+0x148>
mov    -0xc(%rbp),%eax
mov    %eax,-0x60(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <round@plt>
mov    -0x60(%rbp),%eax
cvttsd2si %xmm0,%ecx
cltd
idiv   %ecx
mov    %edx,-0x5c(%rbp)
jmp    1292 <add_timecode+0x152>
xor    %eax,%eax
mov    %eax,-0x5c(%rbp)
jmp    1292 <add_timecode+0x152>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <to_tc_byte@plt>
mov    %eax,%edx
and    $0x3f,%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%edi
call   1040 <to_tc_byte@plt>
mov    %eax,%edx
and    $0x7f,%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x2c(%rbp),%edi
call   1040 <to_tc_byte@plt>
mov    %eax,%edx
and    $0x7f,%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%edi
call   1040 <to_tc_byte@plt>
mov    %eax,%edx
and    $0x3f,%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x8,%ecx
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_timecode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r13
mov    %rsi,%r14
mov    %edi,%ebx
movsd  %xmm0,0x10(%rsp)
movslq (%rdx),%rax
mov    %rax,0x20(%rsp)
lea    (%rsi,%rax,4),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
movsd  0x10(%rsp),%xmm1
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jne    1189 <add_timecode+0x49>
jnp    11b7 <add_timecode+0x77>
xorps  %xmm2,%xmm2
cvtsi2sd %ebx,%xmm2
ucomisd 0xe68(%rip),%xmm1
movapd %xmm1,%xmm0
jbe    11b3 <add_timecode+0x73>
movapd %xmm1,%xmm0
movsd  %xmm2,0x8(%rsp)
call   1050 <round@plt>
movsd  0x8(%rsp),%xmm2
divsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
movapd %xmm2,%xmm0
divsd  0xe3f(%rip),%xmm0
call   1060 <floor@plt>
mov    %rax,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
divsd  0xe2f(%rip),%xmm0
call   1060 <floor@plt>
mov    %rax,%r12
movsd  0x8(%rsp),%xmm0
call   1060 <floor@plt>
movsd  0x10(%rsp),%xmm0
mov    %rax,%rbp
ucomisd 0xdfb(%rip),%xmm0
jbe    1218 <add_timecode+0xd8>
call   1050 <round@plt>
cvttsd2si %xmm0,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %edx,%r15d
movslq %ebp,%rbp
imul   $0xffffffff88888889,%rbp,%rax
shr    $0x20,%rax
add    %ebp,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x5,%eax
add    %ecx,%eax
imul   $0x3c,%eax,%eax
sub    %eax,%ebp
movslq %r12d,%rbx
imul   $0xffffffff88888889,%rbx,%rax
shr    $0x20,%rax
add    %ebx,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x5,%eax
add    %ecx,%eax
imul   $0x3c,%eax,%eax
sub    %eax,%ebx
mov    %r15d,%edi
call   1040 <to_tc_byte@plt>
and    $0x3f,%eax
movslq 0x0(%r13),%rcx
mov    %eax,(%r14,%rcx,4)
mov    %ebp,%edi
call   1040 <to_tc_byte@plt>
and    $0x7f,%eax
movslq 0x0(%r13),%rcx
mov    %eax,0x4(%r14,%rcx,4)
mov    %ebx,%edi
call   1040 <to_tc_byte@plt>
and    $0x7f,%eax
movslq 0x0(%r13),%rcx
mov    %eax,0x8(%r14,%rcx,4)
mov    0x18(%rsp),%rdi
call   1040 <to_tc_byte@plt>
and    $0x3f,%eax
movslq 0x0(%r13),%rcx
mov    %eax,0xc(%r14,%rcx,4)
addl   $0x8,0x0(%r13)
mov    0x20(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @add_timecode(double %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca double
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca double
store double %0, double* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%15 = load i32*, i32** %8
%16 = load i32, i32* %15
store i32 %16, i32* %9
%17 = load i32*, i32** %7
%18 = load i32*, i32** %8
%19 = load i32, i32* %18
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %17, i64 %20
%22 = call i32 @memset(i32* %21, i32 0, i32 8)
%23 = load double, double* %5
%24 = fcmp oeq double %23, 0.000000e+00
br i1 %24, label %25, label %26
25: ; preds = %4
br label %39
26: ; preds = %4
%27 = load i32, i32* %6
%28 = sitofp i32 %27 to double
%29 = load double, double* %5
%30 = fcmp ogt double %29, 1.000000e+00
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load double, double* %5
%33 = call double @llvm.round.f64(double %32)
br label %36
34: ; preds = %26
%35 = load double, double* %5
br label %36
36: ; preds = %34, %31
%37 = phi double [ %33, %31 ], [ %35, %34 ]
%38 = fdiv double %28, %37
br label %39
39: ; preds = %36, %25
%40 = phi double [ 0.000000e+00, %25 ], [ %38, %36 ]
store double %40, double* %14
%41 = load double, double* %14
%42 = fdiv double %41, 3.600000e+03
%43 = call i64 @floor(double %42)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %10
%45 = load double, double* %14
%46 = fdiv double %45, 6.000000e+01
%47 = call i64 @floor(double %46)
%48 = trunc i64 %47 to i32
%49 = srem i32 %48, 60
store i32 %49, i32* %11
%50 = load double, double* %14
%51 = call i64 @floor(double %50)
%52 = trunc i64 %51 to i32
%53 = srem i32 %52, 60
store i32 %53, i32* %12
%54 = load double, double* %5
%55 = fcmp ogt double %54, 1.000000e+00
br i1 %55, label %56, label %62
56: ; preds = %39
%57 = load i32, i32* %6
%58 = load double, double* %5
%59 = call double @llvm.round.f64(double %58)
%60 = fptosi double %59 to i32
%61 = srem i32 %57, %60
br label %63
62: ; preds = %39
br label %63
63: ; preds = %62, %56
%64 = phi i32 [ %61, %56 ], [ 0, %62 ]
store i32 %64, i32* %13
%65 = load i32, i32* %13
%66 = call i32 @to_tc_byte(i32 %65)
%67 = and i32 %66, 63
%68 = load i32*, i32** %7
%69 = load i32*, i32** %8
%70 = load i32, i32* %69
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i32, i32* %68, i64 %71
store i32 %67, i32* %72
%73 = load i32, i32* %12
%74 = call i32 @to_tc_byte(i32 %73)
%75 = and i32 %74, 127
%76 = load i32*, i32** %7
%77 = load i32*, i32** %8
%78 = load i32, i32* %77
%79 = add nsw i32 %78, 1
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %76, i64 %80
store i32 %75, i32* %81
%82 = load i32, i32* %11
%83 = call i32 @to_tc_byte(i32 %82)
%84 = and i32 %83, 127
%85 = load i32*, i32** %7
%86 = load i32*, i32** %8
%87 = load i32, i32* %86
%88 = add nsw i32 %87, 2
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i32, i32* %85, i64 %89
store i32 %84, i32* %90
%91 = load i32, i32* %10
%92 = call i32 @to_tc_byte(i32 %91)
%93 = and i32 %92, 63
%94 = load i32*, i32** %7
%95 = load i32*, i32** %8
%96 = load i32, i32* %95
%97 = add nsw i32 %96, 3
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i32, i32* %94, i64 %98
store i32 %93, i32* %99
%100 = load i32*, i32** %8
%101 = load i32, i32* %100
%102 = add nsw i32 %101, 8
store i32 %102, i32* %100
%103 = load i32, i32* %9
ret i32 %103
}

LLVM IR (-O2):
define i32 @add_timecode(double %0, i32 %1, i32* %2, i32* nocapture %3) {
%5 = load i32, i32* %3
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i32, i32* %2, i64 %6
%8 = tail call i32 @memset(i32* %7, i32 0, i32 8)
%9 = fcmp oeq double %0, 0.000000e+00
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = sitofp i32 %1 to double
%12 = fcmp ogt double %0, 1.000000e+00
%13 = tail call double @llvm.round.f64(double %0)
%14 = select i1 %12, double %13, double %0
%15 = fdiv double %11, %14
br label %16
16: ; preds = %4, %10
%17 = phi double [ %15, %10 ], [ 0.000000e+00, %4 ]
%18 = fdiv double %17, 3.600000e+03
%19 = tail call i64 @floor(double %18)
%20 = fdiv double %17, 6.000000e+01
%21 = tail call i64 @floor(double %20)
%22 = tail call i64 @floor(double %17)
%23 = fcmp ogt double %0, 1.000000e+00
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = tail call double @llvm.round.f64(double %0)
%26 = fptosi double %25 to i32
%27 = srem i32 %1, %26
br label %28
28: ; preds = %16, %24
%29 = phi i32 [ %27, %24 ], [ 0, %16 ]
%30 = trunc i64 %22 to i32
%31 = srem i32 %30, 60
%32 = trunc i64 %21 to i32
%33 = srem i32 %32, 60
%34 = trunc i64 %19 to i32
%35 = tail call i32 @to_tc_byte(i32 %29)
%36 = and i32 %35, 63
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %2, i64 %38
store i32 %36, i32* %39
%40 = tail call i32 @to_tc_byte(i32 %31)
%41 = and i32 %40, 127
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %2, i64 %44
store i32 %41, i32* %45
%46 = tail call i32 @to_tc_byte(i32 %33)
%47 = and i32 %46, 127
%48 = load i32, i32* %3
%49 = add nsw i32 %48, 2
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %2, i64 %50
store i32 %47, i32* %51
%52 = tail call i32 @to_tc_byte(i32 %34)
%53 = and i32 %52, 63
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 3
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %2, i64 %56
store i32 %53, i32* %57
%58 = load i32, i32* %3
%59 = add nsw i32 %58, 8
store i32 %59, i32* %3
ret i32 %5
}