#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 */
scalar_t__ errno;
scalar_t__ strchr(char const *, char);
scalar_t__ strcmp(char *, char *);
unsigned long strtoul(char const *, char **, int /*<<< orphan*/);
int set_timespec(struct timespec *timeout, const char *str) {
  char *end = NULL;
  unsigned long ulong;
  if (*str == '\0' || strchr(str, '-'))
    return -1;
  errno = 0;
  ulong = strtoul(str, &end, 0);
  if (errno != 0)
    return -1;
  if (*end == '\0' || strcmp(end, "s") == 0) {
    timeout->tv_sec = ulong;
    timeout->tv_nsec = 0;
  } else if (strcmp(end, "ms") == 0) {
    timeout->tv_sec = ulong / 1000;
    timeout->tv_nsec = (ulong % 1000) * 1000;
  } else if (strcmp(end, "us") == 0) {
    timeout->tv_sec = ulong / 1000000;
    timeout->tv_nsec = (ulong % 1000000) * 1000;
  } else {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_timespec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1170 <set_timespec+0x40>
mov    -0x18(%rbp),%rdi
mov    $0x2d,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     117c <set_timespec+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <set_timespec+0x195>
mov    0x2e5d(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e3c(%rip),%rax
cmpq   $0x0,(%rax)
je     11ba <set_timespec+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <set_timespec+0x195>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e4 <set_timespec+0xb4>
mov    -0x20(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1200 <set_timespec+0xd0>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
jmp    12be <set_timespec+0x18e>
mov    -0x20(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1254 <set_timespec+0x124>
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
imul   $0x3e8,%rdx,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    12b9 <set_timespec+0x189>
mov    -0x20(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    12a8 <set_timespec+0x178>
mov    -0x28(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
imul   $0x3e8,%rdx,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    12b4 <set_timespec+0x184>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <set_timespec+0x195>
jmp    12b9 <set_timespec+0x189>
jmp    12be <set_timespec+0x18e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_timespec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,0x8(%rsp)
mov    $0xffffffff,%ebp
cmpb   $0x0,(%rsi)
je     123d <set_timespec+0x10d>
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsi,%rdi
mov    $0x2d,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    123d <set_timespec+0x10d>
mov    0x2e6a(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <strtoul@plt>
cmpq   $0x0,(%r15)
jne    123d <set_timespec+0x10d>
mov    %rax,%r14
mov    0x8(%rsp),%r15
cmpb   $0x0,(%r15)
je     1201 <set_timespec+0xd1>
lea    0xe56(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1201 <set_timespec+0xd1>
lea    0xe41(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1209 <set_timespec+0xd9>
lea    0xe30(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    123d <set_timespec+0x10d>
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %r14,%rax
mul    %rcx
shr    $0x12,%rdx
mov    %rdx,(%r12)
imul   $0xf4240,%rdx,%rax
jmp    122c <set_timespec+0xfc>
mov    %r14,(%r12)
xor    %eax,%eax
jmp    1236 <set_timespec+0x106>
mov    %r14,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,(%r12)
imul   $0x3e8,%rdx,%rax
sub    %rax,%r14
imul   $0x3e8,%r14,%rax
mov    %rax,0x8(%r12)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@.str = [2 x i8] c"s\00"
@.str.1 = [3 x i8] c"ms\00"
@.str.2 = [3 x i8] c"us\00"
define i32 @set_timespec(%struct.timespec* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store %struct.timespec* %0, %struct.timespec** %4
store i8* %1, i8** %5
store i8* null, i8** %6
%8 = load i8*, i8** %5
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i64 @strchr(i8* %13, i8 signext 45)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %12, %2
store i32 -1, i32* %3
br label %70
17: ; preds = %12
store i64 0, i64* @errno
%18 = load i8*, i8** %5
%19 = call i64 @strtoul(i8* %18, i8** %6, i32 0)
store i64 %19, i64* %7
%20 = load i64, i64* @errno
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %70
23: ; preds = %17
%24 = load i8*, i8** %6
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %23
%29 = load i8*, i8** %6
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %28, %23
%33 = load i64, i64* %7
%34 = load %struct.timespec*, %struct.timespec** %4
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %34, i32 0, i32 0
store i64 %33, i64* %35
%36 = load %struct.timespec*, %struct.timespec** %4
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %36, i32 0, i32 1
store i64 0, i64* %37
br label %69
38: ; preds = %28
%39 = load i8*, i8** %6
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %38
%43 = load i64, i64* %7
%44 = udiv i64 %43, 1000
%45 = load %struct.timespec*, %struct.timespec** %4
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %45, i32 0, i32 0
store i64 %44, i64* %46
%47 = load i64, i64* %7
%48 = urem i64 %47, 1000
%49 = mul i64 %48, 1000
%50 = load %struct.timespec*, %struct.timespec** %4
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %50, i32 0, i32 1
store i64 %49, i64* %51
br label %68
52: ; preds = %38
%53 = load i8*, i8** %6
%54 = call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %66
56: ; preds = %52
%57 = load i64, i64* %7
%58 = udiv i64 %57, 1000000
%59 = load %struct.timespec*, %struct.timespec** %4
%60 = getelementptr inbounds %struct.timespec, %struct.timespec* %59, i32 0, i32 0
store i64 %58, i64* %60
%61 = load i64, i64* %7
%62 = urem i64 %61, 1000000
%63 = mul i64 %62, 1000
%64 = load %struct.timespec*, %struct.timespec** %4
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %64, i32 0, i32 1
store i64 %63, i64* %65
br label %67
66: ; preds = %52
store i32 -1, i32* %3
br label %70
67: ; preds = %56
br label %68
68: ; preds = %67, %42
br label %69
69: ; preds = %68, %32
store i32 0, i32* %3
br label %70
70: ; preds = %69, %66, %22, %16
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@.str = [2 x i8] c"s\00"
@.str.1 = [3 x i8] c"ms\00"
@.str.2 = [3 x i8] c"us\00"
define i32 @set_timespec(%struct.timespec* nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %42, label %7
7: ; preds = %2
%8 = tail call i64 @strchr(i8* %1, i8 signext 45)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %42
10: ; preds = %7
store i64 0, i64* @errno
%11 = call i64 @strtoul(i8* %1, i8** %3, i32 0)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %42
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18, %14
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i64 %11, i64* %22
br label %39
23: ; preds = %18
%24 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = udiv i64 %11, 1000
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i64 %27, i64* %28
%29 = urem i64 %11, 1000
%30 = mul nuw nsw i64 %29, 1000
br label %39
31: ; preds = %23
%32 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = udiv i64 %11, 1000000
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
store i64 %35, i64* %36
%37 = urem i64 %11, 1000000
%38 = mul nuw nsw i64 %37, 1000
br label %39
39: ; preds = %26, %34, %21
%40 = phi i64 [ 0, %21 ], [ %38, %34 ], [ %30, %26 ]
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
store i64 %40, i64* %41
br label %42
42: ; preds = %39, %31, %10, %2, %7
%43 = phi i32 [ -1, %7 ], [ -1, %2 ], [ -1, %10 ], [ -1, %31 ], [ 0, %39 ]
ret i32 %43
}