#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
int tkh_encode_time_into(const struct timeval *tv, char *target,
                         int max_length) {
  if (max_length < 8)
    return -1;
  if (tv->tv_sec >= 99999999) {
    memset(target, '9', 8);
  } else if (tv->tv_usec < 0) {
    memset(target, '!', 8);
  } else {
    char buffer[32];
    if (tv->tv_sec == 0) {
      snprintf(buffer, 32, "0.%06ld", tv->tv_usec);
      memcpy(target, buffer, 8);
    } else {
      char buffer[32];
      snprintf(buffer, 32, "%ld.%06ld", tv->tv_sec, tv->tv_usec);
      if (buffer[7] == '.') {
        memcpy(target + 1, buffer, 7);
        *target = '0';
      } else
        memcpy(target, buffer, 8);
    }
  }
  if (max_length > 8)
    target[8] = 0;
  return 8;
}


x86 assembly (clang, -O0):
<tkh_encode_time_into>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    1159 <tkh_encode_time_into+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <tkh_encode_time_into+0x14b>
mov    -0x10(%rbp),%rax
cmpl   $0x5f5e0ff,(%rax)
jl     1181 <tkh_encode_time_into+0x51>
mov    -0x18(%rbp),%rdi
mov    $0x39,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
jmp    1262 <tkh_encode_time_into+0x132>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jge    11a8 <tkh_encode_time_into+0x78>
mov    -0x18(%rbp),%rdi
mov    $0x21,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
jmp    125d <tkh_encode_time_into+0x12d>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11ed <tkh_encode_time_into+0xbd>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %eax,%ecx
mov    $0x20,%esi
lea    0xe31(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
jmp    1258 <tkh_encode_time_into+0x128>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
mov    $0x20,%esi
lea    0xdfd(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movsbl -0x59(%rbp),%eax
cmp    $0x2e,%eax
jne    1241 <tkh_encode_time_into+0x111>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
lea    -0x60(%rbp),%rsi
mov    $0x7,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x30,(%rax)
jmp    1253 <tkh_encode_time_into+0x123>
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
jmp    1258 <tkh_encode_time_into+0x128>
jmp    125d <tkh_encode_time_into+0x12d>
jmp    1262 <tkh_encode_time_into+0x132>
cmpl   $0x8,-0x1c(%rbp)
jle    1274 <tkh_encode_time_into+0x144>
mov    -0x18(%rbp),%rax
movb   $0x0,0x8(%rax)
movl   $0x8,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tkh_encode_time_into>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    $0xffffffff,%eax
cmp    $0x8,%edx
jl     11f5 <tkh_encode_time_into+0xc5>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    (%rdi),%ecx
cmp    $0x5f5e0ff,%ecx
jl     115f <tkh_encode_time_into+0x2f>
mov    %rbx,%rdi
mov    $0x39,%esi
jmp    11a7 <tkh_encode_time_into+0x77>
mov    0x8(%rdi),%r8
test   %r8,%r8
js     119f <tkh_encode_time_into+0x6f>
test   %ecx,%ecx
je     11b3 <tkh_encode_time_into+0x83>
lea    0xe95(%rip),%rdx
mov    %rsp,%rdi
mov    $0x20,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
cmpb   $0x2e,0x7(%rsp)
jne    11d7 <tkh_encode_time_into+0xa7>
lea    0x1(%rbx),%rdi
mov    %rsp,%rsi
mov    $0x7,%edx
call   1050 <memcpy@plt>
movb   $0x30,(%rbx)
jmp    11e7 <tkh_encode_time_into+0xb7>
mov    %rbx,%rdi
mov    $0x21,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
jmp    11e7 <tkh_encode_time_into+0xb7>
lea    0xe46(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x20,%esi
mov    %r8d,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
jmp    11dd <tkh_encode_time_into+0xad>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1050 <memcpy@plt>
mov    $0x8,%eax
cmp    $0x9,%ebp
jb     11f5 <tkh_encode_time_into+0xc5>
movb   $0x0,0x8(%rbx)
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"0.%06ld\00"
@.str.1 = [10 x i8] c"%ld.%06ld\00"
define i32 @tkh_encode_time_into(%struct.timeval* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca [32 x i8]
store %struct.timeval* %0, %struct.timeval** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 8
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %77
13: ; preds = %3
%14 = load %struct.timeval*, %struct.timeval** %5
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp sge i32 %16, 99999999
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %6
%20 = call i32 @memset(i8* %19, i8 signext 57, i32 8)
br label %70
21: ; preds = %13
%22 = load %struct.timeval*, %struct.timeval** %5
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
%24 = load i64, i64* %23
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = call i32 @memset(i8* %27, i8 signext 33, i32 8)
br label %69
29: ; preds = %21
%30 = load %struct.timeval*, %struct.timeval** %5
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %44
34: ; preds = %29
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%36 = load %struct.timeval*, %struct.timeval** %5
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = trunc i64 %38 to i32
%40 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %35, i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = load i8*, i8** %6
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%43 = call i32 @memcpy(i8* %41, i8* %42, i32 8)
br label %68
44: ; preds = %29
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%46 = load %struct.timeval*, %struct.timeval** %5
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load %struct.timeval*, %struct.timeval** %5
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %45, i32 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %48, i64 %51)
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 7
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 46
br i1 %56, label %57, label %63
57: ; preds = %44
%58 = load i8*, i8** %6
%59 = getelementptr inbounds i8, i8* %58, i64 1
%60 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%61 = call i32 @memcpy(i8* %59, i8* %60, i32 7)
%62 = load i8*, i8** %6
store i8 48, i8* %62
br label %67
63: ; preds = %44
%64 = load i8*, i8** %6
%65 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%66 = call i32 @memcpy(i8* %64, i8* %65, i32 8)
br label %67
67: ; preds = %63, %57
br label %68
68: ; preds = %67, %34
br label %69
69: ; preds = %68, %26
br label %70
70: ; preds = %69, %18
%71 = load i32, i32* %7
%72 = icmp sgt i32 %71, 8
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i8*, i8** %6
%75 = getelementptr inbounds i8, i8* %74, i64 8
store i8 0, i8* %75
br label %76
76: ; preds = %73, %70
store i32 8, i32* %4
br label %77
77: ; preds = %76, %12
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"0.%06ld\00"
@.str.1 = [10 x i8] c"%ld.%06ld\00"
define i32 @tkh_encode_time_into(%struct.timeval* nocapture %0, i8* %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = alloca [32 x i8]
%6 = icmp slt i32 %2, 8
br i1 %6, label %43, label %7
7: ; preds = %3
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp sgt i32 %9, 99999998
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @memset(i8* %1, i8 signext 57, i32 8)
br label %39
13: ; preds = %7
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%15 = load i64, i64* %14
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 @memset(i8* %1, i8 signext 33, i32 8)
br label %39
19: ; preds = %13
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%21 = icmp eq i32 %9, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = trunc i64 %15 to i32
%24 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %20, i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i32 @memcpy(i8* %1, i8* %20, i32 8)
br label %38
26: ; preds = %19
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%28 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %27, i32 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %9, i64 %15)
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 7
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 46
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = getelementptr inbounds i8, i8* %1, i64 1
%34 = call i32 @memcpy(i8* %33, i8* %27, i32 7)
store i8 48, i8* %1
br label %37
35: ; preds = %26
%36 = call i32 @memcpy(i8* %1, i8* %27, i32 8)
br label %37
37: ; preds = %35, %32
br label %38
38: ; preds = %37, %22
br label %39
39: ; preds = %17, %38, %11
%40 = icmp ugt i32 %2, 8
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = getelementptr inbounds i8, i8* %1, i64 8
store i8 0, i8* %42
br label %43
43: ; preds = %39, %41, %3
%44 = phi i32 [ -1, %3 ], [ 8, %41 ], [ 8, %39 ]
ret i32 %44
}