#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 uint16_t;
typedef scalar_t__ int16_t;

/* Variables and functions */
int addAgc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ stderr;
uint16_t generateBuggerWord(int verbose, int bank, int length, int16_t *code) {
  uint16_t checksum = 0, bugger = 0;
  int i = 0;
  for (i = 0; i < length; i++) {
    int16_t value = code[i];
    if (value < 0) {
      fprintf(stderr, "Warning: zeroing unassigned word at (%02o,%04o).\n",
              bank, (02000) + i);
      value = 0;
    }
    checksum = addAgc(checksum, value);
  }
  if ((checksum & 040000) == 0) {
    bugger = addAgc(bank, 077777 & ~checksum);
    if (verbose)
      printf("Checksum for bank %02o is %05o, MSB=0, bugger word is %05o.\n",
             bank, checksum, bugger);
  } else {
    bugger = addAgc(077777 & ~bank, 077777 & ~checksum);
    if (verbose)
      printf("Checksum for bank %02o is %05o, MSB=1, bugger word is %05o.\n",
             bank, checksum, bugger);
  }
  return (bugger);
}


x86 assembly (clang, -O0):
<generateBuggerWord>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d0 <generateBuggerWord+0xa0>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11b1 <generateBuggerWord+0x81>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0x24(%rbp),%ecx
add    $0x400,%ecx
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    %eax,%esi
call   1030 <addAgc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1161 <generateBuggerWord+0x31>
mov    -0x1c(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
jne    121c <generateBuggerWord+0xec>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
xor    $0xffffffff,%esi
and    $0x7fff,%esi
call   1030 <addAgc@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1217 <generateBuggerWord+0xe7>
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
jmp    1260 <generateBuggerWord+0x130>
mov    -0x8(%rbp),%edi
xor    $0xffffffff,%edi
and    $0x7fff,%edi
mov    -0x1c(%rbp),%esi
xor    $0xffffffff,%esi
and    $0x7fff,%esi
call   1030 <addAgc@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     125b <generateBuggerWord+0x12b>
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe19(%rip),%rdi
call   1040 <printf@plt>
jmp    1260 <generateBuggerWord+0x130>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generateBuggerWord>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%ebx
test   %edx,%edx
jle    11a9 <generateBuggerWord+0x79>
mov    %rcx,%r13
mov    %ebx,0x4(%rsp)
mov    %edx,%ebx
xor    %r14d,%r14d
lea    0xea9(%rip),%r12
xor    %ebp,%ebp
jmp    1172 <generateBuggerWord+0x42>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1030 <addAgc@plt>
mov    %eax,%ebp
add    $0x1,%r14
cmp    %r14,%rbx
je     119b <generateBuggerWord+0x6b>
mov    0x0(%r13,%r14,8),%rsi
test   %rsi,%rsi
jns    1160 <generateBuggerWord+0x30>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0x400(%r14),%ecx
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <fprintf@plt>
xor    %esi,%esi
jmp    1160 <generateBuggerWord+0x30>
test   $0x4000,%ebp
jne    11cd <generateBuggerWord+0x9d>
mov    0x4(%rsp),%ebx
jmp    11ab <generateBuggerWord+0x7b>
xor    %ebp,%ebp
mov    %ebp,%esi
not    %esi
and    $0x7fff,%esi
mov    %r15d,%edi
call   1030 <addAgc@plt>
mov    %eax,%r14d
test   %ebx,%ebx
je     1205 <generateBuggerWord+0xd5>
lea    0xe67(%rip),%rdi
jmp    11f8 <generateBuggerWord+0xc8>
mov    %r15d,%edi
not    %edi
and    $0x7fff,%edi
mov    %ebp,%esi
not    %esi
and    $0x7fff,%esi
call   1030 <addAgc@plt>
mov    %eax,%r14d
cmpl   $0x0,0x4(%rsp)
je     1205 <generateBuggerWord+0xd5>
lea    0xe77(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"Warning: zeroing unassigned word at (%02o,%04o).\0A\00"
@.str.1 = [61 x i8] c"Checksum for bank %02o is %05o, MSB=0, bugger word is %05o.\0A\00"
@.str.2 = [61 x i8] c"Checksum for bank %02o is %05o, MSB=1, bugger word is %05o.\0A\00"
define i32 @generateBuggerWord(i32 %0, i32 %1, i32 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64* %3, i64** %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %11
br label %13
13: ; preds = %36, %4
%14 = load i32, i32* %11
%15 = load i32, i32* %7
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %39
17: ; preds = %13
%18 = load i64*, i64** %8
%19 = load i32, i32* %11
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i64, i64* %18, i64 %20
%22 = load i64, i64* %21
store i64 %22, i64* %12
%23 = load i64, i64* %12
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i32, i32* %6
%28 = load i32, i32* %11
%29 = add nsw i32 1024, %28
%30 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %29)
store i64 0, i64* %12
br label %31
31: ; preds = %25, %17
%32 = load i32, i32* %9
%33 = load i64, i64* %12
%34 = trunc i64 %33 to i32
%35 = call i32 @addAgc(i32 %32, i32 %34)
store i32 %35, i32* %9
br label %36
36: ; preds = %31
%37 = load i32, i32* %11
%38 = add nsw i32 %37, 1
store i32 %38, i32* %11
br label %13
39: ; preds = %13
%40 = load i32, i32* %9
%41 = and i32 %40, 16384
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %57
43: ; preds = %39
%44 = load i32, i32* %6
%45 = load i32, i32* %9
%46 = xor i32 %45, -1
%47 = and i32 32767, %46
%48 = call i32 @addAgc(i32 %44, i32 %47)
store i32 %48, i32* %10
%49 = load i32, i32* %5
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %43
%52 = load i32, i32* %6
%53 = load i32, i32* %9
%54 = load i32, i32* %10
%55 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %52, i32 %53, i32 %54)
br label %56
56: ; preds = %51, %43
br label %73
57: ; preds = %39
%58 = load i32, i32* %6
%59 = xor i32 %58, -1
%60 = and i32 32767, %59
%61 = load i32, i32* %9
%62 = xor i32 %61, -1
%63 = and i32 32767, %62
%64 = call i32 @addAgc(i32 %60, i32 %63)
store i32 %64, i32* %10
%65 = load i32, i32* %5
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %57
%68 = load i32, i32* %6
%69 = load i32, i32* %9
%70 = load i32, i32* %10
%71 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %68, i32 %69, i32 %70)
br label %72
72: ; preds = %67, %57
br label %73
73: ; preds = %72, %56
%74 = load i32, i32* %10
ret i32 %74
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"Warning: zeroing unassigned word at (%02o,%04o).\0A\00"
@.str.1 = [61 x i8] c"Checksum for bank %02o is %05o, MSB=0, bugger word is %05o.\0A\00"
@.str.2 = [61 x i8] c"Checksum for bank %02o is %05o, MSB=1, bugger word is %05o.\0A\00"
define i32 @generateBuggerWord(i32 %0, i32 %1, i32 %2, i64* nocapture %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %28
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %19
%9 = phi i64 [ 0, %6 ], [ %23, %19 ]
%10 = phi i32 [ 0, %6 ], [ %22, %19 ]
%11 = getelementptr inbounds i64, i64* %3, i64 %9
%12 = load i64, i64* %11
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = trunc i64 %9 to i32
%17 = add i32 %16, 1024
%18 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %17)
br label %19
19: ; preds = %14, %8
%20 = phi i64 [ 0, %14 ], [ %12, %8 ]
%21 = trunc i64 %20 to i32
%22 = tail call i32 @addAgc(i32 %10, i32 %21)
%23 = add nuw nsw i64 %9, 1
%24 = icmp eq i64 %23, %7
br i1 %24, label %25, label %8
25: ; preds = %19
%26 = and i32 %22, 16384
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %4, %25
%29 = phi i32 [ %22, %25 ], [ 0, %4 ]
%30 = and i32 %29, 32767
%31 = xor i32 %30, 32767
%32 = tail call i32 @addAgc(i32 %1, i32 %31)
%33 = icmp eq i32 %0, 0
br i1 %33, label %45, label %34
34: ; preds = %28
%35 = tail call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %29, i32 %32)
br label %45
36: ; preds = %25
%37 = and i32 %1, 32767
%38 = xor i32 %37, 32767
%39 = and i32 %22, 32767
%40 = xor i32 %39, 32767
%41 = tail call i32 @addAgc(i32 %38, i32 %40)
%42 = icmp eq i32 %0, 0
br i1 %42, label %45, label %43
43: ; preds = %36
%44 = tail call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %22, i32 %41)
br label %45
45: ; preds = %36, %43, %28, %34
%46 = phi i32 [ %32, %34 ], [ %32, %28 ], [ %41, %43 ], [ %41, %36 ]
ret i32 %46
}