#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 */
scalar_t__ nw_read(char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ nw_write(char *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/ *);
void *udp_echo(void *arg) {
  ssize_t ret;
  char buf[8192];
  while (1) {
    if ((ret = nw_read(buf, 8192, 0, NULL)) < 0)
      return (NULL);
    if (ret == 0)
      continue;
    nw_write(buf, 8192, 0, NULL);
  }
}


x86 assembly (clang, -O0):
<udp_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2010,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x2010(%rbp),%rdi
mov    $0x2000,%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <nw_read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    115f <udp_echo+0x3f>
xor    %eax,%eax
add    $0x2010,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x10(%rbp)
jne    116f <udp_echo+0x4f>
jmp    112f <udp_echo+0xf>
lea    -0x2010(%rbp),%rdi
mov    $0x2000,%esi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <nw_write@plt>
jmp    112f <udp_echo+0xf>

x86 assembly (clang, -O2):
<udp_echo>:
push   %rbx
sub    $0x2000,%rsp
mov    %rsp,%rdi
mov    $0x2000,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <nw_read@plt>
test   %rax,%rax
js     117e <udp_echo+0x5e>
mov    %rsp,%rbx
jmp    1166 <udp_echo+0x46>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x2000,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <nw_read@plt>
test   %rax,%rax
js     117e <udp_echo+0x5e>
test   %rax,%rax
je     1150 <udp_echo+0x30>
mov    %rbx,%rdi
mov    $0x2000,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <nw_write@plt>
jmp    1150 <udp_echo+0x30>
xor    %eax,%eax
add    $0x2000,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i8* @udp_echo(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [8192 x i8]
store i8* %0, i8** %2
br label %5
5: ; preds = %1, %13, %14
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%7 = call i64 @nw_read(i8* %6, i32 8192, i32 0, i32* null)
store i64 %7, i64* %3
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %5
ret i8* null
10: ; preds = %5
%11 = load i64, i64* %3
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10
br label %5
14: ; preds = %10
%15 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%16 = call i32 @nw_write(i8* %15, i32 8192, i32 0, i32* null)
br label %5
}

LLVM IR (-O2):
define noalias i8* @udp_echo(i8* nocapture readnone %0) {
%2 = alloca [8192 x i8]
%3 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%4 = call i64 @nw_read(i8* %3, i32 8192, i32 0, i32* null)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %12, %1
ret i8* null
7: ; preds = %1, %12
%8 = phi i64 [ %13, %12 ], [ %4, %1 ]
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = call i32 @nw_write(i8* %3, i32 8192, i32 0, i32* null)
br label %12
12: ; preds = %10, %7
%13 = call i64 @nw_read(i8* %3, i32 8192, i32 0, i32* null)
%14 = icmp slt i64 %13, 0
br i1 %14, label %6, label %7
}