#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 */
struct gdbserv {
  int dummy;
};

/* Variables and functions */
int gdbserv_input_char(struct gdbserv *);
int gdbserv_input_escaped_binary(struct gdbserv *gdbserv, char *buf, int len) {
  int i;
  int ch;
  for (i = 0; i < len; i++) {
    ch = gdbserv_input_char(gdbserv);
    if (ch < 0)
      return ch;
    if (ch == 0x7d) {
      ch = gdbserv_input_char(gdbserv);
      if (ch < 0)
        return ch;
      buf[i] = 0x20 | (ch & 0xff);
    } else {
      buf[i] = ch;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<gdbserv_input_escaped_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c2 <gdbserv_input_escaped_binary+0xb2>
mov    -0x10(%rbp),%rdi
call   1030 <gdbserv_input_char@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1157 <gdbserv_input_escaped_binary+0x47>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c8 <gdbserv_input_escaped_binary+0xb8>
cmpl   $0x7d,-0x24(%rbp)
jne    119f <gdbserv_input_escaped_binary+0x8f>
mov    -0x10(%rbp),%rdi
call   1030 <gdbserv_input_char@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1182 <gdbserv_input_escaped_binary+0x72>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c8 <gdbserv_input_escaped_binary+0xb8>
mov    -0x24(%rbp),%eax
and    $0xff,%eax
or     $0x20,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11af <gdbserv_input_escaped_binary+0x9f>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11b4 <gdbserv_input_escaped_binary+0xa4>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    112a <gdbserv_input_escaped_binary+0x1a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdbserv_input_escaped_binary>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    115e <gdbserv_input_escaped_binary+0x4e>
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %edx,%r12d
xor    %ebx,%ebx
jmp    113d <gdbserv_input_escaped_binary+0x2d>
nopl   0x0(%rax)
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
je     1162 <gdbserv_input_escaped_binary+0x52>
mov    %rbp,%rdi
call   1030 <gdbserv_input_char@plt>
test   %eax,%eax
js     1165 <gdbserv_input_escaped_binary+0x55>
cmp    $0x7d,%eax
jne    1130 <gdbserv_input_escaped_binary+0x20>
mov    %rbp,%rdi
call   1030 <gdbserv_input_char@plt>
test   %eax,%eax
js     1165 <gdbserv_input_escaped_binary+0x55>
or     $0x20,%al
jmp    1130 <gdbserv_input_escaped_binary+0x20>
xor    %eax,%eax
jmp    1165 <gdbserv_input_escaped_binary+0x55>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.gdbserv = type { i32 }
define i32 @gdbserv_input_escaped_binary(%struct.gdbserv* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.gdbserv*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.gdbserv* %0, %struct.gdbserv** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %48, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %51
14: ; preds = %10
%15 = load %struct.gdbserv*, %struct.gdbserv** %5
%16 = call i32 @gdbserv_input_char(%struct.gdbserv* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* %9
store i32 %20, i32* %4
br label %53
21: ; preds = %14
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, 125
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = load %struct.gdbserv*, %struct.gdbserv** %5
%26 = call i32 @gdbserv_input_char(%struct.gdbserv* %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* %9
store i32 %30, i32* %4
br label %53
31: ; preds = %24
%32 = load i32, i32* %9
%33 = and i32 %32, 255
%34 = or i32 32, %33
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %6
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
br label %47
40: ; preds = %21
%41 = load i32, i32* %9
%42 = trunc i32 %41 to i8
%43 = load i8*, i8** %6
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 %42, i8* %46
br label %47
47: ; preds = %40, %31
br label %48
48: ; preds = %47
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %10
51: ; preds = %10
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %51, %29, %19
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
%struct.gdbserv = type { i32 }
define i32 @gdbserv_input_escaped_binary(%struct.gdbserv* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %26
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %21
%8 = phi i64 [ 0, %5 ], [ %24, %21 ]
%9 = tail call i32 @gdbserv_input_char(%struct.gdbserv* %0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %7
%12 = icmp eq i32 %9, 125
br i1 %12, label %13, label %19
13: ; preds = %11
%14 = tail call i32 @gdbserv_input_char(%struct.gdbserv* %0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %13
%17 = trunc i32 %14 to i8
%18 = or i8 %17, 32
br label %21
19: ; preds = %11
%20 = trunc i32 %9 to i8
br label %21
21: ; preds = %16, %19
%22 = phi i8 [ %18, %16 ], [ %20, %19 ]
%23 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 %22, i8* %23
%24 = add nuw nsw i64 %8, 1
%25 = icmp eq i64 %24, %6
br i1 %25, label %26, label %7
26: ; preds = %7, %13, %21, %3
%27 = phi i32 [ 0, %3 ], [ %2, %21 ], [ %14, %13 ], [ %9, %7 ]
ret i32 %27
}