#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 */
scalar_t__ strcmp(char *, char const *);
scalar_t__ strncmp(char const *, char const *, int);
char *strstr(char const *, char *);
int strtol(char const *, char **, int);
int gwyfile_extract_id(const char *s, const char *template) {
  const char *p = strstr(template, "%d");
  char *end;
  int id;
  if (!p)
    return -1;
  if (strncmp(s, template, p - template))
    return -1;
  id = strtol(s + (p - template), &end, 10);
  if ((const char *)end == s + (p - template))
    return -1;
  if (strcmp(end, p + 2))
    return -1;
  return id;
}


x86 assembly (clang, -O0):
<gwyfile_extract_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117b <gwyfile_extract_id+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <gwyfile_extract_id+0xe6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11ab <gwyfile_extract_id+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <gwyfile_extract_id+0xe6>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rsi
sub    %rsi,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jne    11f9 <gwyfile_extract_id+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <gwyfile_extract_id+0xe6>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x2,%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     1220 <gwyfile_extract_id+0xe0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <gwyfile_extract_id+0xe6>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gwyfile_extract_id>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strstr@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11ba <gwyfile_extract_id+0x7a>
mov    %rax,%r13
mov    %rax,%rbx
sub    %rbp,%rbx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11ba <gwyfile_extract_id+0x7a>
add    %rbx,%r12
mov    %rsp,%rsi
mov    %r12,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    (%rsp),%rdi
cmp    %r12,%rdi
je     11ba <gwyfile_extract_id+0x7a>
mov    %eax,%r15d
add    $0x2,%r13
mov    %r13,%rsi
call   1040 <strcmp@plt>
neg    %rax
sbb    %r14d,%r14d
or     %r15d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i32 @gwyfile_extract_id(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i8* @strstr(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %54
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = load i8*, i8** %5
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = call i64 @strncmp(i8* %15, i8* %16, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %14
store i32 -1, i32* %3
br label %54
26: ; preds = %14
%27 = load i8*, i8** %4
%28 = load i8*, i8** %6
%29 = load i8*, i8** %5
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = getelementptr inbounds i8, i8* %27, i64 %32
%34 = call i32 @strtol(i8* %33, i8** %7, i32 10)
store i32 %34, i32* %8
%35 = load i8*, i8** %7
%36 = load i8*, i8** %4
%37 = load i8*, i8** %6
%38 = load i8*, i8** %5
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = getelementptr inbounds i8, i8* %36, i64 %41
%43 = icmp eq i8* %35, %42
br i1 %43, label %44, label %45
44: ; preds = %26
store i32 -1, i32* %3
br label %54
45: ; preds = %26
%46 = load i8*, i8** %7
%47 = load i8*, i8** %6
%48 = getelementptr inbounds i8, i8* %47, i64 2
%49 = call i64 @strcmp(i8* %46, i8* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
store i32 -1, i32* %3
br label %54
52: ; preds = %45
%53 = load i32, i32* %8
store i32 %53, i32* %3
br label %54
54: ; preds = %52, %51, %44, %25, %13
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i32 @gwyfile_extract_id(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = tail call i8* @strstr(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = bitcast i8** %3 to i8*
%6 = icmp eq i8* %4, null
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = ptrtoint i8* %4 to i64
%9 = ptrtoint i8* %1 to i64
%10 = sub i64 %8, %9
%11 = trunc i64 %10 to i32
%12 = tail call i64 @strncmp(i8* %0, i8* %1, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %7
%15 = getelementptr inbounds i8, i8* %0, i64 %10
%16 = call i32 @strtol(i8* %15, i8** %3, i32 10)
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, %15
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %4, i64 2
%21 = tail call i64 @strcmp(i8* %17, i8* %20)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i32 %16, i32 -1
br label %24
24: ; preds = %19, %14, %7, %2
%25 = phi i32 [ -1, %2 ], [ -1, %7 ], [ -1, %14 ], [ %23, %19 ]
ret i32 %25
}