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

/* Variables and functions */
int /*<<< orphan*/ SYS_nanosleep;
int /*<<< orphan*/ isdigit(char const);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ sscanf(char const *, char *, double *, char *);
int strlen(char const *);
int syscall(int /*<<< orphan*/, struct timespec *, int /*<<< orphan*/ *);
int gen_nanosleep(int argc, char *argv[]) {
  struct timespec req;
  const char *arg = argv[2];
  req.tv_sec = 0;
  req.tv_nsec = 100;
  if (argc > 2) {
    if (!isdigit(*arg) || !isdigit(arg[strlen(arg) - 1])) {
      printf("Invalid argument: %s; the argument should be a non-negative "
             "number with no sign\n",
             arg);
      return 1;
    }
    double time;
    char tail = '\0';
    sscanf(arg, "%le%c", &time, &tail);
    if (tail != '\0') {
      printf("Argument '%s' should only contain numerial charactors\n", arg);
      return 1;
    }
    if (time > 0 && time < 1) {
      time = 1;
    }
    req.tv_sec = (int)(time / 1e9);
    req.tv_nsec = (int)(time - req.tv_sec * 1e9);
  }
  int r = syscall(SYS_nanosleep, &req, NULL);
  if (r) {
    perror("Error in syscall nanosleep");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gen_nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x64,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    12ba <gen_nanosleep+0x15a>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
je     11d7 <gen_nanosleep+0x77>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    11f3 <gen_nanosleep+0x93>
mov    -0x20(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fc <gen_nanosleep+0x19c>
movb   $0x0,-0x29(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe5f(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x29(%rbp),%rcx
call   1070 <sscanf@plt>
movsbl -0x29(%rbp),%eax
cmp    $0x0,%eax
je     1238 <gen_nanosleep+0xd8>
mov    -0x20(%rbp),%rsi
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fc <gen_nanosleep+0x19c>
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    126a <gen_nanosleep+0x10a>
movsd  0xdae(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    126a <gen_nanosleep+0x10a>
movsd  0xd9b(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xd91(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x18(%rbp)
movsd  -0x28(%rbp),%xmm1
cvtsi2sdl -0x18(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  0xd5d(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <syscall@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     12f5 <gen_nanosleep+0x195>
lea    0xdba(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fc <gen_nanosleep+0x19c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_nanosleep>:
push   %rbx
sub    $0x20,%rsp
mov    0x10(%rsi),%rbx
movabs $0x6400000000,%rax
mov    %rax,0x10(%rsp)
cmp    $0x3,%edi
jl     1240 <gen_nanosleep+0xe0>
movsbl (%rbx),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11d3 <gen_nanosleep+0x73>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movsbl -0x1(%rax,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11d3 <gen_nanosleep+0x73>
movb   $0x0,0xf(%rsp)
lea    0xeb0(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0xf(%rsp),%rcx
mov    %rbx,%rdi
call   1070 <sscanf@plt>
cmpb   $0x0,0xf(%rsp)
je     11e9 <gen_nanosleep+0x89>
lea    0xe96(%rip),%rdi
jmp    11da <gen_nanosleep+0x7a>
lea    0xe36(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    $0x1,%ebx
jmp    125b <gen_nanosleep+0xfb>
movsd  0x18(%rsp),%xmm0
movsd  0xe09(%rip),%xmm1
maxsd  %xmm0,%xmm1
xorpd  %xmm2,%xmm2
cmpltsd %xmm0,%xmm2
andpd  %xmm2,%xmm1
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
movsd  0xdf0(%rip),%xmm0
movapd %xmm2,%xmm1
divsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
cvttpd2dq %xmm1,%xmm1
mov    %eax,0x10(%rsp)
cvtdq2pd %xmm1,%xmm1
mulsd  %xmm0,%xmm1
subsd  %xmm1,%xmm2
cvttsd2si %xmm2,%eax
mov    %eax,0x14(%rsp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1060 <syscall@plt>
test   %eax,%eax
jne    1263 <gen_nanosleep+0x103>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
jmp    11e2 <gen_nanosleep+0x82>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [81 x i8] c"Invalid argument: %s; the argument should be a non-negative number with no sign\0A\00"
@.str.1 = [6 x i8] c"%le%c\00"
@.str.2 = [55 x i8] c"Argument '%s' should only contain numerial charactors\0A\00"
@SYS_nanosleep = global i32 0
@.str.3 = [27 x i8] c"Error in syscall nanosleep\00"
define i32 @gen_nanosleep(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca i8*
%8 = alloca double
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 2
%13 = load i8*, i8** %12
store i8* %13, i8** %7
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i32 100, i32* %15
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %65
18: ; preds = %2
%19 = load i8*, i8** %7
%20 = load i8, i8* %19
%21 = call i32 @isdigit(i8 signext %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* %25)
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %24, i64 %28
%30 = load i8, i8* %29
%31 = call i32 @isdigit(i8 signext %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %23, %18
%34 = load i8*, i8** %7
%35 = call i32 @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i64 0, i64 0), i8* %34)
store i32 1, i32* %3
br label %73
36: ; preds = %23
store i8 0, i8* %9
%37 = load i8*, i8** %7
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double* %8, i8* %9)
%39 = load i8, i8* %9
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i8*, i8** %7
%44 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %43)
store i32 1, i32* %3
br label %73
45: ; preds = %36
%46 = load double, double* %8
%47 = fcmp ogt double %46, 0.000000e+00
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load double, double* %8
%50 = fcmp olt double %49, 1.000000e+00
br i1 %50, label %51, label %52
51: ; preds = %48
store double 1.000000e+00, double* %8
br label %52
52: ; preds = %51, %48, %45
%53 = load double, double* %8
%54 = fdiv double %53, 1.000000e+09
%55 = fptosi double %54 to i32
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 %55, i32* %56
%57 = load double, double* %8
%58 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%59 = load i32, i32* %58
%60 = sitofp i32 %59 to double
%61 = fneg double %60
%62 = call double @llvm.fmuladd.f64(double %61, double 1.000000e+09, double %57)
%63 = fptosi double %62 to i32
%64 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i32 %63, i32* %64
br label %65
65: ; preds = %52, %2
%66 = load i32, i32* @SYS_nanosleep
%67 = call i32 @syscall(i32 %66, %struct.timespec* %6, i32* null)
store i32 %67, i32* %10
%68 = load i32, i32* %10
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %73
72: ; preds = %65
store i32 0, i32* %3
br label %73
73: ; preds = %72, %70, %42, %33
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [81 x i8] c"Invalid argument: %s; the argument should be a non-negative number with no sign\0A\00"
@.str.1 = [6 x i8] c"%le%c\00"
@.str.2 = [55 x i8] c"Argument '%s' should only contain numerial charactors\0A\00"
@SYS_nanosleep = global i32 0
@.str.3 = [27 x i8] c"Error in syscall nanosleep\00"
define i32 @gen_nanosleep(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca double
%5 = alloca i8
%6 = bitcast %struct.timespec* %3 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 100, i32* %10
%11 = icmp sgt i32 %0, 2
br i1 %11, label %12, label %45
12: ; preds = %2
%13 = load i8, i8* %8
%14 = tail call i32 @isdigit(i8 signext %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = tail call i32 @strlen(i8* %8)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %8, i64 %19
%21 = load i8, i8* %20
%22 = tail call i32 @isdigit(i8 signext %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16, %12
%25 = tail call i32 @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %51
26: ; preds = %16
%27 = bitcast double* %4 to i8*
store i8 0, i8* %5
%28 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double* %4, i8* %5)
%29 = load i8, i8* %5
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %43
31: ; preds = %26
%32 = load double, double* %4
%33 = fcmp ogt double %32, 0.000000e+00
%34 = fcmp olt double %32, 1.000000e+00
%35 = and i1 %33, %34
%36 = select i1 %35, double 1.000000e+00, double %32
%37 = fdiv double %36, 1.000000e+09
%38 = fptosi double %37 to i32
store i32 %38, i32* %9
%39 = sitofp i32 %38 to double
%40 = fneg double %39
%41 = call double @llvm.fmuladd.f64(double %40, double 1.000000e+09, double %36)
%42 = fptosi double %41 to i32
store i32 %42, i32* %10
br label %45
43: ; preds = %26
%44 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %51
45: ; preds = %31, %2
%46 = load i32, i32* @SYS_nanosleep
%47 = call i32 @syscall(i32 %46, %struct.timespec* %3, i32* null)
%48 = icmp eq i32 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %43, %49, %45, %24
%52 = phi i32 [ 1, %43 ], [ 1, %24 ], [ 1, %49 ], [ 0, %45 ]
ret i32 %52
}