#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 */

/* Variables and functions */
int EOF;
int /*<<< orphan*/ EX_DATAERR;
int /*<<< orphan*/ EX_SOFTWARE;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
scalar_t__ spec2secs(unsigned int, char);
int sscanf(char *, char *, ...);
long parse_duration(char *durp) {
  char duration_spec;
  int advance, ret;
  unsigned int duration = 0;
  long sleep_duration = 0;
  ret = sscanf(durp, "%9u%n", &duration, &advance);
  if (ret != 1)
    errx(EX_DATAERR,
         "duration must be positive integer or short form (e.g. 62 or 1m2s)");
  durp += advance;
  ret = sscanf(durp, "%1c%n", &duration_spec, &advance);
  if (ret == 0 || ret == EOF) {
    sleep_duration = duration;
  } else if (ret == 1) {
    durp += advance;
    sleep_duration += spec2secs(duration, duration_spec);
    while ((ret = sscanf(durp, "%9u%1c%n", &duration, &duration_spec,
                         &advance)) == 2) {
      durp += advance;
      sleep_duration += spec2secs(duration, duration_spec);
    }
  } else {
    err(EX_SOFTWARE, "unknown return '%d' while parsing duration??", ret);
  }
  if (sleep_duration < 1)
    err(EX_DATAERR, "duration must work out to a positive integer");
  return sleep_duration;
}


x86 assembly (clang, -O0):
<parse_duration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9a(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x10(%rbp),%rcx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     1197 <parse_duration+0x57>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe98(%rip),%rsi
lea    -0x9(%rbp),%rdx
lea    -0x10(%rbp),%rcx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11e1 <parse_duration+0xa1>
mov    -0x14(%rbp),%eax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%eax
jne    11ed <parse_duration+0xad>
mov    -0x18(%rbp),%eax
mov    %rax,-0x20(%rbp)
jmp    1294 <parse_duration+0x154>
cmpl   $0x1,-0x14(%rbp)
jne    1275 <parse_duration+0x135>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%edi
movsbl -0x9(%rbp),%esi
call   1060 <spec2secs@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe21(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x9(%rbp),%rcx
lea    -0x10(%rbp),%r8
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x2,%eax
jne    1270 <parse_duration+0x130>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%edi
movsbl -0x9(%rbp),%esi
call   1060 <spec2secs@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    121c <parse_duration+0xdc>
jmp    128f <parse_duration+0x14f>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
jmp    1294 <parse_duration+0x154>
cmpq   $0x1,-0x20(%rbp)
jge    12b6 <parse_duration+0x176>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_duration>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movl   $0x0,0x10(%rsp)
lea    0xea0(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x14(%rsp),%rcx
xor    %eax,%eax
call   1050 <sscanf@plt>
cmp    $0x1,%eax
je     118b <parse_duration+0x4b>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1030 <errx@plt>
movslq 0x14(%rsp),%rax
add    %rax,%rbx
lea    0xeb1(%rip),%rsi
lea    0xf(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
test   %eax,%eax
je     1266 <parse_duration+0x126>
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%eax
je     1266 <parse_duration+0x126>
cmp    $0x1,%eax
jne    1272 <parse_duration+0x132>
movslq 0x14(%rsp),%rax
add    %rax,%rbx
mov    0x10(%rsp),%edi
movsbl 0xf(%rsp),%esi
call   1060 <spec2secs@plt>
mov    %rax,%r14
lea    0xe5a(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xf(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
cmp    $0x2,%eax
jne    126b <parse_duration+0x12b>
lea    0xe35(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xf(%rsp),%r13
lea    0x14(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq 0x14(%rsp),%rax
add    %rax,%rbx
mov    0x10(%rsp),%edi
movsbl 0xf(%rsp),%esi
call   1060 <spec2secs@plt>
add    %rax,%r14
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <sscanf@plt>
cmp    $0x2,%eax
je     1230 <parse_duration+0xf0>
jmp    126b <parse_duration+0x12b>
mov    0x10(%rsp),%r14d
test   %r14,%r14
jg     12a5 <parse_duration+0x165>
jmp    128e <parse_duration+0x14e>
mov    0x2d57(%rip),%rcx
mov    (%rcx),%edi
lea    0xdcf(%rip),%rsi
xor    %r14d,%r14d
mov    %eax,%edx
xor    %eax,%eax
call   1040 <err@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
xor    %eax,%eax
call   1040 <err@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%9u%n\00"
@EX_DATAERR = global i32 0
@.str.1 = [66 x i8] c"duration must be positive integer or short form (e.g. 62 or 1m2s)\00"
@.str.2 = [6 x i8] c"%1c%n\00"
@EOF = global i32 0
@.str.3 = [9 x i8] c"%9u%1c%n\00"
@EX_SOFTWARE = global i32 0
@.str.4 = [45 x i8] c"unknown return '%d' while parsing duration??\00"
@.str.5 = [45 x i8] c"duration must work out to a positive integer\00"
define i64 @parse_duration(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %6
store i64 0, i64* %7
%8 = load i8*, i8** %2
%9 = call i32 (i8*, i8*, ...) @sscanf(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %6, i32* %4)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @EX_DATAERR
%14 = call i32 @errx(i32 %13, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* %4
%17 = load i8*, i8** %2
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8* %19, i8** %2
%20 = load i8*, i8** %2
%21 = call i32 (i8*, i8*, ...) @sscanf(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %3, i32* %4)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %15
%25 = load i32, i32* %5
%26 = load i32, i32* @EOF
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24, %15
%29 = load i32, i32* %6
%30 = zext i32 %29 to i64
store i64 %30, i64* %7
br label %64
31: ; preds = %24
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %59
34: ; preds = %31
%35 = load i32, i32* %4
%36 = load i8*, i8** %2
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %2
%39 = load i32, i32* %6
%40 = load i8, i8* %3
%41 = call i64 @spec2secs(i32 %39, i8 signext %40)
%42 = load i64, i64* %7
%43 = add nsw i64 %42, %41
store i64 %43, i64* %7
br label %44
44: ; preds = %48, %34
%45 = load i8*, i8** %2
%46 = call i32 (i8*, i8*, ...) @sscanf(i8* %45, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %6, i8* %3, i32* %4)
store i32 %46, i32* %5
%47 = icmp eq i32 %46, 2
br i1 %47, label %48, label %58
48: ; preds = %44
%49 = load i32, i32* %4
%50 = load i8*, i8** %2
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
store i8* %52, i8** %2
%53 = load i32, i32* %6
%54 = load i8, i8* %3
%55 = call i64 @spec2secs(i32 %53, i8 signext %54)
%56 = load i64, i64* %7
%57 = add nsw i64 %56, %55
store i64 %57, i64* %7
br label %44
58: ; preds = %44
br label %63
59: ; preds = %31
%60 = load i32, i32* @EX_SOFTWARE
%61 = load i32, i32* %5
%62 = call i32 (i32, i8*, ...) @err(i32 %60, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %59, %58
br label %64
64: ; preds = %63, %28
%65 = load i64, i64* %7
%66 = icmp slt i64 %65, 1
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* @EX_DATAERR
%69 = call i32 (i32, i8*, ...) @err(i32 %68, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %70
70: ; preds = %67, %64
%71 = load i64, i64* %7
ret i64 %71
}

LLVM IR (-O2):
@.str = [6 x i8] c"%9u%n\00"
@EX_DATAERR = global i32 0
@.str.1 = [66 x i8] c"duration must be positive integer or short form (e.g. 62 or 1m2s)\00"
@.str.2 = [6 x i8] c"%1c%n\00"
@EOF = global i32 0
@.str.3 = [9 x i8] c"%9u%1c%n\00"
@EX_SOFTWARE = global i32 0
@.str.4 = [45 x i8] c"unknown return '%d' while parsing duration??\00"
@.str.5 = [45 x i8] c"duration must work out to a positive integer\00"
define i64 @parse_duration(i8* nocapture %0) {
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = call i32 (i8*, i8*, ...) @sscanf(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %4, i32* %3)
%8 = icmp eq i32 %7, 1
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* @EX_DATAERR
%11 = call i32 @errx(i32 %10, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %9, %1
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = call i32 (i8*, i8*, ...) @sscanf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32* %3)
%17 = icmp eq i32 %16, 0
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %16, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = load i32, i32* %4
%23 = zext i32 %22 to i64
br label %50
24: ; preds = %12
%25 = icmp eq i32 %16, 1
br i1 %25, label %26, label %47
26: ; preds = %24
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %15, i64 %28
%30 = load i32, i32* %4
%31 = load i8, i8* %2
%32 = call i64 @spec2secs(i32 %30, i8 signext %31)
%33 = call i32 (i8*, i8*, ...) @sscanf(i8* %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %4, i8* %2, i32* %3)
%34 = icmp eq i32 %33, 2
br i1 %34, label %35, label %50
35: ; preds = %26, %35
%36 = phi i64 [ %44, %35 ], [ %32, %26 ]
%37 = phi i8* [ %40, %35 ], [ %29, %26 ]
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i32, i32* %4
%42 = load i8, i8* %2
%43 = call i64 @spec2secs(i32 %41, i8 signext %42)
%44 = add nsw i64 %43, %36
%45 = call i32 (i8*, i8*, ...) @sscanf(i8* %40, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %4, i8* %2, i32* %3)
%46 = icmp eq i32 %45, 2
br i1 %46, label %35, label %50
47: ; preds = %24
%48 = load i32, i32* @EX_SOFTWARE
%49 = call i32 (i32, i8*, ...) @err(i32 %48, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %16)
br label %53
50: ; preds = %35, %26, %21
%51 = phi i64 [ %23, %21 ], [ %32, %26 ], [ %44, %35 ]
%52 = icmp slt i64 %51, 1
br i1 %52, label %53, label %57
53: ; preds = %47, %50
%54 = phi i64 [ 0, %47 ], [ %51, %50 ]
%55 = load i32, i32* @EX_DATAERR
%56 = call i32 (i32, i8*, ...) @err(i32 %55, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %53, %50
%58 = phi i64 [ %54, %53 ], [ %51, %50 ]
ret i64 %58
}