#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 uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int parse_integer(char *);
int parse_line(int /*<<< orphan*/ *, int, char *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int parse_buffer(FILE *output, char *buffer, size_t buffer_length) {
  size_t current_offset = 0, sentence_start = 0, sentence_end = 0;
  unsigned current_sentence = 0;
  uint32_t memory_position = 0;
  size_t sentence_length = 0;
  while (current_offset < buffer_length) {
    sentence_start = current_offset;
    sentence_end = current_offset;
    current_sentence++;
    for (; buffer[sentence_end] && buffer[sentence_end] != '\n';
         sentence_end++) {
    }
    sentence_length = sentence_end - sentence_start;
    if (buffer[sentence_start] == '*') {
      for (; sentence_start < sentence_end && buffer[sentence_start] != '=';
           sentence_start++) {
      }
      memory_position =
          ((parse_integer(buffer + sentence_start + 1)) & 0xFFFFF);
      printf(" M:PRSR: ORG to 0x%X\n", memory_position);
      memory_position--;
    } else if (sentence_start == sentence_end) {
      fprintf(output, "0x%05X 0x000000\n", memory_position);
      printf(" M:PRSR: Blank line -> LDC 0\n");
    } else {
      switch (parse_line(output, memory_position, buffer + sentence_start,
                         sentence_length)) {
      case -1:
        printf(" E:PRSR: Parser failed in sentence %d\n", current_sentence);
        return -1;
      case 1:
        memory_position--;
      default:
        break;
      }
    }
    current_offset = sentence_end + 1;
    memory_position = ((memory_position + 1) & 0xFFFFF);
  }
  printf(" M:PRSR: Parser done\n");
  return 0;
}


x86 assembly (clang, -O0):
<parse_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1371 <parse_buffer+0x231>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x49(%rbp)
je     11d8 <parse_buffer+0x98>
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11e8 <parse_buffer+0xa8>
jmp    11fe <parse_buffer+0xbe>
jmp    11ed <parse_buffer+0xad>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11a9 <parse_buffer+0x69>
mov    -0x38(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2a,%eax
jne    12aa <parse_buffer+0x16a>
jmp    1224 <parse_buffer+0xe4>
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x38(%rbp),%rcx
mov    %al,-0x4a(%rbp)
jae    124c <parse_buffer+0x10c>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3d,%eax
setne  %al
mov    %al,-0x4a(%rbp)
mov    -0x4a(%rbp),%al
test   $0x1,%al
jne    125c <parse_buffer+0x11c>
jmp    1272 <parse_buffer+0x132>
jmp    1261 <parse_buffer+0x121>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1224 <parse_buffer+0xe4>
mov    -0x18(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1060 <parse_integer@plt>
and    $0xfffff,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%esi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x40(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
jmp    1352 <parse_buffer+0x212>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    12de <parse_buffer+0x19e>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%edx
lea    0xd50(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    134d <parse_buffer+0x20d>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rax
add    %rax,%rdx
mov    -0x48(%rbp),%rcx
call   1050 <parse_line@plt>
mov    %eax,%ecx
mov    %ecx,-0x50(%rbp)
sub    $0xffffffff,%eax
je     131d <parse_buffer+0x1dd>
jmp    130c <parse_buffer+0x1cc>
mov    -0x50(%rbp),%eax
sub    $0x1,%eax
je     133a <parse_buffer+0x1fa>
jmp    1343 <parse_buffer+0x203>
mov    -0x3c(%rbp),%esi
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <parse_buffer+0x246>
mov    -0x40(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
jmp    1348 <parse_buffer+0x208>
jmp    134d <parse_buffer+0x20d>
jmp    1352 <parse_buffer+0x212>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
and    $0xfffff,%eax
mov    %eax,-0x40(%rbp)
jmp    1182 <parse_buffer+0x42>
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x8(%rsp)
test   %rdx,%rdx
je     12a1 <parse_buffer+0x151>
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x1(%rsi),%rax
mov    %rax,0x10(%rsp)
xor    %r13d,%r13d
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    11cb <parse_buffer+0x7b>
mov    0x8(%rsp),%rdi
lea    0xe85(%rip),%rsi
mov    %r13d,%edx
call   1050 <fprintf@plt>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nop
sub    %r15,%r12
add    $0x1,%r12
add    $0x1,%r13d
and    $0xfffff,%r13d
cmp    %r14,%r12
jae    12a1 <parse_buffer+0x151>
add    $0x1,%ebp
mov    $0xffffffffffffffff,%rax
mov    %rbx,%rcx
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
movzbl (%rcx,%r12,1),%edx
test   %dl,%dl
je     1200 <parse_buffer+0xb0>
cmp    $0xa,%dl
je     1200 <parse_buffer+0xb0>
add    $0xffffffffffffffff,%r15
add    $0x1,%rcx
add    $0x1,%rax
jmp    11e0 <parse_buffer+0x90>
nopl   0x0(%rax)
cmpb   $0x2a,(%rbx,%r12,1)
jne    1240 <parse_buffer+0xf0>
mov    %r12,%rcx
sub    %r15,%rcx
mov    %r12,%rdi
cmp    %r12,%rcx
jbe    1273 <parse_buffer+0x123>
mov    %r12,%rdx
nopl   0x0(%rax,%rax,1)
sub    $0x1,%rax
jb     1270 <parse_buffer+0x120>
lea    0x1(%rdx),%rdi
cmpb   $0x3d,0x1(%rbx,%rdx,1)
mov    %rdi,%rdx
jne    1220 <parse_buffer+0xd0>
jmp    1273 <parse_buffer+0x123>
cs nopw 0x0(%rax,%rax,1)
test   %r15,%r15
je     1185 <parse_buffer+0x35>
mov    %r15,%rcx
neg    %rcx
lea    (%rbx,%r12,1),%rdx
mov    0x8(%rsp),%rdi
mov    %r13d,%esi
call   1060 <parse_line@plt>
cmp    $0x1,%eax
je     1298 <parse_buffer+0x148>
cmp    $0xffffffff,%eax
jne    11b0 <parse_buffer+0x60>
jmp    12be <parse_buffer+0x16e>
mov    %rcx,%rdi
add    0x10(%rsp),%rdi
call   1070 <parse_integer@plt>
mov    %eax,%r13d
and    $0xfffff,%r13d
lea    0xd72(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%r13d
jmp    11b0 <parse_buffer+0x60>
lea    0xda6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd62(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    12af <parse_buffer+0x15f>

LLVM IR (-O0):
@.str = [22 x i8] c" M:PRSR: ORG to 0x%X\0A\00"
@.str.1 = [17 x i8] c"0x%05X 0x000000\0A\00"
@.str.2 = [30 x i8] c" M:PRSR: Blank line -> LDC 0\0A\00"
@.str.3 = [39 x i8] c" E:PRSR: Parser failed in sentence %d\0A\00"
@.str.4 = [22 x i8] c" M:PRSR: Parser done\0A\00"
define i32 @parse_buffer(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i64 0, i64* %13
br label %14
14: ; preds = %108, %3
%15 = load i64, i64* %8
%16 = load i64, i64* %7
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %114
18: ; preds = %14
%19 = load i64, i64* %8
store i64 %19, i64* %9
%20 = load i64, i64* %8
store i64 %20, i64* %10
%21 = load i32, i32* %11
%22 = add i32 %21, 1
store i32 %22, i32* %11
br label %23
23: ; preds = %40, %18
%24 = load i8*, i8** %6
%25 = load i64, i64* %10
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = load i64, i64* %10
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 10
br label %37
37: ; preds = %30, %23
%38 = phi i1 [ false, %23 ], [ %36, %30 ]
br i1 %38, label %39, label %43
39: ; preds = %37
br label %40
40: ; preds = %39
%41 = load i64, i64* %10
%42 = add i64 %41, 1
store i64 %42, i64* %10
br label %23
43: ; preds = %37
%44 = load i64, i64* %10
%45 = load i64, i64* %9
%46 = sub i64 %44, %45
store i64 %46, i64* %13
%47 = load i8*, i8** %6
%48 = load i64, i64* %9
%49 = getelementptr inbounds i8, i8* %47, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 42
br i1 %52, label %53, label %82
53: ; preds = %43
br label %54
54: ; preds = %68, %53
%55 = load i64, i64* %9
%56 = load i64, i64* %10
%57 = icmp ult i64 %55, %56
br i1 %57, label %58, label %65
58: ; preds = %54
%59 = load i8*, i8** %6
%60 = load i64, i64* %9
%61 = getelementptr inbounds i8, i8* %59, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp ne i32 %63, 61
br label %65
65: ; preds = %58, %54
%66 = phi i1 [ false, %54 ], [ %64, %58 ]
br i1 %66, label %67, label %71
67: ; preds = %65
br label %68
68: ; preds = %67
%69 = load i64, i64* %9
%70 = add i64 %69, 1
store i64 %70, i64* %9
br label %54
71: ; preds = %65
%72 = load i8*, i8** %6
%73 = load i64, i64* %9
%74 = getelementptr inbounds i8, i8* %72, i64 %73
%75 = getelementptr inbounds i8, i8* %74, i64 1
%76 = call i32 @parse_integer(i8* %75)
%77 = and i32 %76, 1048575
store i32 %77, i32* %12
%78 = load i32, i32* %12
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %78)
%80 = load i32, i32* %12
%81 = add nsw i32 %80, -1
store i32 %81, i32* %12
br label %108
82: ; preds = %43
%83 = load i64, i64* %9
%84 = load i64, i64* %10
%85 = icmp eq i64 %83, %84
br i1 %85, label %86, label %91
86: ; preds = %82
%87 = load i32*, i32** %5
%88 = load i32, i32* %12
%89 = call i32 @fprintf(i32* %87, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %88)
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %107
91: ; preds = %82
%92 = load i32*, i32** %5
%93 = load i32, i32* %12
%94 = load i8*, i8** %6
%95 = load i64, i64* %9
%96 = getelementptr inbounds i8, i8* %94, i64 %95
%97 = load i64, i64* %13
%98 = call i32 @parse_line(i32* %92, i32 %93, i8* %96, i64 %97)
switch i32 %98, label %105 [
i32 -1, label %99
i32 1, label %102
]
99: ; preds = %91
%100 = load i32, i32* %11
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %100)
store i32 -1, i32* %4
br label %116
102: ; preds = %91
%103 = load i32, i32* %12
%104 = add nsw i32 %103, -1
store i32 %104, i32* %12
br label %105
105: ; preds = %91, %102
br label %106
106: ; preds = %105
br label %107
107: ; preds = %106, %86
br label %108
108: ; preds = %107, %71
%109 = load i64, i64* %10
%110 = add i64 %109, 1
store i64 %110, i64* %8
%111 = load i32, i32* %12
%112 = add nsw i32 %111, 1
%113 = and i32 %112, 1048575
store i32 %113, i32* %12
br label %14
114: ; preds = %14
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %116
116: ; preds = %114, %99
%117 = load i32, i32* %4
ret i32 %117
}

LLVM IR (-O2):
@.str = [22 x i8] c" M:PRSR: ORG to 0x%X\0A\00"
@.str.1 = [17 x i8] c"0x%05X 0x000000\0A\00"
@.str.3 = [39 x i8] c" E:PRSR: Parser failed in sentence %d\0A\00"
@str = [21 x i8] c" M:PRSR: Parser done\00"
@str.5 = [29 x i8] c" M:PRSR: Blank line -> LDC 0\00"
define i32 @parse_buffer(i32* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %57, label %5
5: ; preds = %3
%6 = getelementptr inbounds i8, i8* %1, i64 1
br label %7
7: ; preds = %5, %51
%8 = phi i32 [ %55, %51 ], [ 0, %5 ]
%9 = phi i32 [ %11, %51 ], [ 0, %5 ]
%10 = phi i64 [ %53, %51 ], [ 0, %5 ]
%11 = add i32 %9, 1
br label %12
12: ; preds = %16, %7
%13 = phi i64 [ %10, %7 ], [ %17, %16 ]
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = load i8, i8* %14
switch i8 %15, label %16 [
i8 0, label %18
i8 10, label %18
]
16: ; preds = %12
%17 = add i64 %13, 1
br label %12
18: ; preds = %12, %12
%19 = sub i64 %13, %10
%20 = getelementptr inbounds i8, i8* %1, i64 %10
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 42
br i1 %22, label %23, label %40
23: ; preds = %18
%24 = icmp ugt i64 %13, %10
br i1 %24, label %25, label %33
25: ; preds = %23, %29
%26 = phi i64 [ %27, %29 ], [ %10, %23 ]
%27 = add i64 %26, 1
%28 = icmp eq i64 %27, %13
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = getelementptr inbounds i8, i8* %1, i64 %27
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 61
br i1 %32, label %33, label %25
33: ; preds = %29, %25, %23
%34 = phi i64 [ %10, %23 ], [ %13, %25 ], [ %27, %29 ]
%35 = getelementptr inbounds i8, i8* %6, i64 %34
%36 = tail call i32 @parse_integer(i8* %35)
%37 = and i32 %36, 1048575
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = add nsw i32 %37, -1
br label %51
40: ; preds = %18
%41 = icmp eq i64 %13, %10
br i1 %41, label %42, label %45
42: ; preds = %40
%43 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
br label %51
45: ; preds = %40
%46 = tail call i32 @parse_line(i32* %0, i32 %8, i8* %20, i64 %19)
switch i32 %46, label %51 [
i32 -1, label %47
i32 1, label %49
]
47: ; preds = %45
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %11)
br label %59
49: ; preds = %45
%50 = add nsw i32 %8, -1
br label %51
51: ; preds = %42, %49, %45, %33
%52 = phi i32 [ %39, %33 ], [ %8, %42 ], [ %8, %45 ], [ %50, %49 ]
%53 = add i64 %13, 1
%54 = add nsw i32 %52, 1
%55 = and i32 %54, 1048575
%56 = icmp ult i64 %53, %2
br i1 %56, label %7, label %57
57: ; preds = %51, %3
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %59
59: ; preds = %57, %47
%60 = phi i32 [ -1, %47 ], [ 0, %57 ]
ret i32 %60
}