#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
void *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ recv(int, void *, size_t, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void receive_frames(int sock) {
  size_t max_buffer_size = 512;
  void *rxBuffer = malloc(max_buffer_size);
  ssize_t rxSize = 0;
  unsigned char *charBuffer = (unsigned char *)rxBuffer;
  while ((rxSize = recv(sock, rxBuffer, max_buffer_size, 0)) == 0)
    usleep(1);
  if (rxSize > 0) {
    printf("Got response '0x");
    for (unsigned i = 0; i < rxSize; i++)
      printf("%02x", charBuffer[i]);
    printf("'\n");
  } else {
    printf("Error waiting for response: %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<receive_frames>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x200,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11af <receive_frames+0x5f>
mov    $0x1,%edi
call   1070 <usleep@plt>
jmp    1180 <receive_frames+0x30>
cmpq   $0x0,-0x20(%rbp)
jle    1216 <receive_frames+0xc6>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%rax
jge    1203 <receive_frames+0xb3>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movzbl (%rax,%rcx,1),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cf <receive_frames+0x7f>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1235 <receive_frames+0xe5>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_frames>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x200,%edx
mov    %ebp,%edi
mov    %rax,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11e0 <receive_frames+0x80>
test   %rbx,%rbx
jle    1208 <receive_frames+0xa8>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe68(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
cmp    %rbp,%rbx
jg     11b0 <receive_frames+0x50>
lea    0xe68(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
nopl   (%rax)
mov    $0x1,%edi
call   1080 <usleep@plt>
mov    $0x200,%edx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     11e0 <receive_frames+0x80>
mov    %rax,%rbx
test   %rbx,%rbx
jg     1194 <receive_frames+0x34>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdf9(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Got response '0x\00"
@.str.1 = [5 x i8] c"%02x\00"
@.str.2 = [3 x i8] c"'\0A\00"
@.str.3 = [32 x i8] c"Error waiting for response: %s\0A\00"
@errno = global i32 0
define void @receive_frames(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %2
store i64 512, i64* %3
%8 = load i64, i64* %3
%9 = call i8* @malloc(i64 %8)
store i8* %9, i8** %4
store i64 0, i64* %5
%10 = load i8*, i8** %4
store i8* %10, i8** %6
br label %11
11: ; preds = %17, %1
%12 = load i32, i32* %2
%13 = load i8*, i8** %4
%14 = load i64, i64* %3
%15 = call i64 @recv(i32 %12, i8* %13, i64 %14, i32 0)
store i64 %15, i64* %5
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @usleep(i32 1)
br label %11
19: ; preds = %11
%20 = load i64, i64* %5
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %42
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %24
24: ; preds = %37, %22
%25 = load i32, i32* %7
%26 = zext i32 %25 to i64
%27 = load i64, i64* %5
%28 = icmp slt i64 %26, %27
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %29
%38 = load i32, i32* %7
%39 = add i32 %38, 1
store i32 %39, i32* %7
br label %24
40: ; preds = %24
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %46
42: ; preds = %19
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %42, %40
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"Got response '0x\00"
@.str.1 = [5 x i8] c"%02x\00"
@.str.3 = [32 x i8] c"Error waiting for response: %s\0A\00"
@errno = global i32 0
@str = [2 x i8] c"'\00"
define void @receive_frames(i32 %0) {
%2 = tail call dereferenceable_or_null(512) i8* @malloc(i64 512)
%3 = tail call i64 @recv(i32 %0, i8* %2, i64 512, i32 0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %1, %5
%6 = tail call i32 @usleep(i32 1)
%7 = tail call i64 @recv(i32 %0, i8* %2, i64 512, i32 0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %5, label %9
9: ; preds = %5, %1
%10 = phi i64 [ %3, %1 ], [ %7, %5 ]
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %16
14: ; preds = %16
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %29
16: ; preds = %12, %16
%17 = phi i64 [ 0, %12 ], [ %23, %16 ]
%18 = getelementptr inbounds i8, i8* %2, i64 %17
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = add nuw nsw i64 %17, 1
%23 = and i64 %22, 4294967295
%24 = icmp sgt i64 %10, %23
br i1 %24, label %16, label %14
25: ; preds = %9
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %14
ret void
}