#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 timezone {
  int /*<<< orphan*/ tz_dsttime;
  int /*<<< orphan*/ tz_minuteswest;
};
struct timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct timeb {
  int millitm;
  int /*<<< orphan*/ dstflag;
  int /*<<< orphan*/ timezone;
  int /*<<< orphan*/ time;
};

/* Variables and functions */
scalar_t__ __gettimeofday(struct timeval *, struct timezone *);
int ftime(struct timeb *timebuf) {
  struct timeval tv;
  struct timezone tz;
  if (__gettimeofday(&tv, &tz) < 0)
    return -1;
  timebuf->time = tv.tv_sec;
  timebuf->millitm = (tv.tv_usec + 500) / 1000;
  if (timebuf->millitm == 1000) {
    ++timebuf->time;
    timebuf->millitm = 0;
  }
  timebuf->timezone = tz.tz_minuteswest;
  timebuf->dstflag = tz.tz_dsttime;
  return 0;
}


x86 assembly (clang, -O0):
<ftime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <__gettimeofday@plt>
cmp    $0x0,%rax
jge    113f <ftime+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a3 <ftime+0x93>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x18(%rbp),%eax
add    $0x1f4,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x3e8,(%rax)
jne    1188 <ftime+0x78>
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%ecx
add    $0x1,%ecx
mov    %ecx,0xc(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftime>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <__gettimeofday@plt>
test   %rax,%rax
js     117f <ftime+0x6f>
mov    0xc(%rsp),%eax
mov    %eax,0xc(%rbx)
mov    0x8(%rsp),%ecx
lea    0x1f4(%rcx),%edx
movslq %edx,%rdx
imul   $0x10624dd3,%rdx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rdx
add    %esi,%edx
mov    %edx,(%rbx)
add    $0xfff0bfb4,%ecx
cmp    $0x3e7,%ecx
ja     116e <ftime+0x5e>
add    $0x1,%eax
mov    %eax,0xc(%rbx)
movl   $0x0,(%rbx)
mov    0x4(%rsp),%eax
mov    %eax,0x8(%rbx)
mov    (%rsp),%eax
mov    %eax,0x4(%rbx)
xor    %eax,%eax
jmp    1184 <ftime+0x74>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeb = type { i32, i32, i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32, i32 }
define i32 @ftime(%struct.timeb* %0) {
%2 = alloca i32
%3 = alloca %struct.timeb*
%4 = alloca %struct.timeval
%5 = alloca %struct.timezone
store %struct.timeb* %0, %struct.timeb** %3
%6 = call i64 @__gettimeofday(%struct.timeval* %4, %struct.timezone* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %40
9: ; preds = %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load %struct.timeb*, %struct.timeb** %3
%13 = getelementptr inbounds %struct.timeb, %struct.timeb* %12, i32 0, i32 3
store i32 %11, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 500
%17 = sdiv i32 %16, 1000
%18 = load %struct.timeb*, %struct.timeb** %3
%19 = getelementptr inbounds %struct.timeb, %struct.timeb* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load %struct.timeb*, %struct.timeb** %3
%21 = getelementptr inbounds %struct.timeb, %struct.timeb* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = icmp eq i32 %22, 1000
br i1 %23, label %24, label %31
24: ; preds = %9
%25 = load %struct.timeb*, %struct.timeb** %3
%26 = getelementptr inbounds %struct.timeb, %struct.timeb* %25, i32 0, i32 3
%27 = load i32, i32* %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %26
%29 = load %struct.timeb*, %struct.timeb** %3
%30 = getelementptr inbounds %struct.timeb, %struct.timeb* %29, i32 0, i32 0
store i32 0, i32* %30
br label %31
31: ; preds = %24, %9
%32 = getelementptr inbounds %struct.timezone, %struct.timezone* %5, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load %struct.timeb*, %struct.timeb** %3
%35 = getelementptr inbounds %struct.timeb, %struct.timeb* %34, i32 0, i32 2
store i32 %33, i32* %35
%36 = getelementptr inbounds %struct.timezone, %struct.timezone* %5, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load %struct.timeb*, %struct.timeb** %3
%39 = getelementptr inbounds %struct.timeb, %struct.timeb* %38, i32 0, i32 1
store i32 %37, i32* %39
store i32 0, i32* %2
br label %40
40: ; preds = %31, %8
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.timeb = type { i32, i32, i32, i32 }
%struct.timeval = type { i32, i32 }
%struct.timezone = type { i32, i32 }
define i32 @ftime(%struct.timeb* nocapture %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timezone
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timezone* %3 to i8*
%6 = call i64 @__gettimeofday(%struct.timeval* %2, %struct.timezone* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %28, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timeb, %struct.timeb* %0, i64 0, i32 3
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 500
%15 = sdiv i32 %14, 1000
%16 = getelementptr inbounds %struct.timeb, %struct.timeb* %0, i64 0, i32 0
store i32 %15, i32* %16
%17 = add i32 %13, -999500
%18 = icmp ult i32 %17, 1000
br i1 %18, label %19, label %21
19: ; preds = %8
%20 = add nsw i32 %10, 1
store i32 %20, i32* %11
store i32 0, i32* %16
br label %21
21: ; preds = %19, %8
%22 = getelementptr inbounds %struct.timezone, %struct.timezone* %3, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.timeb, %struct.timeb* %0, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.timezone, %struct.timezone* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timeb, %struct.timeb* %0, i64 0, i32 1
store i32 %26, i32* %27
br label %28
28: ; preds = %1, %21
%29 = phi i32 [ 0, %21 ], [ -1, %1 ]
ret i32 %29
}