#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 /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int sscanf(char *, char *, unsigned int *, int *);
unsigned int strlen(char *);
int scanstr_serial(const char **str, char *buf) {
  int count;
  unsigned int len;
  if (sscanf(buf, "%u\"%n", &len, &count) < 1)
    goto invalid;
  buf += count;
  if (len == 0 && *buf == '0') {
    len = 1;
    *str = NULL;
  } else if (len < strlen(buf) && buf[len] == '\"') {
    buf[len] = '\0';
    *str = buf;
  } else
    goto invalid;
  return count + len + 1;
invalid:
  errno = EINVAL;
  return -1;
}


x86 assembly (clang, -O0):
<scanstr_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xec5(%rip),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jge    1156 <scanstr_serial+0x36>
jmp    11fe <scanstr_serial+0xde>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1198 <scanstr_serial+0x78>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1198 <scanstr_serial+0x78>
movl   $0x1,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    11ed <scanstr_serial+0xcd>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jae    11e3 <scanstr_serial+0xc3>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x22,%eax
jne    11e3 <scanstr_serial+0xc3>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11e8 <scanstr_serial+0xc8>
jmp    11fe <scanstr_serial+0xde>
jmp    11ed <scanstr_serial+0xcd>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <scanstr_serial+0xf7>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanstr_serial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xecb(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %rbx,%rdi
call   1040 <sscanf@plt>
test   %eax,%eax
jle    1191 <scanstr_serial+0x71>
movslq 0xc(%rsp),%rax
add    %rax,%rbx
mov    0x8(%rsp),%ebp
test   %ebp,%ebp
jne    1169 <scanstr_serial+0x49>
cmpb   $0x30,(%rbx)
jne    1169 <scanstr_serial+0x49>
mov    $0x1,%ecx
xor    %ebx,%ebx
jmp    1187 <scanstr_serial+0x67>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %eax,%ebp
jae    1191 <scanstr_serial+0x71>
mov    0x8(%rsp),%ecx
cmpb   $0x22,(%rbx,%rcx,1)
jne    1191 <scanstr_serial+0x71>
movb   $0x0,(%rbx,%rcx,1)
mov    0xc(%rsp),%eax
mov    %rbx,(%r14)
add    %ecx,%eax
add    $0x1,%eax
jmp    11a8 <scanstr_serial+0x88>
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    0x2e3f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%u\22%n\00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @scanstr_serial(i8** nocapture writeonly %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %4, i32* %3)
%8 = icmp slt i32 %7, 1
br i1 %8, label %35, label %9
9: ; preds = %2
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i8, i8* %12
%17 = icmp eq i8 %16, 48
br i1 %17, label %29, label %18
18: ; preds = %15, %9
%19 = call i32 @strlen(i8* %12)
%20 = icmp ult i32 %13, %19
br i1 %20, label %21, label %35
21: ; preds = %18
%22 = load i32, i32* %4
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %12, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 34
br i1 %26, label %27, label %35
27: ; preds = %21
store i8 0, i8* %24
%28 = load i32, i32* %3
br label %29
29: ; preds = %15, %27
%30 = phi i32 [ %22, %27 ], [ 1, %15 ]
%31 = phi i32 [ %28, %27 ], [ %10, %15 ]
%32 = phi i8* [ %12, %27 ], [ null, %15 ]
store i8* %32, i8** %0
%33 = add i32 %31, 1
%34 = add i32 %33, %30
br label %37
35: ; preds = %18, %21, %2
%36 = load i32, i32* @EINVAL
store i32 %36, i32* @errno
br label %37
37: ; preds = %35, %29
%38 = phi i32 [ -1, %35 ], [ %34, %29 ]
ret i32 %38
}