#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 sscanf(char const *, char *, long *, char *);
char *strchr(char *, int);
int /*<<< orphan*/ strtrm(char *, int, char const *);
long continuation_depth(const char *key) {
  char keybuf[64 + 1];
  const char *ps;
  long tmp, number = 0l;
  int numc = 0;
  char rest;
  if (key == (char *)NULL)
    keybuf[0] = '\0';
  else
    strtrm(keybuf, 64 + 1, key);
  ps = strchr(keybuf, (int)'~');
  if (ps) {
    numc = sscanf(++ps, "%ld%c", &tmp, &rest);
    if ((numc == 1) && (tmp > 0))
      number = tmp;
    else
      number = -1l;
  }
  return (number);
}


x86 assembly (clang, -O0):
<continuation_depth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x68(%rbp)
movl   $0x0,-0x6c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    115f <continuation_depth+0x2f>
movb   $0x0,-0x50(%rbp)
jmp    1171 <continuation_depth+0x41>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x41,%esi
call   1040 <strtrm@plt>
lea    -0x50(%rbp),%rdi
mov    $0x7e,%esi
call   1030 <strchr@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     11e0 <continuation_depth+0xb0>
mov    -0x58(%rbp),%rdi
add    $0x1,%rdi
mov    %rdi,-0x58(%rbp)
lea    0xe5f(%rip),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x6d(%rbp),%rcx
call   1050 <sscanf@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x1,-0x6c(%rbp)
jne    11d3 <continuation_depth+0xa3>
cmpq   $0x0,-0x60(%rbp)
jle    11d3 <continuation_depth+0xa3>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    11db <continuation_depth+0xab>
movq   $0xffffffffffffffff,-0x68(%rbp)
ff
jmp    11e0 <continuation_depth+0xb0>
mov    -0x68(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<continuation_depth>:
sub    $0x58,%rsp
test   %rdi,%rdi
je     114d <continuation_depth+0x1d>
mov    %rdi,%rdx
lea    0x10(%rsp),%rdi
mov    $0x41,%esi
call   1040 <strtrm@plt>
jmp    1152 <continuation_depth+0x22>
movb   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x7e,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     119f <continuation_depth+0x6f>
add    $0x1,%rax
lea    0xe8f(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x7(%rsp),%rcx
mov    %rax,%rdi
call   1050 <sscanf@plt>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
mov    $0xffffffffffffffff,%rdx
cmovle %rdx,%rcx
cmp    $0x1,%eax
cmovne %rdx,%rcx
jmp    11a1 <continuation_depth+0x71>
xor    %ecx,%ecx
mov    %rcx,%rax
add    $0x58,%rsp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%ld%c\00"
define i64 @continuation_depth(i8* %0) {
%2 = alloca i8*
%3 = alloca [65 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %2
store i64 0, i64* %6
store i32 0, i32* %7
%9 = load i8*, i8** %2
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
store i8 0, i8* %12
br label %17
13: ; preds = %1
%14 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%15 = load i8*, i8** %2
%16 = call i32 @strtrm(i8* %14, i32 65, i8* %15)
br label %17
17: ; preds = %13, %11
%18 = getelementptr inbounds [65 x i8], [65 x i8]* %3, i64 0, i64 0
%19 = call i8* @strchr(i8* %18, i32 126)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %35
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
%25 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64* %5, i8* %8)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i64, i64* %5
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = load i64, i64* %5
store i64 %32, i64* %6
br label %34
33: ; preds = %28, %22
store i64 -1, i64* %6
br label %34
34: ; preds = %33, %31
br label %35
35: ; preds = %34, %17
%36 = load i64, i64* %6
ret i64 %36
}

LLVM IR (-O2):
@.str = [6 x i8] c"%ld%c\00"
define i64 @continuation_depth(i8* %0) {
%2 = alloca [65 x i8]
%3 = alloca i64
%4 = alloca i8
%5 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0
%6 = bitcast i64* %3 to i8*
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8 0, i8* %5
br label %11
9: ; preds = %1
%10 = call i32 @strtrm(i8* %5, i32 65, i8* %0)
br label %11
11: ; preds = %9, %8
%12 = call i8* @strchr(i8* %5, i32 126)
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64* %3, i8* %4)
%17 = icmp eq i32 %16, 1
%18 = load i64, i64* %3
%19 = icmp sgt i64 %18, 0
%20 = select i1 %17, i1 %19, i1 false
%21 = select i1 %20, i64 %18, i64 -1
br label %22
22: ; preds = %14, %11
%23 = phi i64 [ 0, %11 ], [ %21, %14 ]
ret i64 %23
}