#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_usec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int, int);
char *tv_to_str(struct timeval *origtv) {
  static char res[32];
  struct timeval localtv = *origtv;
  struct timeval *tv = &localtv;
  char *s = res;
  if (tv->tv_usec < 0) {
    tv->tv_sec--;
    tv->tv_usec += 1000 * 1000;
  }
  if (tv->tv_usec < 0 || tv->tv_usec > 1000 * 1000)
    return "(error)";
  if (tv->tv_sec < 0) {
    *(s++) = '-';
    if (tv->tv_usec) {
      tv->tv_sec++;
      tv->tv_usec = 1000 * 1000 - tv->tv_usec;
    }
    tv->tv_sec *= -1;
  }
  sprintf(s, "%d.%06d", (int)tv->tv_sec, (int)tv->tv_usec);
  return res;
}


x86 assembly (clang, -O0):
<tv_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0x2f0a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1162 <tv_to_str+0x52>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0xf4240,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jl     117f <tv_to_str+0x6f>
mov    -0x20(%rbp),%rax
cmpl   $0xf4240,(%rax)
jle    118f <tv_to_str+0x7f>
lea    0xe7a(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    120d <tv_to_str+0xfd>
mov    -0x20(%rbp),%rax
cmpl   $0x0,0x4(%rax)
jge    11e5 <tv_to_str+0xd5>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2d,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
je     11da <tv_to_str+0xca>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    $0xf4240,%ecx
sub    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
imul   $0xffffffff,0x4(%rax),%ecx
mov    %ecx,0x4(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
lea    0xe0b(%rip),%rsi
call   1030 <sprintf@plt>
lea    0x2e37(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tv_to_str>:
push   %rbx
mov    (%rdi),%edx
lea    0xf4240(%rdx),%ecx
test   %edx,%edx
cmovns %edx,%ecx
cmp    $0xf4240,%ecx
jbe    112f <tv_to_str+0x1f>
lea    0xed3(%rip),%rbx
jmp    1175 <tv_to_str+0x65>
sar    $0x1f,%edx
add    0x4(%rdi),%edx
lea    0x2f04(%rip),%rbx
js     1143 <tv_to_str+0x33>
mov    %rbx,%rdi
jmp    1169 <tv_to_str+0x59>
movb   $0x2d,0x2ef6(%rip)
mov    $0xf4240,%eax
sub    %ecx,%eax
xor    %esi,%esi
test   %ecx,%ecx
cmovne %eax,%ecx
setne  %sil
add    %edx,%esi
neg    %esi
lea    0x2eda(%rip),%rdi
mov    %esi,%edx
lea    0xe98(%rip),%rsi
call   1030 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@tv_to_str.res = internal global [32 x i8] zeroinitializer
@.str = [8 x i8] c"(error)\00"
@.str.1 = [8 x i8] c"%d.%06d\00"
define i8* @tv_to_str(%struct.timeval* %0) {
%2 = alloca i8*
%3 = alloca %struct.timeval*
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval*
%6 = alloca i8*
store %struct.timeval* %0, %struct.timeval** %3
%7 = load %struct.timeval*, %struct.timeval** %3
%8 = bitcast %struct.timeval* %4 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
store %struct.timeval* %4, %struct.timeval** %5
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tv_to_str.res, i64 0, i64 0), i8** %6
%10 = load %struct.timeval*, %struct.timeval** %5
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %1
%15 = load %struct.timeval*, %struct.timeval** %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = add nsw i32 %17, -1
store i32 %18, i32* %16
%19 = load %struct.timeval*, %struct.timeval** %5
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1000000
store i32 %22, i32* %20
br label %23
23: ; preds = %14, %1
%24 = load %struct.timeval*, %struct.timeval** %5
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = icmp slt i32 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %23
%29 = load %struct.timeval*, %struct.timeval** %5
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = icmp sgt i32 %31, 1000000
br i1 %32, label %33, label %34
33: ; preds = %28, %23
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %2
br label %71
34: ; preds = %28
%35 = load %struct.timeval*, %struct.timeval** %5
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %62
39: ; preds = %34
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %6
store i8 45, i8* %40
%42 = load %struct.timeval*, %struct.timeval** %5
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %39
%47 = load %struct.timeval*, %struct.timeval** %5
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 1
store i32 %50, i32* %48
%51 = load %struct.timeval*, %struct.timeval** %5
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = sub nsw i32 1000000, %53
%55 = load %struct.timeval*, %struct.timeval** %5
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 0
store i32 %54, i32* %56
br label %57
57: ; preds = %46, %39
%58 = load %struct.timeval*, %struct.timeval** %5
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %58, i32 0, i32 1
%60 = load i32, i32* %59
%61 = mul nsw i32 %60, -1
store i32 %61, i32* %59
br label %62
62: ; preds = %57, %34
%63 = load i8*, i8** %6
%64 = load %struct.timeval*, %struct.timeval** %5
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %64, i32 0, i32 1
%66 = load i32, i32* %65
%67 = load %struct.timeval*, %struct.timeval** %5
%68 = getelementptr inbounds %struct.timeval, %struct.timeval* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @sprintf(i8* %63, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %66, i32 %69)
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tv_to_str.res, i64 0, i64 0), i8** %2
br label %71
71: ; preds = %62, %33
%72 = load i8*, i8** %2
ret i8* %72
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@tv_to_str.res = internal global [32 x i8] zeroinitializer
@.str = [8 x i8] c"(error)\00"
@.str.1 = [8 x i8] c"%d.%06d\00"
define i8* @tv_to_str(%struct.timeval* nocapture %0) {
%2 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%5 = load i32, i32* %4
%6 = icmp slt i32 %3, 0
%7 = add nsw i32 %3, 1000000
%8 = ashr i32 %3, 31
%9 = add nsw i32 %5, %8
%10 = select i1 %6, i32 %7, i32 %3
%11 = icmp ugt i32 %10, 1000000
br i1 %11, label %26, label %12
12: ; preds = %1
%13 = icmp slt i32 %9, 0
br i1 %13, label %14, label %21
14: ; preds = %12
store i8 45, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @tv_to_str.res, i64 0, i64 0)
%15 = icmp eq i32 %10, 0
%16 = sub nuw nsw i32 1000000, %10
%17 = xor i1 %15, true
%18 = sext i1 %17 to i32
%19 = sub i32 %18, %9
%20 = select i1 %15, i32 0, i32 %16
br label %21
21: ; preds = %14, %12
%22 = phi i32 [ %19, %14 ], [ %9, %12 ]
%23 = phi i32 [ %20, %14 ], [ %10, %12 ]
%24 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @tv_to_str.res, i64 0, i64 1), %14 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @tv_to_str.res, i64 0, i64 0), %12 ]
%25 = tail call i32 @sprintf(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
br label %26
26: ; preds = %1, %21
%27 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @tv_to_str.res, i64 0, i64 0), %21 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %1 ]
ret i8* %27
}