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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ MSG_DONTWAIT;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void read_some(int connfd) {
  int ret = 0;
  char buf[512 + 1] = {0};
  usleep(100000);
  do {
    ret = recv(connfd, buf, sizeof(buf), MSG_DONTWAIT);
    if (ret <= 0) {
      if (errno == EAGAIN)
        break;
      printf("recv failed, errno %d\n", errno);
      break;
    }
    buf[ret] = 0;
    printf("%s", buf);
  } while (1);
}


x86 assembly (clang, -O0):
<read_some>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1050 <memset@plt>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
mov    $0x201,%edx
call   1030 <recv@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     11d6 <read_some+0x96>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <read_some+0x7b>
jmp    1204 <read_some+0xc4>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rsi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
jmp    1204 <read_some+0xc4>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rsi
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    1172 <read_some+0x32>
jmp    1204 <read_some+0xc4>
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_some>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
mov    $0x201,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
mov    0x2e66(%rip),%r15
mov    (%r15),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x201,%edx
call   1030 <recv@plt>
test   %eax,%eax
jle    11c7 <read_some+0x87>
lea    0xe88(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    (%r15),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x201,%edx
call   1030 <recv@plt>
test   %eax,%eax
jg     11a0 <read_some+0x60>
mov    0x2e12(%rip),%rax
mov    (%rax),%rsi
mov    0x2df0(%rip),%rax
cmp    (%rax),%rsi
je     11e9 <read_some+0xa9>
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_DONTWAIT = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [23 x i8] c"recv failed, errno %d\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define void @read_some(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [513 x i8]
store i32 %0, i32* %2
store i32 0, i32* %3
%5 = bitcast [513 x i8]* %4 to i8*
%6 = call i32 @usleep(i32 100000)
br label %7
7: ; preds = %29, %1
%8 = load i32, i32* %2
%9 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @MSG_DONTWAIT
%11 = call i32 @recv(i32 %8, i8* %9, i32 513, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %7
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
br label %30
19: ; preds = %14
%20 = load i64, i64* @errno
%21 = inttoptr i64 %20 to i8*
%22 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %30
23: ; preds = %7
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%28 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %23
br i1 true, label %7, label %30
30: ; preds = %29, %19, %18
ret void
}

LLVM IR (-O2):
@MSG_DONTWAIT = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [23 x i8] c"recv failed, errno %d\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define void @read_some(i32 %0) {
%2 = alloca [513 x i8]
%3 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @usleep(i32 100000)
%5 = load i32, i32* @MSG_DONTWAIT
%6 = call i32 @recv(i32 %0, i8* %3, i32 513, i32 %5)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %15
8: ; preds = %15, %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAGAIN
%11 = icmp eq i64 %9, %10
br i1 %11, label %23, label %12
12: ; preds = %8
%13 = inttoptr i64 %9 to i8*
%14 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %23
15: ; preds = %1, %15
%16 = phi i32 [ %21, %15 ], [ %6, %1 ]
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 %17
store i8 0, i8* %18
%19 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%20 = load i32, i32* @MSG_DONTWAIT
%21 = call i32 @recv(i32 %0, i8* %3, i32 513, i32 %20)
%22 = icmp slt i32 %21, 1
br i1 %22, label %8, label %15
23: ; preds = %8, %12
ret void
}