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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int poll(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
void sleep_for(int period_ms, struct timeval *start_tv) {
  int status;
  int done = 0;
  struct timeval then;
  int us;
  then.tv_sec = 0;
  do {
    status = poll(NULL, 0, period_ms);
    if (status < 0) {
      if (errno != EINTR) {
        perror("poll");
        exit(1);
      }
      if (then.tv_sec == 0) {
        then.tv_sec = start_tv->tv_sec + (period_ms / 1000);
        us = ((period_ms % 1000) * 1000) + start_tv->tv_usec;
        if (us > 1000000) {
          then.tv_sec++;
          then.tv_usec = us - 1000000;
        } else
          then.tv_usec = us;
      }
      gettimeofday(start_tv, NULL);
      period_ms = (then.tv_sec - start_tv->tv_sec) * 1000;
      period_ms += (then.tv_usec - start_tv->tv_usec) / 1000;
      if (period_ms <= 0)
        done = 1;
    } else
      done = 1;
  } while (!done);
}


x86 assembly (clang, -O0):
<sleep_for>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1040 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1279 <sleep_for+0x139>
mov    0x2e60(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <sleep_for+0x69>
lea    0xe66(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1229 <sleep_for+0xe9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    -0x10(%rbp),%rcx
add    0x8(%rcx),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0xf4240,-0x2c(%rbp)
jle    121e <sleep_for+0xde>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x20(%rbp)
jmp    1224 <sleep_for+0xe4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1229 <sleep_for+0xe9>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%rax
imul   $0x3e8,%rax,%rax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x10(%rbp),%rcx
sub    0x8(%rcx),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     1274 <sleep_for+0x134>
movl   $0x1,-0x18(%rbp)
jmp    1280 <sleep_for+0x140>
movl   $0x1,-0x18(%rbp)
jmp    1285 <sleep_for+0x145>
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    115e <sleep_for+0x1e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_for>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
xor    %r13d,%r13d
mov    0x2e86(%rip),%r15
mov    0x2e77(%rip),%r12
jmp    11aa <sleep_for+0x6a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    (%r14),%eax
mov    %r13d,%ecx
sub    %eax,%ecx
mov    %ebp,%eax
sub    0x8(%r14),%eax
imul   $0x3e8,%ecx,%ecx
cltq
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x26,%rbx
add    %eax,%ebx
add    %ecx,%ebx
test   %ebx,%ebx
jle    1213 <sleep_for+0xd3>
xor    %edi,%edi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <poll@plt>
test   %eax,%eax
jns    1213 <sleep_for+0xd3>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1222 <sleep_for+0xe2>
test   %r13,%r13
jne    1170 <sleep_for+0x30>
movslq %ebx,%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
cltq
mov    (%r14),%r13
add    %rax,%r13
imul   $0x3e8,%eax,%eax
sub    %eax,%ebx
imul   $0x3e8,%ebx,%ebp
add    0x8(%r14),%ebp
cmp    $0xf4241,%ebp
jl     1170 <sleep_for+0x30>
add    $0x1,%r13
add    $0xfff0bdc0,%ebp
jmp    1170 <sleep_for+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"poll\00"
define void @sleep_for(i32 %0, %struct.timeval* %1) {
%3 = alloca i32
%4 = alloca %struct.timeval*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
store i32 %0, i32* %3
store %struct.timeval* %1, %struct.timeval** %4
store i32 0, i32* %6
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i64 0, i64* %9
br label %10
10: ; preds = %81, %2
%11 = load i32, i32* %3
%12 = call i32 @poll(i32* null, i32 0, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %79
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %55
26: ; preds = %22
%27 = load %struct.timeval*, %struct.timeval** %4
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i32, i32* %3
%31 = sdiv i32 %30, 1000
%32 = sext i32 %31 to i64
%33 = add nsw i64 %29, %32
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i64 %33, i64* %34
%35 = load i32, i32* %3
%36 = srem i32 %35, 1000
%37 = mul nsw i32 %36, 1000
%38 = load %struct.timeval*, %struct.timeval** %4
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = add nsw i32 %37, %40
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp sgt i32 %42, 1000000
br i1 %43, label %44, label %51
44: ; preds = %26
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%46 = load i64, i64* %45
%47 = add nsw i64 %46, 1
store i64 %47, i64* %45
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, 1000000
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i32 %49, i32* %50
br label %54
51: ; preds = %26
%52 = load i32, i32* %8
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i32 %52, i32* %53
br label %54
54: ; preds = %51, %44
br label %55
55: ; preds = %54, %22
%56 = load %struct.timeval*, %struct.timeval** %4
%57 = call i32 @gettimeofday(%struct.timeval* %56, i32* null)
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%59 = load i64, i64* %58
%60 = load %struct.timeval*, %struct.timeval** %4
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %60, i32 0, i32 0
%62 = load i64, i64* %61
%63 = sub nsw i64 %59, %62
%64 = mul nsw i64 %63, 1000
%65 = trunc i64 %64 to i32
store i32 %65, i32* %3
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%67 = load i32, i32* %66
%68 = load %struct.timeval*, %struct.timeval** %4
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %68, i32 0, i32 1
%70 = load i32, i32* %69
%71 = sub nsw i32 %67, %70
%72 = sdiv i32 %71, 1000
%73 = load i32, i32* %3
%74 = add nsw i32 %73, %72
store i32 %74, i32* %3
%75 = load i32, i32* %3
%76 = icmp sle i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %55
store i32 1, i32* %6
br label %78
78: ; preds = %77, %55
br label %80
79: ; preds = %10
store i32 1, i32* %6
br label %80
80: ; preds = %79, %78
br label %81
81: ; preds = %80
%82 = load i32, i32* %6
%83 = icmp ne i32 %82, 0
%84 = xor i1 %83, true
br i1 %84, label %10, label %85
85: ; preds = %81
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"poll\00"
define void @sleep_for(i32 %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
br label %5
5: ; preds = %33, %2
%6 = phi i32 [ undef, %2 ], [ %34, %33 ]
%7 = phi i64 [ 0, %2 ], [ %35, %33 ]
%8 = phi i32 [ %0, %2 ], [ %44, %33 ]
%9 = tail call i32 @poll(i32* null, i32 0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %46
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = icmp eq i64 %7, 0
br i1 %19, label %20, label %33
20: ; preds = %18
%21 = load i64, i64* %3
%22 = sdiv i32 %8, 1000
%23 = sext i32 %22 to i64
%24 = add nsw i64 %21, %23
%25 = srem i32 %8, 1000
%26 = mul nsw i32 %25, 1000
%27 = load i32, i32* %4
%28 = add nsw i32 %27, %26
%29 = icmp sgt i32 %28, 1000000
br i1 %29, label %30, label %33
30: ; preds = %20
%31 = add nsw i64 %24, 1
%32 = add nsw i32 %28, -1000000
br label %33
33: ; preds = %20, %30, %18
%34 = phi i32 [ %32, %30 ], [ %6, %18 ], [ %28, %20 ]
%35 = phi i64 [ %31, %30 ], [ %7, %18 ], [ %24, %20 ]
%36 = tail call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%37 = load i64, i64* %3
%38 = sub nsw i64 %35, %37
%39 = trunc i64 %38 to i32
%40 = mul i32 %39, 1000
%41 = load i32, i32* %4
%42 = sub nsw i32 %34, %41
%43 = sdiv i32 %42, 1000
%44 = add nsw i32 %40, %43
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %5
46: ; preds = %33, %5
ret void
}