#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*/ try_read_some(int, void **, unsigned int *);
int /*<<< orphan*/ try_write(int, void *, int);
int try_relaying(int fromfd, int tofd, void *buf) {
  void *bufp;
  unsigned size_rem;
  size_rem = 4096;
  bufp = buf;
  if (!try_read_some(fromfd, &bufp, &size_rem))
    return 0;
  if (size_rem != 4096)
    if (!try_write(tofd, buf, 4096 - size_rem))
      return 0;
  return 1;
}


x86 assembly (clang, -O0):
<try_relaying>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1000,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <try_read_some@plt>
cmp    $0x0,%eax
jne    1166 <try_relaying+0x46>
movl   $0x0,-0x4(%rbp)
jmp    11a8 <try_relaying+0x88>
cmpl   $0x1000,-0x24(%rbp)
je     11a1 <try_relaying+0x81>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
sub    -0x24(%rbp),%edx
call   1030 <try_write@plt>
cmp    $0x0,%eax
jne    119c <try_relaying+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    11a8 <try_relaying+0x88>
jmp    11a1 <try_relaying+0x81>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_relaying>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
movl   $0x1000,0x4(%rsp)
mov    %rdx,0x8(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1040 <try_read_some@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     1174 <try_relaying+0x54>
mov    0x4(%rsp),%eax
cmp    $0x1000,%eax
je     116f <try_relaying+0x4f>
mov    $0x1000,%edx
sub    %eax,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <try_write@plt>
test   %eax,%eax
je     1174 <try_relaying+0x54>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @try_relaying(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 4096, i32* %9
%10 = load i8*, i8** %7
store i8* %10, i8** %8
%11 = load i32, i32* %5
%12 = call i32 @try_read_some(i32 %11, i8** %8, i32* %9)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %3
store i32 0, i32* %4
br label %28
15: ; preds = %3
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 4096
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i32, i32* %6
%20 = load i8*, i8** %7
%21 = load i32, i32* %9
%22 = sub i32 4096, %21
%23 = call i32 @try_write(i32 %19, i8* %20, i32 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %18
store i32 0, i32* %4
br label %28
26: ; preds = %18
br label %27
27: ; preds = %26, %15
store i32 1, i32* %4
br label %28
28: ; preds = %27, %25, %14
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
define i32 @try_relaying(i32 %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast i8** %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4096, i32* %5
store i8* %2, i8** %4
%8 = call i32 @try_read_some(i32 %0, i8** %4, i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %3
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 4096
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = sub i32 4096, %11
%15 = call i32 @try_write(i32 %1, i8* %2, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %13, %10
br label %18
18: ; preds = %13, %3, %17
%19 = phi i32 [ 1, %17 ], [ 0, %3 ], [ 0, %13 ]
ret i32 %19
}