// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
pushq %rax
movb (%rdi), %r10b
testb %r10b, %r10b
je 0x138c
incq %rdi
xorl %eax, %eax
xorl %esi, %esi
xorl %ebx, %ebx
xorl %edx, %edx
xorl %r11d, %r11d
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %esi, %ebp
orl %ebx, %ebp
setne %bpl
testl %r11d, %r11d
jne 0x1258
testb %bpl, %bpl
jne 0x1258
testl %edx, %edx
jne 0x1258
movzbl %r10b, %r14d
cmpb $0x29, %r14b
je 0x122f
cmpl $0x28, %r14d
jne 0x1232
incl %r9d
jmp 0x1232
decl %r9d
cmpb $0x5d, %r10b
je 0x1243
cmpl $0x5b, %r14d
jne 0x1246
incl %r8d
jmp 0x1246
decl %r8d
cmpb $0x7d, %r10b
je 0x1256
cmpl $0x7b, %r14d
jne 0x1258
incl %ecx
jmp 0x1258
decl %ecx
testb %bpl, %bpl
jne 0x12ee
testl %edx, %edx
sete %r14b
testl %r11d, %r11d
sete %bpl
cmpb $0x27, %r10b
sete %r15b
testl %r11d, %r11d
je 0x129d
movl $0x1, %r11d
movb $0x1, %bpl
cmpb $0x27, %r10b
jne 0x12a7
cmpb $0x5c, -0x2(%rdi)
jne 0x1296
cmpb $0x5c, -0x3(%rdi)
jne 0x12a7
xorl %ebp, %ebp
xorl %r11d, %r11d
jmp 0x12a7
andb %r15b, %bpl
andb %r14b, %bpl
movzbl %bpl, %r11d
testl %edx, %edx
setne %r15b
cmpb $0x22, %r10b
setne %r14b
orb %r15b, %r14b
orb %bpl, %r14b
movl $0x0, %ebp
cmovnel %r11d, %ebp
testl %edx, %edx
je 0x12e3
movl $0x1, %edx
cmpb $0x22, %r10b
jne 0x12ee
cmpb $0x5c, -0x2(%rdi)
jne 0x12df
cmpb $0x5c, -0x3(%rdi)
jne 0x12ee
xorl %edx, %edx
jmp 0x12ee
xorb $0x1, %r14b
movzbl %r14b, %edx
movl %ebp, %r11d
movl %r11d, %ebp
orl %edx, %ebp
je 0x12f9
movl %ebx, %ebp
jmp 0x1346
cmpb $0x2f, %r10b
jne 0x130d
testl %esi, %esi
jne 0x130d
movl $0x1, %ebp
cmpb $0x2a, (%rdi)
je 0x131b
cmpb $0x2a, %r10b
jne 0x1319
cmpb $0x2f, (%rdi)
cmovel %eax, %ebx
movl %ebx, %ebp
cmpb $0x2f, %r10b
jne 0x1336
cmpb $0x2f, (%rdi)
setne %bl
testl %ebp, %ebp
setne %r14b
orb %bl, %r14b
cmpb $0x1, %r14b
jne 0x133f
cmpb $0xa, %r10b
cmovel %eax, %esi
jmp 0x1346
movl $0x1, %esi
xorl %ebp, %ebp
movb (%rdi), %r10b
testb %r10b, %r10b
je 0x1365
testl %r9d, %r9d
js 0x1365
testl %r8d, %r8d
js 0x1365
incq %rdi
movl %ebp, %ebx
testl %ecx, %ecx
jns 0x1204
testl %r9d, %r9d
sete %al
testl %r8d, %r8d
sete %r13b
testl %ecx, %ecx
sete %r12b
testl %r11d, %r11d
sete %r15b
testl %edx, %edx
sete %r14b
testl %ebp, %ebp
sete %bl
jmp 0x139c
movb $0x1, %al
movb $0x1, %r13b
movb $0x1, %r12b
movb $0x1, %r15b
movb $0x1, %r14b
movb $0x1, %bl
testb %al, %al
jne 0x13ac
leaq 0xc5d(%rip), %rdi       # 0x2004
callq 0x1030
testb %r13b, %r13b
jne 0x13bd
leaq 0xc6b(%rip), %rdi       # 0x2023
callq 0x1030
testb %r12b, %r12b
jne 0x13ce
leaq 0xc76(%rip), %rdi       # 0x203f
callq 0x1030
testb %r15b, %r15b
jne 0x13df
leaq 0xc7f(%rip), %rdi       # 0x2059
callq 0x1030
testb %r14b, %r14b
jne 0x13f0
leaq 0xc8f(%rip), %rdi       # 0x207a
callq 0x1030
testb %bl, %bl
je 0x1403
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc91(%rip), %rdi       # 0x209b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1030