#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 int uint8_t;
typedef unsigned int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
unsigned int read(int, int *, int);
int /*<<< orphan*/ stderr;
unsigned int write(int, int *, unsigned int);
int copy_data(int src_fd, int dst_fd, const char *prefix) {
  uint8_t data[1024];
  ssize_t n1, n2;
  unsigned int i;
  n1 = read(src_fd, data, 1024);
  if (n1 <= 0)
    return -1;
  fprintf(stderr, "read data fd=%d, len=%zi\n", src_fd, n1);
  for (i = 0; i < n1; i++) {
    if (i % 16 == 0)
      fprintf(stderr, "%s: 0x%08x", prefix, i);
    fprintf(stderr, " %02x", data[i]);
    if ((i + 1) % 16 == 0)
      fprintf(stderr, "\n");
  }
  fprintf(stderr, "\n");
  n2 = write(dst_fd, data, n1);
  if (n2 <= 0 || n1 != n2)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x1024(%rbp)
cmpl   $0x0,-0x1024(%rbp)
ja     1178 <copy_data+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <copy_data+0x184>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0x1024(%rbp),%ecx
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x102c(%rbp)
mov    -0x102c(%rbp),%eax
cmp    -0x1024(%rbp),%eax
jae    1250 <copy_data+0x120>
mov    -0x102c(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    11e7 <copy_data+0xb7>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x102c(%rbp),%ecx
lea    0xe3a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x102c(%rbp),%eax
mov    -0x1020(%rbp,%rax,4),%edx
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x102c(%rbp),%eax
add    $0x1,%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    1237 <copy_data+0x107>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    123c <copy_data+0x10c>
mov    -0x102c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x102c(%rbp)
jmp    11a2 <copy_data+0x72>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
jbe    12a1 <copy_data+0x171>
mov    -0x1024(%rbp),%eax
cmp    -0x1028(%rbp),%eax
je     12ad <copy_data+0x17d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <copy_data+0x184>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdx,%r12
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1221 <copy_data+0xf1>
mov    %ebx,0xc(%rsp)
mov    %eax,%ebx
mov    0x2e74(%rip),%r15
mov    (%r15),%edi
lea    0xe8a(%rip),%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe9d(%rip),%rbp
lea    0xe89(%rip),%r13
xor    %r14d,%r14d
jmp    11a5 <copy_data+0x75>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %r14,%rbx
je     11ed <copy_data+0xbd>
test   $0xf,%r14b
jne    11c2 <copy_data+0x92>
mov    (%r15),%edi
lea    0xe65(%rip),%rsi
mov    %r12,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r15),%edi
mov    0x10(%rsp,%r14,4),%edx
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r14
test   $0xf,%r14b
jne    11a0 <copy_data+0x70>
mov    (%r15),%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11a0 <copy_data+0x70>
mov    (%r15),%edi
lea    0xe21(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0x10(%rsp),%rsi
mov    0xc(%rsp),%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
sete   %cl
cmp    %eax,%ebx
setne  %al
or     %cl,%al
movzbl %al,%eax
neg    %eax
jmp    1226 <copy_data+0xf6>
mov    $0xffffffff,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"read data fd=%d, len=%zi\0A\00"
@.str.1 = [11 x i8] c"%s: 0x%08x\00"
@.str.2 = [6 x i8] c" %02x\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @copy_data(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [1024 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = load i32, i32* %5
%13 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 0
%14 = call i32 @read(i32 %12, i32* %13, i32 1024)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp ule i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %69
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = load i32, i32* %5
%21 = load i32, i32* %9
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21)
store i32 0, i32* %11
br label %23
23: ; preds = %51, %18
%24 = load i32, i32* %11
%25 = load i32, i32* %9
%26 = icmp ult i32 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %23
%28 = load i32, i32* %11
%29 = urem i32 %28, 16
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %7
%34 = load i32, i32* %11
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %34)
br label %36
36: ; preds = %31, %27
%37 = load i32, i32* @stderr
%38 = load i32, i32* %11
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %11
%44 = add i32 %43, 1
%45 = urem i32 %44, 16
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %36
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %47, %36
br label %51
51: ; preds = %50
%52 = load i32, i32* %11
%53 = add i32 %52, 1
store i32 %53, i32* %11
br label %23
54: ; preds = %23
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = getelementptr inbounds [1024 x i32], [1024 x i32]* %8, i64 0, i64 0
%59 = load i32, i32* %9
%60 = call i32 @write(i32 %57, i32* %58, i32 %59)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp ule i32 %61, 0
br i1 %62, label %67, label %63
63: ; preds = %54
%64 = load i32, i32* %9
%65 = load i32, i32* %10
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63, %54
store i32 -1, i32* %4
br label %69
68: ; preds = %63
store i32 0, i32* %4
br label %69
69: ; preds = %68, %67, %17
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"read data fd=%d, len=%zi\0A\00"
@.str.1 = [11 x i8] c"%s: 0x%08x\00"
@.str.2 = [6 x i8] c" %02x\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @copy_data(i32 %0, i32 %1, i8* %2) {
%4 = alloca [1024 x i32]
%5 = bitcast [1024 x i32]* %4 to i8*
%6 = getelementptr inbounds [1024 x i32], [1024 x i32]* %4, i64 0, i64 0
%7 = call i32 @read(i32 %0, i32* %6, i32 1024)
%8 = icmp eq i32 %7, 0
br i1 %8, label %42, label %9
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %7)
%12 = zext i32 %7 to i64
br label %13
13: ; preds = %9, %32
%14 = phi i64 [ 0, %9 ], [ %26, %32 ]
%15 = trunc i64 %14 to i32
%16 = and i32 %15, 15
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32 %15)
br label %21
21: ; preds = %18, %13
%22 = load i32, i32* @stderr
%23 = getelementptr inbounds [1024 x i32], [1024 x i32]* %4, i64 0, i64 %14
%24 = load i32, i32* %23
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = add nuw nsw i64 %14, 1
%27 = and i64 %26, 15
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %21, %29
%33 = icmp eq i64 %26, %12
br i1 %33, label %34, label %13
34: ; preds = %32
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @write(i32 %1, i32* %6, i32 %7)
%38 = icmp eq i32 %37, 0
%39 = icmp ne i32 %7, %37
%40 = or i1 %38, %39
%41 = sext i1 %40 to i32
br label %42
42: ; preds = %34, %3
%43 = phi i32 [ -1, %3 ], [ %41, %34 ]
ret i32 %43
}