#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_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int clock_getres(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int get_clock_precision(clockid_t clk_id) {
  struct timespec res;
  if (clock_getres(clk_id, &res) == -1) {
    perror(NULL);
    exit(1);
  }
  if (res.tv_sec)
    return 0;
  if (res.tv_nsec >= 100000000)
    return 1;
  if (res.tv_nsec >= 10000000)
    return 2;
  if (res.tv_nsec >= 1000000)
    return 3;
  if (res.tv_nsec >= 100000)
    return 4;
  if (res.tv_nsec >= 10000)
    return 5;
  if (res.tv_nsec >= 1000)
    return 6;
  if (res.tv_nsec >= 100)
    return 7;
  if (res.tv_nsec >= 10)
    return 8;
  return 9;
}


x86 assembly (clang, -O0):
<get_clock_precision>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_getres@plt>
cmp    $0xffffffff,%eax
jne    1163 <get_clock_precision+0x33>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     117a <get_clock_precision+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0x5f5e100,-0x18(%rbp)
jl     1193 <get_clock_precision+0x63>
movl   $0x1,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0x989680,-0x18(%rbp)
jl     11ac <get_clock_precision+0x7c>
movl   $0x2,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0xf4240,-0x18(%rbp)
jl     11c5 <get_clock_precision+0x95>
movl   $0x3,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0x186a0,-0x18(%rbp)
jl     11de <get_clock_precision+0xae>
movl   $0x4,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0x2710,-0x18(%rbp)
jl     11f7 <get_clock_precision+0xc7>
movl   $0x5,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0x3e8,-0x18(%rbp)
jl     1210 <get_clock_precision+0xe0>
movl   $0x6,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0x64,-0x18(%rbp)
jl     1226 <get_clock_precision+0xf6>
movl   $0x7,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
cmpl   $0xa,-0x18(%rbp)
jl     123c <get_clock_precision+0x10c>
movl   $0x8,-0x4(%rbp)
jmp    1243 <get_clock_precision+0x113>
movl   $0x9,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_clock_precision>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rsi
call   1030 <clock_getres@plt>
cmp    $0xffffffff,%eax
je     11be <get_clock_precision+0x8e>
xor    %eax,%eax
cmpq   $0x0,0x10(%rsp)
je     1152 <get_clock_precision+0x22>
add    $0x18,%rsp
ret
mov    0x8(%rsp),%ecx
mov    $0x1,%eax
cmp    $0x5f5e0ff,%ecx
jg     114d <get_clock_precision+0x1d>
mov    $0x2,%eax
cmp    $0x98967f,%ecx
jg     114d <get_clock_precision+0x1d>
mov    $0x3,%eax
cmp    $0xf423f,%ecx
jg     114d <get_clock_precision+0x1d>
mov    $0x4,%eax
cmp    $0x1869f,%ecx
jg     114d <get_clock_precision+0x1d>
mov    $0x5,%eax
cmp    $0x270f,%ecx
jg     114d <get_clock_precision+0x1d>
mov    $0x6,%eax
cmp    $0x3e7,%ecx
jg     114d <get_clock_precision+0x1d>
mov    $0x7,%eax
cmp    $0x63,%ecx
jg     114d <get_clock_precision+0x1d>
xor    %eax,%eax
cmp    $0xa,%ecx
setge  %al
xor    $0x9,%eax
add    $0x18,%rsp
ret
xor    %edi,%edi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
define i32 @get_clock_precision(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @clock_getres(i32 %5, %struct.timespec* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 @perror(i32* null)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%13 = load i64, i64* %12
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 0, i32* %2
br label %57
16: ; preds = %11
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp sge i32 %18, 100000000
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 1, i32* %2
br label %57
21: ; preds = %16
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp sge i32 %23, 10000000
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 2, i32* %2
br label %57
26: ; preds = %21
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = icmp sge i32 %28, 1000000
br i1 %29, label %30, label %31
30: ; preds = %26
store i32 3, i32* %2
br label %57
31: ; preds = %26
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp sge i32 %33, 100000
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 4, i32* %2
br label %57
36: ; preds = %31
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%38 = load i32, i32* %37
%39 = icmp sge i32 %38, 10000
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 5, i32* %2
br label %57
41: ; preds = %36
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = icmp sge i32 %43, 1000
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 6, i32* %2
br label %57
46: ; preds = %41
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = icmp sge i32 %48, 100
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 7, i32* %2
br label %57
51: ; preds = %46
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%53 = load i32, i32* %52
%54 = icmp sge i32 %53, 10
br i1 %54, label %55, label %56
55: ; preds = %51
store i32 8, i32* %2
br label %57
56: ; preds = %51
store i32 9, i32* %2
br label %57
57: ; preds = %56, %55, %50, %45, %40, %35, %30, %25, %20, %15
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
define i32 @get_clock_precision(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = call i32 @clock_getres(i32 %0, %struct.timespec* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 @perror(i32* null)
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%11 = load i64, i64* %10
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %32
13: ; preds = %9
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = icmp sgt i32 %15, 99999999
br i1 %16, label %32, label %17
17: ; preds = %13
%18 = icmp sgt i32 %15, 9999999
br i1 %18, label %32, label %19
19: ; preds = %17
%20 = icmp sgt i32 %15, 999999
br i1 %20, label %32, label %21
21: ; preds = %19
%22 = icmp sgt i32 %15, 99999
br i1 %22, label %32, label %23
23: ; preds = %21
%24 = icmp sgt i32 %15, 9999
br i1 %24, label %32, label %25
25: ; preds = %23
%26 = icmp sgt i32 %15, 999
br i1 %26, label %32, label %27
27: ; preds = %25
%28 = icmp sgt i32 %15, 99
br i1 %28, label %32, label %29
29: ; preds = %27
%30 = icmp sgt i32 %15, 9
%31 = select i1 %30, i32 8, i32 9
br label %32
32: ; preds = %29, %27, %25, %23, %21, %19, %17, %13, %9
%33 = phi i32 [ 0, %9 ], [ 1, %13 ], [ 2, %17 ], [ 3, %19 ], [ 4, %21 ], [ 5, %23 ], [ 6, %25 ], [ 7, %27 ], [ %31, %29 ]
ret i32 %33
}