#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 timespec {
  unsigned long tv_sec;
  unsigned long tv_nsec;
};

/* Variables and functions */
int EINTR;
int errno;
int /*<<< orphan*/ message(char *, char *, int, char *, int /*<<< orphan*/,
                           int /*<<< orphan*/);
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ serial_errcode();
int /*<<< orphan*/ serial_errmsg();
int serial_sleep(unsigned long timeout) {
  struct timespec ts;
  ts.tv_sec = (timeout / 1000);
  ts.tv_nsec = (timeout % 1000) * 1000000;
  while (nanosleep(&ts, &ts) != 0) {
    if (errno != EINTR) {
      {
        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",
                682, "nanosleep", serial_errmsg(), serial_errcode());
        errno = error;
      };
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
imul   $0xf4240,%rdx,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1050 <nanosleep@plt>
cmp    $0x0,%rax
je     1205 <serial_sleep+0xc5>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
je     1200 <serial_sleep+0xc0>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <serial_errmsg@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <serial_errcode@plt>
mov    -0x28(%rbp),%r8d
mov    %eax,%r9d
lea    0xe30(%rip),%rdi
lea    0xe45(%rip),%rsi
mov    $0x2aa,%edx
lea    0xed0(%rip),%rcx
call   1030 <message@plt>
mov    -0x24(%rbp),%ecx
mov    0x2dee(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <serial_sleep+0xcc>
jmp    1177 <serial_sleep+0x37>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_sleep>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,0x8(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rdi
imul   $0xf4240,%rdi,%rax
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    0x2e57(%rip),%r14
mov    0x2e48(%rip),%r15
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1050 <nanosleep@plt>
test   %rax,%rax
je     11e7 <serial_sleep+0xa7>
mov    (%r14),%ebp
cmp    (%r15),%ebp
je     1190 <serial_sleep+0x50>
xor    %eax,%eax
call   1040 <serial_errmsg@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <serial_errcode@plt>
lea    0xe41(%rip),%rdi
lea    0xe56(%rip),%rsi
lea    0xee6(%rip),%rcx
mov    $0x2aa,%edx
mov    %ebx,%r8d
mov    %eax,%r9d
call   1030 <message@plt>
mov    %ebp,(%r14)
mov    $0xffffffff,%eax
jmp    11e9 <serial_sleep+0xa9>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@errno = global i32 0
@EINTR = 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 = [10 x i8] c"nanosleep\00"
define i32 @serial_sleep(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.timespec
%5 = alloca i32
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = udiv i64 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %7, i64* %8
%9 = load i64, i64* %3
%10 = urem i64 %9, 1000
%11 = mul i64 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %11, i64* %12
br label %13
13: ; preds = %26, %1
%14 = call i64 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = load i32, i32* @EINTR
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* @errno
store i32 %21, i32* %5
%22 = call i32 (...) @serial_errmsg()
%23 = call i32 (...) @serial_errcode()
%24 = 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 682, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i32, i32* %5
store i32 %25, i32* @errno
store i32 -1, i32* %2
br label %28
26: ; preds = %16
br label %13
27: ; preds = %13
store i32 0, i32* %2
br label %28
28: ; preds = %27, %20
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i32 0
@EINTR = 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 = [10 x i8] c"nanosleep\00"
define i32 @serial_sleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = udiv i64 %0, 1000
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %4, i64* %5
%6 = urem i64 %0, 1000
%7 = mul nuw nsw i64 %6, 1000000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %7, i64* %8
br label %9
9: ; preds = %12, %1
%10 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i32, i32* @EINTR
%15 = icmp eq i32 %13, %14
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = call i32 (...) @serial_errmsg()
%18 = call i32 (...) @serial_errcode()
%19 = 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 682, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18)
store i32 %13, i32* @errno
br label %20
20: ; preds = %9, %16
%21 = phi i32 [ -1, %16 ], [ 0, %9 ]
ret i32 %21
}