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

/* Variables and functions */
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int gpio_microsleep(unsigned micros) {
  struct timespec req, rem;
  if (micros > 999999) {
    req.tv_sec = (int)(micros / 1000000);
    req.tv_nsec = (micros - ((long)req.tv_sec * 1000000)) * 1000;
  } else {
    req.tv_sec = 0;
    req.tv_nsec = micros * 1000;
  }
  if (nanosleep(&req, &rem) == 0)
    return 0;
  else
    return rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
}


x86 assembly (clang, -O0):
<gpio_microsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xf423f,-0x8(%rbp)
jbe    1157 <gpio_microsleep+0x47>
mov    -0x8(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
movslq -0x10(%rbp),%rcx
imul   $0xf4240,%rcx,%rcx
sub    %rcx,%rax
imul   $0x3e8,%rax,%rax
mov    %eax,-0xc(%rbp)
jmp    1168 <gpio_microsleep+0x58>
movl   $0x0,-0x10(%rbp)
imul   $0x3e8,-0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jne    118b <gpio_microsleep+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    11ab <gpio_microsleep+0x9b>
imul   $0xf4240,-0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %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):
<gpio_microsleep>:
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
cmp    $0xf4240,%edi
jb     1136 <gpio_microsleep+0x26>
mov    %edi,%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
imul   $0xfff0bdc0,%eax,%ecx
add    %ecx,%edi
jmp    1138 <gpio_microsleep+0x28>
xor    %eax,%eax
imul   $0x3e8,%edi,%ecx
mov    %eax,0x8(%rsp)
mov    %ecx,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     1170 <gpio_microsleep+0x60>
imul   $0xf4240,(%rsp),%eax
mov    0x4(%rsp),%ecx
imul   $0x10624dd3,%rcx,%rbx
shr    $0x26,%rbx
add    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @gpio_microsleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp ugt i32 %6, 999999
br i1 %7, label %8, label %22
8: ; preds = %1
%9 = load i32, i32* %3
%10 = udiv i32 %9, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* %3
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = sext i32 %15 to i64
%17 = mul nsw i64 %16, 1000000
%18 = sub nsw i64 %13, %17
%19 = mul nsw i64 %18, 1000
%20 = trunc i64 %19 to i32
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %20, i32* %21
br label %27
22: ; preds = %1
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 0, i32* %23
%24 = load i32, i32* %3
%25 = mul i32 %24, 1000
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %25, i32* %26
br label %27
27: ; preds = %22, %8
%28 = call i64 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 0, i32* %2
br label %39
31: ; preds = %27
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%33 = load i32, i32* %32
%34 = mul nsw i32 %33, 1000000
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%36 = load i32, i32* %35
%37 = udiv i32 %36, 1000
%38 = add i32 %34, %37
store i32 %38, i32* %2
br label %39
39: ; preds = %31, %30
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @gpio_microsleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = icmp ugt i32 %0, 999999
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = udiv i32 %0, 1000000
%9 = mul i32 %8, -1000000
%10 = add i32 %9, %0
br label %11
11: ; preds = %1, %7
%12 = phi i32 [ %8, %7 ], [ 0, %1 ]
%13 = phi i32 [ %10, %7 ], [ %0, %1 ]
%14 = mul i32 %13, 1000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %12, i32* %15
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %14, i32* %16
%17 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %11
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 1000000
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%24 = load i32, i32* %23
%25 = udiv i32 %24, 1000
%26 = add i32 %25, %22
br label %27
27: ; preds = %11, %19
%28 = phi i32 [ %26, %19 ], [ 0, %11 ]
ret i32 %28
}