// File: /workspace/github_c_makefile_star2_O3/ohkimur[P]the-c-programming-language-2nd-edition-solutions/chapter_1/exercise_1_24/check_syntax.c
// Function: check_syntax
void check_syntax(char str[])
{
  int parentheses = 0;
  int brackets = 0;
  int braces = 0;

  int single_quotes = FALSE;
  int double_quotes = FALSE;

  int block_comment = FALSE;
  int line_comment = FALSE;

  int i = 0;
  while (str[i] != '\0' && parentheses >= 0 && brackets >= 0 && braces >= 0)
  {
    if (!line_comment && !block_comment && !single_quotes && !double_quotes)
    {
      if (str[i] == '(')
      {
        ++parentheses;
      }
      else if (str[i] == ')')
      {
        --parentheses;
      }

      if (str[i] == '[')
      {
        ++brackets;
      }
      else if (str[i] == ']')
      {
        --brackets;
      }

      if (str[i] == '{')
      {
        ++braces;
      }
      else if (str[i] == '}')
      {
        --braces;
      }
    }

    if (!line_comment && !block_comment)
    {
      if (str[i] == '\'' && !single_quotes && !double_quotes)
      {
        single_quotes = TRUE;
      }
      else if (single_quotes && str[i] == '\'' && (str[i - 1] != '\\' || str[i - 2] == '\\'))
      {
        single_quotes = FALSE;
      }

      if (str[i] == '"' && !single_quotes && !double_quotes)
      {
        double_quotes = TRUE;
      }
      else if (double_quotes && str[i] == '"' && (str[i - 1] != '\\' || str[i - 2] == '\\'))
      {
        double_quotes = FALSE;
      }
    }

    if (!single_quotes && !double_quotes)
    {
      if (str[i] == '/' && str[i + 1] == '*' && !line_comment)
      {
        block_comment = TRUE;
      }
      else if (str[i] == '*' && str[i + 1] == '/')
      {
        block_comment = FALSE;
      }

      if (str[i] == '/' && str[i + 1] == '/' && !block_comment)
      {
        line_comment = TRUE;
      }
      else if (str[i] == '\n')
      {
        line_comment = FALSE;
      }
    }

    ++i;
  }

  if (parentheses)
  {
    printf("Error: unbalanced parentheses.\n");
  }

  if (brackets)
  {
    printf("Error: unbalanced brackets.\n");
  }

  if (braces)
  {
    printf("Error: unbalanced braces.\n");
  }

  if (single_quotes)
  {
    printf("Error: unbalanced single quotes.\n");
  }

  if (double_quotes)
  {
    printf("Error: unbalanced double quotes.\n");
  }

  if (block_comment)
  {
    printf("Error: block comment not closed.\n");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movb (%rdi), %dl
testb %dl, %dl
je 0x1406
incq %rdi
xorl %eax, %eax
xorl %esi, %esi
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %ecx, %ecx
movl %esi, %r8d
orl %ebx, %r8d
setne %r8b
testl %r12d, %r12d
jne 0x125c
testb %r8b, %r8b
jne 0x125c
testl %r15d, %r15d
jne 0x125c
movzbl %dl, %r9d
cmpl $0x5c, %r9d
jg 0x1237
cmpl $0x28, %r9d
je 0x124d
cmpl $0x29, %r9d
je 0x1256
cmpl $0x5b, %r9d
jne 0x125c
incl %r14d
jmp 0x125c
cmpl $0x5d, %r9d
je 0x1251
cmpl $0x7b, %r9d
je 0x125a
cmpl $0x7d, %r9d
jne 0x125c
decl %ebp
jmp 0x125c
incl %ecx
jmp 0x125c
decl %r14d
jmp 0x125c
decl %ecx
jmp 0x125c
incl %ebp
testb %r8b, %r8b
jne 0x1312
testl %r15d, %r15d
sete %r9b
testl %r12d, %r12d
sete %r8b
cmpb $0x27, %dl
sete %r10b
testl %r12d, %r12d
je 0x12a1
movl $0x1, %r9d
movb $0x1, %r8b
cmpb $0x27, %dl
jne 0x12ab
cmpb $0x5c, -0x2(%rdi)
jne 0x1299
cmpb $0x5c, -0x3(%rdi)
jne 0x12ab
xorl %r8d, %r8d
xorl %r9d, %r9d
jmp 0x12ab
andb %r10b, %r8b
andb %r9b, %r8b
movzbl %r8b, %r9d
testl %r15d, %r15d
setne %r11b
cmpb $0x22, %dl
setne %r10b
orb %r11b, %r10b
orb %r8b, %r10b
movl $0x0, %r12d
cmovnel %r9d, %r12d
testl %r15d, %r15d
je 0x130a
xorl %ebx, %ebx
movl $0x1, %r15d
cmpb $0x22, %dl
jne 0x1302
cmpb $0x5c, -0x2(%rdi)
jne 0x12e7
cmpb $0x5c, -0x3(%rdi)
jne 0x1302
movl $0x1, %r12d
xorl %r8d, %r8d
movl $0x0, %r15d
movl $0x0, %ebx
testl %r9d, %r9d
jne 0x137e
jmp 0x136b
movl %r9d, %r12d
xorl %r8d, %r8d
jmp 0x137e
xorb $0x1, %r10b
movzbl %r10b, %r15d
movl %r12d, %r8d
orl %r15d, %r8d
jne 0x137b
cmpb $0x2a, %dl
je 0x1362
movzbl %dl, %r8d
cmpl $0x2f, %r8d
jne 0x1368
testl %esi, %esi
setne %r8b
movb (%rdi), %r9b
cmpb $0x2a, %r9b
setne %r10b
orb %r8b, %r10b
movl $0x1, %r8d
je 0x136b
cmpb $0x2f, %r9b
jne 0x1368
movl %ebx, %r8d
testl %ebx, %ebx
jne 0x136b
xorl %r15d, %r15d
movl $0x1, %r8d
xorl %r12d, %r12d
xorl %ebx, %ebx
jmp 0x137e
cmpb $0x2f, (%rdi)
cmovel %eax, %ebx
movl %ebx, %r8d
xorl %r15d, %r15d
cmpb $0xa, %dl
cmovel %r15d, %esi
xorl %r12d, %r12d
movl %r8d, %ebx
movl %esi, %r8d
movb (%rdi), %dl
testb %dl, %dl
je 0x139b
testl %ecx, %ecx
js 0x139b
testl %r14d, %r14d
js 0x139b
incq %rdi
movl %r8d, %esi
testl %ebp, %ebp
jns 0x11fd
testl %ecx, %ecx
je 0x13ab
leaq 0xc5e(%rip), %rdi       # 0x2004
callq 0x1030
testl %r14d, %r14d
je 0x13bc
leaq 0xc6c(%rip), %rdi       # 0x2023
callq 0x1030
testl %ebp, %ebp
je 0x13cc
leaq 0xc78(%rip), %rdi       # 0x203f
callq 0x1030
testl %r12d, %r12d
je 0x13dd
leaq 0xc81(%rip), %rdi       # 0x2059
callq 0x1030
testl %r15d, %r15d
je 0x13ee
leaq 0xc91(%rip), %rdi       # 0x207a
callq 0x1030
testl %ebx, %ebx
je 0x1406
leaq 0xca2(%rip), %rdi       # 0x209b
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1030
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq