#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 */

/* Variables and functions */
int read_b(int, char *);
int receive_string(int port, char *dst, int data_len) {
  int read_result;
  char recv;
  char *pch;
  int i;
  for (pch = dst, i = 0; i < data_len; i++, pch++) {
    read_result = read_b(port, pch);
    if (read_result == -1 || read_result == 0)
      break;
  }
  return read_result;
}


x86 assembly (clang, -O0):
<receive_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1184 <receive_string+0x74>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1030 <read_b@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     1160 <receive_string+0x50>
cmpl   $0x0,-0x18(%rbp)
jne    1165 <receive_string+0x55>
jmp    1184 <receive_string+0x74>
jmp    116a <receive_string+0x5a>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1131 <receive_string+0x21>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1155 <receive_string+0x45>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x1,%ebp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <read_b@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
cmp    $0xfffffffd,%ecx
ja     1155 <receive_string+0x45>
add    $0x1,%rbx
lea    0x1(%rbp),%ecx
cmp    %r14d,%ebp
mov    %ecx,%ebp
jl     1130 <receive_string+0x20>
jmp    1155 <receive_string+0x45>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @receive_string(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %15
5: ; preds = %3, %5
%6 = phi i32 [ %11, %5 ], [ 0, %3 ]
%7 = phi i8* [ %12, %5 ], [ %1, %3 ]
%8 = tail call i32 @read_b(i32 %0, i8* %7)
%9 = add i32 %8, -1
%10 = icmp ult i32 %9, -2
%11 = add nuw nsw i32 %6, 1
%12 = getelementptr inbounds i8, i8* %7, i64 1
%13 = icmp slt i32 %11, %2
%14 = select i1 %10, i1 %13, i1 false
br i1 %14, label %5, label %15
15: ; preds = %5, %3
%16 = phi i32 [ undef, %3 ], [ %8, %5 ]
ret i32 %16
}