#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 */
typedef int /*<<< orphan*/ RETURN_OK;
typedef int /*<<< orphan*/ ERR_BEGIN;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int strlen(char const *);
scalar_t__ strncmp(char const *, char const *, int);
char *strstr(char const *, char const *);
int altera_parse_q12_response(const char *buf, size_t buflen, const char **p,
                              const char **q) {
  static const char ERR_BEGIN[] = "error {";
  static const char ERR_END[] = "}\n";
  if (strncmp(buf, ERR_BEGIN, sizeof(ERR_BEGIN) - 1) == 0) {
    *p = buf + (sizeof(ERR_BEGIN) - 1);
    *q = strstr(buf, ERR_END);
    if (*q == NULL)
      *q = buf + strlen(buf);
    return (23);
  }
  const char RETURN_OK[] = "return {";
  if (strncmp(RETURN_OK, buf, sizeof(RETURN_OK) - 1) != 0) {
    fprintf(stderr, "Did not get \"return\" reply: '%s'\n", buf);
    return (15);
  }
  *q = strstr(buf, "}\n");
  if (*q == NULL) {
    return (22);
  }
  *p = strstr(buf, "{");
  if (*p == NULL) {
    fprintf(stderr, "no '{'\n");
    return (15);
  }
  (*p) += 1;
  return (0);
}


x86 assembly (clang, -O0):
<altera_parse_q12_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9d(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11db <altera_parse_q12_response+0x9b>
mov    -0x10(%rbp),%rcx
add    $0x7,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11cf <altera_parse_q12_response+0x8f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
cltq
add    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x17,-0x4(%rbp)
jmp    12c6 <altera_parse_q12_response+0x186>
mov    0xe29(%rip),%rax
mov    %rax,-0x31(%rbp)
mov    0xe27(%rip),%al
mov    %al,-0x29(%rbp)
lea    -0x31(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1232 <altera_parse_q12_response+0xf2>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xf,-0x4(%rbp)
jmp    12c6 <altera_parse_q12_response+0x186>
mov    -0x10(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1266 <altera_parse_q12_response+0x126>
movl   $0x16,-0x4(%rbp)
jmp    12c6 <altera_parse_q12_response+0x186>
mov    -0x10(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12b1 <altera_parse_q12_response+0x171>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xf,-0x4(%rbp)
jmp    12c6 <altera_parse_q12_response+0x186>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<altera_parse_q12_response>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%rbx
lea    0xe96(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11c5 <altera_parse_q12_response+0x75>
movabs $0x7b206e7275746572,%rax
6e 20 7b
mov    %rax,0x8(%rsp)
movb   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11fa <altera_parse_q12_response+0xaa>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xf,%ebp
jmp    1236 <altera_parse_q12_response+0xe6>
lea    0x7(%rbx),%rax
mov    %rax,(%r15)
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
mov    %rax,(%r14)
mov    $0x17,%ebp
test   %rax,%rax
jne    1236 <altera_parse_q12_response+0xe6>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
add    %rbx,%rax
mov    %rax,(%r14)
jmp    1236 <altera_parse_q12_response+0xe6>
lea    0xe07(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1231 <altera_parse_q12_response+0xe1>
mov    %rbx,%rdi
mov    $0x7b,%esi
call   1050 <strchr@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1243 <altera_parse_q12_response+0xf3>
add    $0x1,%rax
mov    %rax,(%r15)
xor    %ebp,%ebp
jmp    1236 <altera_parse_q12_response+0xe6>
mov    $0x16,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11be <altera_parse_q12_response+0x6e>

LLVM IR (-O0):
@altera_parse_q12_response.ERR_BEGIN = internal constant [8 x i8] c"error {\00"
@altera_parse_q12_response.ERR_END = internal constant [3 x i8] c"}\0A\00"
@__const.altera_parse_q12_response.RETURN_OK = [9 x i8] c"return {\00"
@stderr = global i32 0
@.str = [34 x i8] c"Did not get \22return\22 reply: '%s'\0A\00"
@.str.1 = [3 x i8] c"}\0A\00"
@.str.2 = [2 x i8] c"{\00"
@.str.3 = [8 x i8] c"no '{'\0A\00"
define i32 @altera_parse_q12_response(i8* %0, i64 %1, i8** %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i8**
%10 = alloca [9 x i8]
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8** %2, i8*** %8
store i8** %3, i8*** %9
%11 = load i8*, i8** %6
%12 = call i64 @strncmp(i8* %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @altera_parse_q12_response.ERR_BEGIN, i64 0, i64 0), i32 7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i64 7
%17 = load i8**, i8*** %8
store i8* %16, i8** %17
%18 = load i8*, i8** %6
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @altera_parse_q12_response.ERR_END, i64 0, i64 0))
%20 = load i8**, i8*** %9
store i8* %19, i8** %20
%21 = load i8**, i8*** %9
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %31
24: ; preds = %14
%25 = load i8*, i8** %6
%26 = load i8*, i8** %6
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = load i8**, i8*** %9
store i8* %29, i8** %30
br label %31
31: ; preds = %24, %14
store i32 23, i32* %5
br label %64
32: ; preds = %4
%33 = bitcast [9 x i8]* %10 to i8*
%34 = getelementptr inbounds [9 x i8], [9 x i8]* %10, i64 0, i64 0
%35 = load i8*, i8** %6
%36 = call i64 @strncmp(i8* %34, i8* %35, i32 8)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %6
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %40)
store i32 15, i32* %5
br label %64
42: ; preds = %32
%43 = load i8*, i8** %6
%44 = call i8* @strstr(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%45 = load i8**, i8*** %9
store i8* %44, i8** %45
%46 = load i8**, i8*** %9
%47 = load i8*, i8** %46
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %42
store i32 22, i32* %5
br label %64
50: ; preds = %42
%51 = load i8*, i8** %6
%52 = call i8* @strstr(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%53 = load i8**, i8*** %8
store i8* %52, i8** %53
%54 = load i8**, i8*** %8
%55 = load i8*, i8** %54
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %60
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 15, i32* %5
br label %64
60: ; preds = %50
%61 = load i8**, i8*** %8
%62 = load i8*, i8** %61
%63 = getelementptr inbounds i8, i8* %62, i64 1
store i8* %63, i8** %61
store i32 0, i32* %5
br label %64
64: ; preds = %60, %57, %49, %38, %31
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@altera_parse_q12_response.ERR_BEGIN = internal constant [8 x i8] c"error {\00"
@__const.altera_parse_q12_response.RETURN_OK = [9 x i8] c"return {\00"
@stderr = global i32 0
@.str = [34 x i8] c"Did not get \22return\22 reply: '%s'\0A\00"
@.str.1 = private constant [3 x i8] c"}\0A\00"
@.str.3 = [8 x i8] c"no '{'\0A\00"
define i32 @altera_parse_q12_response(i8* %0, i64 %1, i8** nocapture writeonly %2, i8** nocapture writeonly %3) {
%5 = alloca [9 x i8]
%6 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @altera_parse_q12_response.ERR_BEGIN, i64 0, i64 0), i32 7)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %4
%9 = getelementptr inbounds i8, i8* %0, i64 7
store i8* %9, i8** %2
%10 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %36
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %0)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8* %15, i8** %3
br label %36
16: ; preds = %4
%17 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%18 = call i64 @strncmp(i8* %17, i8* %0, i32 8)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %34
23: ; preds = %16
%24 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %3
%25 = icmp eq i8* %24, null
br i1 %25, label %34, label %26
26: ; preds = %23
%27 = call i8* @strchr(i8* dereferenceable(1) %0, i32 123)
store i8* %27, i8** %2
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %34
32: ; preds = %26
%33 = getelementptr inbounds i8, i8* %27, i64 1
store i8* %33, i8** %2
br label %34
34: ; preds = %23, %32, %29, %20
%35 = phi i32 [ 15, %20 ], [ 15, %29 ], [ 0, %32 ], [ 22, %23 ]
br label %36
36: ; preds = %8, %12, %34
%37 = phi i32 [ %35, %34 ], [ 23, %12 ], [ 23, %8 ]
ret i32 %37
}