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

/* Variables and functions */
int /*<<< orphan*/ fcloseall();
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int *malloc(int);
int main() {
  FILE *fi = fopen("INP.TXT", "r"), *fo = fopen("OUT.TXT", "w");
  int n, m, i, *a, *b, x, y;
  char signal;
  fscanf(fi, "%d %d", &n, &m);
  a = malloc(n * sizeof(int));
  b = malloc(n * sizeof(int));
  for (i = 0; i < n; i++)
    fscanf(fi, "%d %d", a + i, b + i);
  for (i = 0; i < m; i++) {
    fscanf(fi, "%hhd %d %d", &signal, &x, &y);
    x--;
    b[x] += (signal ^ 1) ? -y : y;
    if (b[x] < a[x]) {
      fprintf(fo, "BUY %d %d\n", x + 1, a[x] - b[x]);
      b[x] = a[x];
    }
  }
  fcloseall();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1030 <fscanf@plt>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    121e <main+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rdx
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
mov    -0x38(%rbp),%rcx
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rcx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1030 <fscanf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d4 <main+0x84>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12fd <main+0x1ad>
mov    -0x10(%rbp),%rdi
lea    0xdd8(%rip),%rsi
lea    -0x41(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
lea    -0x40(%rbp),%r8
mov    $0x0,%al
call   1030 <fscanf@plt>
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
movsbl -0x41(%rbp),%eax
xor    $0x1,%eax
cmp    $0x0,%eax
je     1275 <main+0x125>
xor    %eax,%eax
sub    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    127b <main+0x12b>
mov    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%edx
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x30(%rbp),%rcx
movslq -0x3c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    12ea <main+0x19a>
mov    -0x18(%rbp),%rdi
mov    -0x3c(%rbp),%edx
add    $0x1,%edx
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rsi
sub    (%rax,%rsi,4),%ecx
lea    0xd50(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    12ef <main+0x19f>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1225 <main+0xd5>
mov    $0x0,%al
call   1070 <fcloseall@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe9b(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
lea    0xe8f(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,0x20(%rsp)
lea    0xe86(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <fscanf@plt>
mov    0x18(%rsp),%ebp
lea    0x0(,%rbp,4),%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %ebp,%ebp
jle    120d <main+0xbd>
mov    %r15,%rbp
mov    %r13,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
lea    0xe2f(%rip),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <fscanf@plt>
add    $0x1,%r14
movslq 0x18(%rsp),%rax
add    $0x4,%rbx
add    $0x4,%rbp
cmp    %rax,%r14
jl     11e0 <main+0x90>
cmpl   $0x0,0x14(%rsp)
jle    12a6 <main+0x156>
lea    0x10(%rsp),%r14
lea    0x1c(%rsp),%rbx
xor    %ebp,%ebp
jmp    1239 <main+0xe9>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    0x14(%rsp),%ebp
jge    12a6 <main+0x156>
mov    %r12,%rdi
lea    0xdd1(%rip),%rsi
lea    0xf(%rsp),%rdx
mov    %r14,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1030 <fscanf@plt>
mov    0x1c(%rsp),%eax
mov    %eax,%esi
neg    %esi
cmpb   $0x1,0xf(%rsp)
cmove  %eax,%esi
movslq 0x10(%rsp),%rdx
lea    -0x1(%rdx),%rax
add    -0x4(%r13,%rdx,4),%esi
mov    %eax,0x10(%rsp)
mov    %esi,-0x4(%r13,%rdx,4)
mov    -0x4(%r15,%rdx,4),%ecx
sub    %esi,%ecx
jle    1230 <main+0xe0>
mov    0x20(%rsp),%rdi
lea    0xd8e(%rip),%rsi
call   1040 <fprintf@plt>
movslq 0x10(%rsp),%rax
mov    (%r15,%rax,4),%ecx
mov    %ecx,0x0(%r13,%rax,4)
jmp    1230 <main+0xe0>
xor    %eax,%eax
call   1070 <fcloseall@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"INP.TXT\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"OUT.TXT\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"%d %d\00"
@.str.5 = [11 x i8] c"%hhd %d %d\00"
@.str.6 = [11 x i8] c"BUY %d %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
store i32 0, i32* %1
%12 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %2
%13 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %13, i32** %3
%14 = load i32*, i32** %2
%15 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %4, i32* %5)
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 4
%19 = trunc i64 %18 to i32
%20 = call i32* @malloc(i32 %19)
store i32* %20, i32** %7
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 4
%24 = trunc i64 %23 to i32
%25 = call i32* @malloc(i32 %24)
store i32* %25, i32** %8
store i32 0, i32* %6
br label %26
26: ; preds = %41, %0
%27 = load i32, i32* %6
%28 = load i32, i32* %4
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %44
30: ; preds = %26
%31 = load i32*, i32** %2
%32 = load i32*, i32** %7
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32*, i32** %8
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = call i32 (i32*, i8*, ...) @fscanf(i32* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %35, i32* %39)
br label %41
41: ; preds = %30
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %26
44: ; preds = %26
store i32 0, i32* %6
br label %45
45: ; preds = %108, %44
%46 = load i32, i32* %6
%47 = load i32, i32* %5
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %111
49: ; preds = %45
%50 = load i32*, i32** %2
%51 = call i32 (i32*, i8*, ...) @fscanf(i32* %50, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %11, i32* %9, i32* %10)
%52 = load i32, i32* %9
%53 = add nsw i32 %52, -1
store i32 %53, i32* %9
%54 = load i8, i8* %11
%55 = sext i8 %54 to i32
%56 = xor i32 %55, 1
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %49
%59 = load i32, i32* %10
%60 = sub nsw i32 0, %59
br label %63
61: ; preds = %49
%62 = load i32, i32* %10
br label %63
63: ; preds = %61, %58
%64 = phi i32 [ %60, %58 ], [ %62, %61 ]
%65 = load i32*, i32** %8
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %65, i64 %67
%69 = load i32, i32* %68
%70 = add nsw i32 %69, %64
store i32 %70, i32* %68
%71 = load i32*, i32** %8
%72 = load i32, i32* %9
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = load i32*, i32** %7
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32, i32* %76, i64 %78
%80 = load i32, i32* %79
%81 = icmp slt i32 %75, %80
br i1 %81, label %82, label %107
82: ; preds = %63
%83 = load i32*, i32** %3
%84 = load i32, i32* %9
%85 = add nsw i32 %84, 1
%86 = load i32*, i32** %7
%87 = load i32, i32* %9
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i32, i32* %86, i64 %88
%90 = load i32, i32* %89
%91 = load i32*, i32** %8
%92 = load i32, i32* %9
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i32, i32* %91, i64 %93
%95 = load i32, i32* %94
%96 = sub nsw i32 %90, %95
%97 = call i32 @fprintf(i32* %83, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %85, i32 %96)
%98 = load i32*, i32** %7
%99 = load i32, i32* %9
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i32, i32* %98, i64 %100
%102 = load i32, i32* %101
%103 = load i32*, i32** %8
%104 = load i32, i32* %9
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds i32, i32* %103, i64 %105
store i32 %102, i32* %106
br label %107
107: ; preds = %82, %63
br label %108
108: ; preds = %107
%109 = load i32, i32* %6
%110 = add nsw i32 %109, 1
store i32 %110, i32* %6
br label %45
111: ; preds = %45
%112 = call i32 (...) @fcloseall()
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"INP.TXT\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"OUT.TXT\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"%d %d\00"
@.str.5 = [11 x i8] c"%hhd %d %d\00"
@.str.6 = [11 x i8] c"BUY %d %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = bitcast i32* %1 to i8*
%9 = bitcast i32* %2 to i8*
%10 = bitcast i32* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = call i32 (i32*, i8*, ...) @fscanf(i32* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %1, i32* %2)
%13 = load i32, i32* %1
%14 = shl i32 %13, 2
%15 = call i32* @malloc(i32 %14)
%16 = call i32* @malloc(i32 %14)
%17 = icmp sgt i32 %13, 0
br i1 %17, label %21, label %18
18: ; preds = %21, %0
%19 = load i32, i32* %2
%20 = icmp sgt i32 %19, 0
br i1 %20, label %30, label %59
21: ; preds = %0, %21
%22 = phi i64 [ %26, %21 ], [ 0, %0 ]
%23 = getelementptr inbounds i32, i32* %15, i64 %22
%24 = getelementptr inbounds i32, i32* %16, i64 %22
%25 = call i32 (i32*, i8*, ...) @fscanf(i32* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %23, i32* %24)
%26 = add nuw nsw i64 %22, 1
%27 = load i32, i32* %1
%28 = sext i32 %27 to i64
%29 = icmp slt i64 %26, %28
br i1 %29, label %21, label %18
30: ; preds = %18, %55
%31 = phi i32 [ %56, %55 ], [ 0, %18 ]
%32 = call i32 (i32*, i8*, ...) @fscanf(i32* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %5, i32* %3, i32* %4)
%33 = load i32, i32* %3
%34 = add nsw i32 %33, -1
store i32 %34, i32* %3
%35 = load i8, i8* %5
%36 = icmp eq i8 %35, 1
%37 = load i32, i32* %4
%38 = sub nsw i32 0, %37
%39 = select i1 %36, i32 %37, i32 %38
%40 = sext i32 %34 to i64
%41 = getelementptr inbounds i32, i32* %16, i64 %40
%42 = load i32, i32* %41
%43 = add nsw i32 %42, %39
store i32 %43, i32* %41
%44 = getelementptr inbounds i32, i32* %15, i64 %40
%45 = load i32, i32* %44
%46 = icmp sgt i32 %45, %43
br i1 %46, label %47, label %55
47: ; preds = %30
%48 = sub nsw i32 %45, %43
%49 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i32 %33, i32 %48)
%50 = load i32, i32* %3
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %15, i64 %51
%53 = load i32, i32* %52
%54 = getelementptr inbounds i32, i32* %16, i64 %51
store i32 %53, i32* %54
br label %55
55: ; preds = %30, %47
%56 = add nuw nsw i32 %31, 1
%57 = load i32, i32* %2
%58 = icmp slt i32 %56, %57
br i1 %58, label %30, label %59
59: ; preds = %55, %18
%60 = call i32 (...) @fcloseall()
ret i32 0
}