// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x29(%rbp)
je 0x128a
xorl %eax, %eax
cmpl $0x0, -0xc(%rbp)
movb %al, -0x29(%rbp)
jl 0x128a
xorl %eax, %eax
cmpl $0x0, -0x10(%rbp)
movb %al, -0x29(%rbp)
jl 0x128a
cmpl $0x0, -0x14(%rbp)
setge %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
testb $0x1, %al
jne 0x1296
jmp 0x153c
cmpl $0x0, -0x24(%rbp)
jne 0x1368
cmpl $0x0, -0x20(%rbp)
jne 0x1368
cmpl $0x0, -0x18(%rbp)
jne 0x1368
cmpl $0x0, -0x1c(%rbp)
jne 0x1368
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x28, %eax
jne 0x12da
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x12f6
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x29, %eax
jne 0x12f4
movl -0xc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x12f6
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5b, %eax
jne 0x1312
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x132e
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5d, %eax
jne 0x132c
movl -0x10(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x132e
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7b, %eax
jne 0x134a
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1366
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7d, %eax
jne 0x1364
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1366
jmp 0x1368
cmpl $0x0, -0x24(%rbp)
jne 0x1462
cmpl $0x0, -0x20(%rbp)
jne 0x1462
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x27, %eax
jne 0x13a2
cmpl $0x0, -0x18(%rbp)
jne 0x13a2
cmpl $0x0, -0x1c(%rbp)
jne 0x13a2
movl $0x1, -0x18(%rbp)
jmp 0x13ee
cmpl $0x0, -0x18(%rbp)
je 0x13ec
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x27, %eax
jne 0x13ec
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5c, %eax
jne 0x13e5
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5c, %eax
jne 0x13ec
movl $0x0, -0x18(%rbp)
jmp 0x13ee
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x22, %eax
jne 0x1414
cmpl $0x0, -0x18(%rbp)
jne 0x1414
cmpl $0x0, -0x1c(%rbp)
jne 0x1414
movl $0x1, -0x1c(%rbp)
jmp 0x1460
cmpl $0x0, -0x1c(%rbp)
je 0x145e
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x22, %eax
jne 0x145e
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5c, %eax
jne 0x1457
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5c, %eax
jne 0x145e
movl $0x0, -0x1c(%rbp)
jmp 0x1460
jmp 0x1462
cmpl $0x0, -0x18(%rbp)
jne 0x152e
cmpl $0x0, -0x1c(%rbp)
jne 0x152e
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x14ac
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
jne 0x14ac
cmpl $0x0, -0x24(%rbp)
jne 0x14ac
movl $0x1, -0x20(%rbp)
jmp 0x14dc
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2a, %eax
jne 0x14da
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x14da
movl $0x0, -0x20(%rbp)
jmp 0x14dc
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x1512
movq -0x8(%rbp), %rax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x1512
cmpl $0x0, -0x20(%rbp)
jne 0x1512
movl $0x1, -0x24(%rbp)
jmp 0x152c
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x152a
movl $0x0, -0x24(%rbp)
jmp 0x152c
jmp 0x152e
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1254
cmpl $0x0, -0xc(%rbp)
je 0x1550
leaq 0xabb(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1030
cmpl $0x0, -0x10(%rbp)
je 0x1564
leaq 0xac7(%rip), %rdi       # 0x2024
movb $0x0, %al
callq 0x1030
cmpl $0x0, -0x14(%rbp)
je 0x1578
leaq 0xad0(%rip), %rdi       # 0x2041
movb $0x0, %al
callq 0x1030
cmpl $0x0, -0x18(%rbp)
je 0x158c
leaq 0xad7(%rip), %rdi       # 0x205c
movb $0x0, %al
callq 0x1030
cmpl $0x0, -0x1c(%rbp)
je 0x15a0
leaq 0xae5(%rip), %rdi       # 0x207e
movb $0x0, %al
callq 0x1030
cmpl $0x0, -0x20(%rbp)
je 0x15b4
leaq 0xaf3(%rip), %rdi       # 0x20a0
movb $0x0, %al
callq 0x1030
addq $0x30, %rsp
popq %rbp
retq