#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_string(struct gdbserv *gdbserv, char *buf, int len) {
  int i;
  for (i = 0; i < (len - 1); i++) {
    int byte = gdbserv_input_char(gdbserv);
    if (byte < 0)
      break;
    buf[i] = byte;
  }
  buf[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<gdbserv_input_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1174 <gdbserv_input_string+0x64>
mov    -0x8(%rbp),%rdi
call   1030 <gdbserv_input_char@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1156 <gdbserv_input_string+0x46>
jmp    1174 <gdbserv_input_string+0x64>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112a <gdbserv_input_string+0x1a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdbserv_input_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
xor    %ebx,%ebx
cmp    $0x2,%edx
jl     115c <gdbserv_input_string+0x4c>
mov    %edx,%r15d
mov    %rdi,%r12
add    $0xffffffff,%r15d
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <gdbserv_input_char@plt>
test   %eax,%eax
js     115c <gdbserv_input_string+0x4c>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1140 <gdbserv_input_string+0x30>
mov    %r15d,%ebx
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.gdbserv = type { i32 }
define i32 @gdbserv_input_string(%struct.gdbserv* %0, i8* %1, i32 %2) {
%4 = alloca %struct.gdbserv*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.gdbserv* %0, %struct.gdbserv** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %27, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
%13 = icmp slt i32 %10, %12
br i1 %13, label %14, label %30
14: ; preds = %9
%15 = load %struct.gdbserv*, %struct.gdbserv** %4
%16 = call i32 @gdbserv_input_char(%struct.gdbserv* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
br label %30
20: ; preds = %14
%21 = load i32, i32* %8
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %20
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %9
30: ; preds = %19, %9
%31 = load i8*, i8** %5
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %7
ret i32 %35
}

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