#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 /*<<< orphan*/ close(int);
int net_splice(int, int, int);
int /*<<< orphan*/ net_sys_err(char *);
int pipe(int *);
int net_exchange(int sock_in, int sock_out, size_t nbytes) {
  int ret;
  int pipefd[2];
  ret = pipe(pipefd);
  if (ret < 0)
    net_sys_err("pipe()");
  ret = net_splice(sock_in, pipefd[1], nbytes);
  if (ret <= 0)
    return ret;
  ret = net_splice(pipefd[0], sock_out, ret);
  if (ret <= 0)
    return ret;
  close(pipefd[0]);
  close(pipefd[1]);
  return ret;
}


x86 assembly (clang, -O0):
<net_exchange>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1174 <net_exchange+0x34>
lea    0xe91(%rip),%rdi
call   1060 <net_sys_err@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
call   1050 <net_splice@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     119d <net_exchange+0x5d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d9 <net_exchange+0x99>
mov    -0x24(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1050 <net_splice@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11c3 <net_exchange+0x83>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d9 <net_exchange+0x99>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_exchange>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
call   1040 <pipe@plt>
test   %eax,%eax
jns    116a <net_exchange+0x2a>
lea    0xe9b(%rip),%rdi
call   1060 <net_sys_err@plt>
mov    0xc(%rsp),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1050 <net_splice@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11a3 <net_exchange+0x63>
mov    0x8(%rsp),%edi
mov    %r14d,%esi
mov    %ebp,%edx
call   1050 <net_splice@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11a3 <net_exchange+0x63>
mov    0x8(%rsp),%edi
call   1030 <close@plt>
mov    0xc(%rsp),%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"pipe()\00"
define i32 @net_exchange(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca [2 x i32]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @net_sys_err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i32, i32* %5
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i64, i64* %7
%21 = trunc i64 %20 to i32
%22 = call i32 @net_splice(i32 %17, i32 %19, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %45
27: ; preds = %16
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%29 = load i32, i32* %28
%30 = load i32, i32* %6
%31 = load i32, i32* %8
%32 = call i32 @net_splice(i32 %29, i32 %30, i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %45
37: ; preds = %27
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %8
store i32 %44, i32* %4
br label %45
45: ; preds = %37, %35, %25
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@.str = [7 x i8] c"pipe()\00"
define i32 @net_exchange(i32 %0, i32 %1, i64 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 @net_sys_err(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %3
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%13 = load i32, i32* %12
%14 = trunc i64 %2 to i32
%15 = call i32 @net_splice(i32 %0, i32 %13, i32 %14)
%16 = icmp slt i32 %15, 1
br i1 %16, label %26, label %17
17: ; preds = %11
%18 = load i32, i32* %6
%19 = call i32 @net_splice(i32 %18, i32 %1, i32 %15)
%20 = icmp slt i32 %19, 1
br i1 %20, label %26, label %21
21: ; preds = %17
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %12
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %17, %11, %21
%27 = phi i32 [ %19, %21 ], [ %15, %11 ], [ %19, %17 ]
ret i32 %27
}