#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(int, char);
int sscanf(char *, char *, ...);
int parse_duration(char *durp) {
  char duration_spec;
  int advance, ret, sleep_duration, tmp_duration;
  sleep_duration = tmp_duration = 0;
  ret = sscanf(durp, "%9d%n", &tmp_duration, &advance);
  if (ret != 1)
    errx(EX_DATAERR,
         "duration must be a 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 = tmp_duration;
  } else if (ret == 1) {
    durp += advance;
    sleep_duration += spec2secs(tmp_duration, duration_spec);
    while ((ret = sscanf(durp, "%9d%1c%n", &tmp_duration, &duration_spec,
                         &advance)) == 2) {
      durp += advance;
      sleep_duration += spec2secs(tmp_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,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9b(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x10(%rbp),%rcx
mov    $0x0,%al
call   1050 <sscanf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     1196 <parse_duration+0x56>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%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    0xe9b(%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     11e0 <parse_duration+0xa0>
mov    -0x14(%rbp),%eax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%eax
jne    11eb <parse_duration+0xab>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    129c <parse_duration+0x15c>
cmpl   $0x1,-0x14(%rbp)
jne    127d <parse_duration+0x13d>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%edi
movsbl -0x9(%rbp),%esi
call   1060 <spec2secs@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe20(%rip),%rsi
lea    -0x1c(%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    1278 <parse_duration+0x138>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%edi
movsbl -0x9(%rbp),%esi
call   1060 <spec2secs@plt>
mov    %rax,%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
jmp    121f <parse_duration+0xdf>
jmp    1297 <parse_duration+0x157>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
jmp    129c <parse_duration+0x15c>
cmpl   $0x1,-0x18(%rbp)
jge    12bd <parse_duration+0x17d>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x18(%rbp),%eax
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,%r14
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,%r14
lea    0xeb3(%rip),%rsi
lea    0xf(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
test   %eax,%eax
je     1269 <parse_duration+0x129>
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%eax
je     1269 <parse_duration+0x129>
cmp    $0x1,%eax
jne    1273 <parse_duration+0x133>
movslq 0x14(%rsp),%rax
add    %rax,%r14
mov    0x10(%rsp),%edi
movsbl 0xf(%rsp),%esi
call   1060 <spec2secs@plt>
mov    %rax,%rbx
lea    0xe5c(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0xf(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <sscanf@plt>
cmp    $0x2,%eax
jne    126d <parse_duration+0x12d>
lea    0xe37(%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,%r14
mov    0x10(%rsp),%edi
movsbl 0xf(%rsp),%esi
call   1060 <spec2secs@plt>
movslq %ebx,%rbx
add    %rax,%rbx
mov    %r14,%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    126d <parse_duration+0x12d>
mov    0x10(%rsp),%ebx
test   %ebx,%ebx
jg     12a5 <parse_duration+0x165>
jmp    128e <parse_duration+0x14e>
mov    0x2d56(%rip),%rcx
mov    (%rcx),%edi
lea    0xdd0(%rip),%rsi
xor    %ebx,%ebx
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    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%9d%n\00"
@EX_DATAERR = global i32 0
@.str.1 = [68 x i8] c"duration must be a 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"%9d%1c%n\00"
@EX_SOFTWARE = global i32 0
@.str.4 = [43 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 i32 @parse_duration(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %7
store i32 0, i32* %6
%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* %7, 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 ([68 x i8], [68 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 %30
28: ; preds = %24, %15
%29 = load i32, i32* %7
store i32 %29, i32* %6
br label %67
30: ; preds = %24
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %62
33: ; preds = %30
%34 = load i32, i32* %4
%35 = load i8*, i8** %2
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %2
%38 = load i32, i32* %7
%39 = load i8, i8* %3
%40 = call i64 @spec2secs(i32 %38, i8 signext %39)
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %6
br label %45
45: ; preds = %49, %33
%46 = load i8*, i8** %2
%47 = call i32 (i8*, i8*, ...) @sscanf(i8* %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %7, i8* %3, i32* %4)
store i32 %47, i32* %5
%48 = icmp eq i32 %47, 2
br i1 %48, label %49, label %61
49: ; preds = %45
%50 = load i32, i32* %4
%51 = load i8*, i8** %2
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8* %53, i8** %2
%54 = load i32, i32* %7
%55 = load i8, i8* %3
%56 = call i64 @spec2secs(i32 %54, i8 signext %55)
%57 = load i32, i32* %6
%58 = sext i32 %57 to i64
%59 = add nsw i64 %58, %56
%60 = trunc i64 %59 to i32
store i32 %60, i32* %6
br label %45
61: ; preds = %45
br label %66
62: ; preds = %30
%63 = load i32, i32* @EX_SOFTWARE
%64 = load i32, i32* %5
%65 = call i32 (i32, i8*, ...) @err(i32 %63, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %62, %61
br label %67
67: ; preds = %66, %28
%68 = load i32, i32* %6
%69 = icmp slt i32 %68, 1
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32, i32* @EX_DATAERR
%72 = call i32 (i32, i8*, ...) @err(i32 %71, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %73
73: ; preds = %70, %67
%74 = load i32, i32* %6
ret i32 %74
}

LLVM IR (-O2):
@.str = [6 x i8] c"%9d%n\00"
@EX_DATAERR = global i32 0
@.str.1 = [68 x i8] c"duration must be a 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"%9d%1c%n\00"
@EX_SOFTWARE = global i32 0
@.str.4 = [43 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 i32 @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 ([68 x i8], [68 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 %23
21: ; preds = %12
%22 = load i32, i32* %4
br label %54
23: ; preds = %12
%24 = icmp eq i32 %16, 1
br i1 %24, label %25, label %48
25: ; preds = %23
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %15, i64 %27
%29 = load i32, i32* %4
%30 = load i8, i8* %2
%31 = call i64 @spec2secs(i32 %29, i8 signext %30)
%32 = call i32 (i8*, i8*, ...) @sscanf(i8* %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %4, i8* %2, i32* %3)
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %51
34: ; preds = %25, %34
%35 = phi i64 [ %45, %34 ], [ %31, %25 ]
%36 = phi i8* [ %39, %34 ], [ %28, %25 ]
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %4
%41 = load i8, i8* %2
%42 = call i64 @spec2secs(i32 %40, i8 signext %41)
%43 = shl i64 %35, 32
%44 = ashr exact i64 %43, 32
%45 = add nsw i64 %42, %44
%46 = call i32 (i8*, i8*, ...) @sscanf(i8* %39, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %4, i8* %2, i32* %3)
%47 = icmp eq i32 %46, 2
br i1 %47, label %34, label %51
48: ; preds = %23
%49 = load i32, i32* @EX_SOFTWARE
%50 = call i32 (i32, i8*, ...) @err(i32 %49, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %16)
br label %57
51: ; preds = %34, %25
%52 = phi i64 [ %31, %25 ], [ %45, %34 ]
%53 = trunc i64 %52 to i32
br label %54
54: ; preds = %51, %21
%55 = phi i32 [ %22, %21 ], [ %53, %51 ]
%56 = icmp slt i32 %55, 1
br i1 %56, label %57, label %61
57: ; preds = %48, %54
%58 = phi i32 [ 0, %48 ], [ %55, %54 ]
%59 = load i32, i32* @EX_DATAERR
%60 = call i32 (i32, i8*, ...) @err(i32 %59, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %57, %54
%62 = phi i32 [ %58, %57 ], [ %55, %54 ]
ret i32 %62
}