#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 sockaddr_in {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int net_recv(int, char *, int, struct sockaddr_in *, int);
int /*<<< orphan*/ net_send(int, char *, int, struct sockaddr_in *, int);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void echo_driver(int fd, struct sockaddr_in *serv_addr) {
  char buf[512];
  int n;
  struct sockaddr_in fm_addr;
  while ((n = read(0, buf, sizeof(buf))) != 0) {
    net_send(fd, buf, n, serv_addr, sizeof(struct sockaddr_in));
    n = net_recv(fd, buf, sizeof(buf), &fm_addr, sizeof(struct sockaddr_in));
    write(1, buf, n);
  }
  return;
}


x86 assembly (clang, -O0):
<echo_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x210(%rbp),%rsi
xor    %edi,%edi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
je     11d6 <echo_driver+0x96>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <net_send@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
lea    -0x218(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <net_recv@plt>
mov    %eax,-0x214(%rbp)
lea    -0x210(%rbp),%rsi
mov    -0x214(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1152 <echo_driver+0x12>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_driver>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11cf <echo_driver+0x8f>
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %r14,%rcx
mov    $0x4,%r8d
call   1040 <net_send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
mov    %r15,%rcx
mov    $0x4,%r8d
call   1060 <net_recv@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1180 <echo_driver+0x40>
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
define void @echo_driver(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in*
%5 = alloca [512 x i8]
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %4
br label %8
8: ; preds = %12, %2
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%10 = call i32 @read(i32 0, i8* %9, i32 512)
store i32 %10, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load i32, i32* %3
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%15 = load i32, i32* %6
%16 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%17 = call i32 @net_send(i32 %13, i8* %14, i32 %15, %struct.sockaddr_in* %16, i32 4)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%20 = call i32 @net_recv(i32 %18, i8* %19, i32 512, %struct.sockaddr_in* %7, i32 4)
store i32 %20, i32* %6
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%22 = load i32, i32* %6
%23 = call i32 @write(i32 1, i8* %21, i32 %22)
br label %8
24: ; preds = %8
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
define void @echo_driver(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca [512 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @read(i32 0, i8* %5, i32 512)
%8 = icmp eq i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %2, %9
%10 = phi i32 [ %14, %9 ], [ %7, %2 ]
%11 = call i32 @net_send(i32 %0, i8* %5, i32 %10, %struct.sockaddr_in* %1, i32 4)
%12 = call i32 @net_recv(i32 %0, i8* %5, i32 512, %struct.sockaddr_in* %4, i32 4)
%13 = call i32 @write(i32 1, i8* %5, i32 %12)
%14 = call i32 @read(i32 0, i8* %5, i32 512)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %9
16: ; preds = %9, %2
ret void
}