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

/* Variables and functions */
int errno;
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ message(char *, char *, int, char *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ serial_errcode();
int /*<<< orphan*/ serial_errmsg();
int serial_timer(void) {
  struct timeval tv;
  if (gettimeofday(&tv, NULL) != 0) {
    {
      int error = errno;
      message("TRACE (%s:%d, %s): %s (%d)\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/andysan/libdivecomputer/refs/heads/"
              "master/src/serial_posix.c",
              696, "gettimeofday", serial_errmsg(), serial_errcode());
      errno = error;
    };
    return 0;
  }
  return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}


x86 assembly (clang, -O0):
<serial_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
je     11ba <serial_timer+0x7a>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <serial_errmsg@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <serial_errcode@plt>
mov    -0x18(%rbp),%r8d
mov    %eax,%r9d
lea    0xe76(%rip),%rdi
lea    0xe8b(%rip),%rsi
mov    $0x2b8,%edx
lea    0xf16(%rip),%rcx
call   1030 <message@plt>
mov    -0x14(%rbp),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11d9 <serial_timer+0x99>
imul   $0x3e8,-0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_timer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
je     119c <serial_timer+0x5c>
mov    0x2e81(%rip),%r14
mov    (%r14),%r15d
xor    %eax,%eax
call   1040 <serial_errmsg@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <serial_errcode@plt>
lea    0xe87(%rip),%rdi
lea    0xe9c(%rip),%rsi
lea    0xf2c(%rip),%rcx
mov    $0x2b8,%edx
mov    %ebp,%r8d
mov    %eax,%r9d
call   1030 <message@plt>
mov    %r15d,(%r14)
jmp    11be <serial_timer+0x7e>
imul   $0x3e8,(%rsp),%eax
movslq 0x4(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rbx
add    %ecx,%ebx
add    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i32 0
@.str = [28 x i8] c"TRACE (%s:%d, %s): %s (%d)\0A\00"
@.str.1 = [151 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/andysan/libdivecomputer/refs/heads/master/src/serial_posix.c\00"
@.str.2 = [13 x i8] c"gettimeofday\00"
define i32 @serial_timer() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = load i32, i32* @errno
store i32 %7, i32* %3
%8 = call i32 (...) @serial_errmsg()
%9 = call i32 (...) @serial_errcode()
%10 = call i32 @message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 696, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %9)
%11 = load i32, i32* %3
store i32 %11, i32* @errno
store i32 0, i32* %1
br label %20
12: ; preds = %0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%14 = load i32, i32* %13
%15 = mul nsw i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%17 = load i32, i32* %16
%18 = sdiv i32 %17, 1000
%19 = add nsw i32 %15, %18
store i32 %19, i32* %1
br label %20
20: ; preds = %12, %6
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i32 0
@.str = [28 x i8] c"TRACE (%s:%d, %s): %s (%d)\0A\00"
@.str.1 = [151 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/andysan/libdivecomputer/refs/heads/master/src/serial_posix.c\00"
@.str.2 = [13 x i8] c"gettimeofday\00"
define i32 @serial_timer() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i64 @gettimeofday(%struct.timeval* %1, i32* null)
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 (...) @serial_errmsg()
%8 = tail call i32 (...) @serial_errcode()
%9 = tail call i32 @message(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 696, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %7, i32 %8)
store i32 %6, i32* @errno
br label %18
10: ; preds = %0
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = mul nsw i32 %12, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%15 = load i32, i32* %14
%16 = sdiv i32 %15, 1000
%17 = add nsw i32 %16, %13
br label %18
18: ; preds = %10, %5
%19 = phi i32 [ 0, %5 ], [ %17, %10 ]
ret i32 %19
}