#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*/ F_GETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ read(int, char *, unsigned int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ write(int, char *, unsigned int);
void do_msg(int fdin, int fdout) {
  char buf[1024];
  unsigned int len;
  int flags = fcntl(fdin, F_GETFL, 0);
  fcntl(fdin, F_GETFL, flags | (~O_NONBLOCK));
  read(fdin, buf, 6);
  buf[6] = '\0';
  sscanf(buf, "%x", &len);
  for (; len >= 1024; len -= 1024) {
    read(fdin, buf, 1024);
    write(fdout, buf, 1024);
  }
  read(fdin, buf, len);
  write(fdout, buf, len);
  fcntl(fdin, F_GETFL, flags);
}


x86 assembly (clang, -O0):
<do_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x418(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    -0x418(%rbp),%edx
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
or     %eax,%edx
call   1030 <fcntl@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x6,%edx
call   1050 <read@plt>
movb   $0x0,-0x40a(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xe48(%rip),%rsi
lea    -0x414(%rbp),%rdx
call   1060 <sscanf@plt>
cmpl   $0x400,-0x414(%rbp)
jb     1212 <do_msg+0xd2>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    -0x414(%rbp),%eax
sub    $0x400,%eax
mov    %eax,-0x414(%rbp)
jmp    11c4 <do_msg+0x84>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1040 <write@plt>
mov    -0x4(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    -0x418(%rbp),%edx
call   1030 <fcntl@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
mov    0x2e7b(%rip),%r12
mov    (%r12),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%r14d
mov    (%r12),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
not    %edx
or     %r14d,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
lea    0x10(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x6,%edx
call   1050 <read@plt>
movb   $0x0,0x16(%rsp)
lea    0xe5c(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbp,%rdi
call   1060 <sscanf@plt>
mov    0xc(%rsp),%edx
cmp    $0x400,%edx
jb     1202 <do_msg+0xc2>
lea    0x10(%rsp),%rbp
mov    $0xfffffc00,%r13d
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edx
add    %r13d,%edx
mov    %edx,0xc(%rsp)
cmp    $0x3ff,%edx
ja     11d0 <do_msg+0x90>
lea    0x10(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <read@plt>
mov    0xc(%rsp),%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1040 <write@plt>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [3 x i8] c"%x\00"
define void @do_msg(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = load i32, i32* @F_GETFL
%10 = call i32 @fcntl(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %3
%12 = load i32, i32* @F_GETFL
%13 = load i32, i32* %7
%14 = load i32, i32* @O_NONBLOCK
%15 = xor i32 %14, -1
%16 = or i32 %13, %15
%17 = call i32 @fcntl(i32 %11, i32 %12, i32 %16)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 6)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 6
store i8 0, i8* %21
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
br label %24
24: ; preds = %34, %2
%25 = load i32, i32* %6
%26 = icmp uge i32 %25, 1024
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i32, i32* %3
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 1024)
%31 = load i32, i32* %4
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = call i32 @write(i32 %31, i8* %32, i32 1024)
br label %34
34: ; preds = %27
%35 = load i32, i32* %6
%36 = sub i32 %35, 1024
store i32 %36, i32* %6
br label %24
37: ; preds = %24
%38 = load i32, i32* %3
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%40 = load i32, i32* %6
%41 = call i32 @read(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %4
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %6
%45 = call i32 @write(i32 %42, i8* %43, i32 %44)
%46 = load i32, i32* %3
%47 = load i32, i32* @F_GETFL
%48 = load i32, i32* %7
%49 = call i32 @fcntl(i32 %46, i32 %47, i32 %48)
ret void
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [3 x i8] c"%x\00"
define void @do_msg(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @F_GETFL
%8 = tail call i32 @fcntl(i32 %0, i32 %7, i32 0)
%9 = load i32, i32* @F_GETFL
%10 = load i32, i32* @O_NONBLOCK
%11 = xor i32 %10, -1
%12 = or i32 %8, %11
%13 = tail call i32 @fcntl(i32 %0, i32 %9, i32 %12)
%14 = call i32 @read(i32 %0, i8* %5, i32 6)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 6
store i8 0, i8* %15
%16 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%17 = load i32, i32* %4
%18 = icmp ugt i32 %17, 1023
br i1 %18, label %19, label %25
19: ; preds = %2, %19
%20 = call i32 @read(i32 %0, i8* %5, i32 1024)
%21 = call i32 @write(i32 %1, i8* %5, i32 1024)
%22 = load i32, i32* %4
%23 = add i32 %22, -1024
store i32 %23, i32* %4
%24 = icmp ugt i32 %23, 1023
br i1 %24, label %19, label %25
25: ; preds = %19, %2
%26 = phi i32 [ %17, %2 ], [ %23, %19 ]
%27 = call i32 @read(i32 %0, i8* %5, i32 %26)
%28 = load i32, i32* %4
%29 = call i32 @write(i32 %1, i8* %5, i32 %28)
%30 = load i32, i32* @F_GETFL
%31 = call i32 @fcntl(i32 %0, i32 %30, i32 %8)
ret void
}