#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*/ SEEK_CUR;
int errno;
int /*<<< orphan*/ free(char *);
int lseek(int, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
size_t read(int, char *, int);
size_t skip_zeros(char **, size_t *);
int write(int, char *, int);
int write_nonzeros(int, char **, size_t *);
int copy(int src_fd, int dest_fd) {
  char *buf = NULL;
  size_t num_read;
  size_t num_skip = 0;
  if ((buf = (char *)malloc(sizeof(char) * 1024)) == NULL) {
    goto fail;
  }
  while ((num_read = read(src_fd, buf, 1024)) > 0) {
    char *left = buf;
    size_t num_left = num_read;
    while (num_left > 0) {
      int num_written;
      num_skip = skip_zeros(&left, &num_left);
      if (num_skip) {
        if (lseek(dest_fd, num_skip, SEEK_CUR) == -1) {
          goto fail;
        }
      }
      num_written = write_nonzeros(dest_fd, &left, &num_left);
      if (num_written < 0) {
        goto fail;
      } else if (num_written > 0) {
        num_skip = 0;
      }
    }
  }
  if (num_read == -1) {
    goto fail;
  }
  if (num_skip) {
    if (lseek(dest_fd, -1, SEEK_CUR) == -1) {
      goto fail;
    }
    if (write(dest_fd, "\0", 1) != 1) {
      goto fail;
    }
  }
  free(buf);
  return 0;
fail:
  if (buf) {
    free(buf);
  }
  return errno;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ab <copy+0x3b>
jmp    12ee <copy+0x17e>
jmp    11b0 <copy+0x40>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jbe    1273 <copy+0x103>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    126e <copy+0xfe>
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1040 <skip_zeros@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1230 <copy+0xc0>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    %eax,%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    122b <copy+0xbb>
jmp    12ee <copy+0x17e>
jmp    1230 <copy+0xc0>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <write_nonzeros@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1252 <copy+0xe2>
jmp    12ee <copy+0x17e>
cmpl   $0x0,-0x3c(%rbp)
jle    1264 <copy+0xf4>
movq   $0x0,-0x28(%rbp)
jmp    1269 <copy+0xf9>
jmp    11df <copy+0x6f>
jmp    11b0 <copy+0x40>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1283 <copy+0x113>
jmp    12ee <copy+0x17e>
cmpq   $0x0,-0x28(%rbp)
je     12d9 <copy+0x169>
mov    -0xc(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12b2 <copy+0x142>
jmp    12ee <copy+0x17e>
mov    -0xc(%rbp),%edi
lea    0xd44(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x1,%eax
je     12d4 <copy+0x164>
jmp    12ee <copy+0x17e>
jmp    12d9 <copy+0x169>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    130e <copy+0x19e>
cmpq   $0x0,-0x18(%rbp)
je     1302 <copy+0x192>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    $0x400,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     127c <copy+0x10c>
mov    %rax,%r15
mov    %ebx,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1268 <copy+0xf8>
mov    %ebx,0x4(%rsp)
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
mov    %r15,0x10(%rsp)
mov    %rax,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <skip_zeros@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fa <copy+0x8a>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     1274 <copy+0x104>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <write_nonzeros@plt>
test   %eax,%eax
js     1274 <copy+0x104>
mov    %eax,%r14d
cmpq   $0x0,0x8(%rsp)
jne    11d0 <copy+0x60>
mov    0x4(%rsp),%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
jne    11be <copy+0x4e>
test   %r14d,%r14d
jne    1268 <copy+0xf8>
test   %rbx,%rbx
je     1268 <copy+0xf8>
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     1274 <copy+0x104>
lea    0xda9(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0x1,%eax
jne    1274 <copy+0x104>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1285 <copy+0x115>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@.str = [2 x i8] zeroinitializer
@errno = global i32 0
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i64 0, i64* %8
%12 = call i64 @malloc(i32 1024)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
br label %76
16: ; preds = %2
br label %17
17: ; preds = %53, %16
%18 = load i32, i32* %4
%19 = load i8*, i8** %6
%20 = call i64 @read(i32 %18, i8* %19, i32 1024)
store i64 %20, i64* %7
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %54
22: ; preds = %17
%23 = load i8*, i8** %6
store i8* %23, i8** %9
%24 = load i64, i64* %7
store i64 %24, i64* %10
br label %25
25: ; preds = %52, %22
%26 = load i64, i64* %10
%27 = icmp ugt i64 %26, 0
br i1 %27, label %28, label %53
28: ; preds = %25
%29 = call i64 @skip_zeros(i8** %9, i64* %10)
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = load i32, i32* %5
%34 = load i64, i64* %8
%35 = trunc i64 %34 to i32
%36 = load i32, i32* @SEEK_CUR
%37 = call i32 @lseek(i32 %33, i32 %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %32
br label %76
40: ; preds = %32
br label %41
41: ; preds = %40, %28
%42 = load i32, i32* %5
%43 = call i32 @write_nonzeros(i32 %42, i8** %9, i64* %10)
store i32 %43, i32* %11
%44 = load i32, i32* %11
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %41
br label %76
47: ; preds = %41
%48 = load i32, i32* %11
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
store i64 0, i64* %8
br label %51
51: ; preds = %50, %47
br label %52
52: ; preds = %51
br label %25
53: ; preds = %25
br label %17
54: ; preds = %17
%55 = load i64, i64* %7
%56 = icmp eq i64 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %54
br label %76
58: ; preds = %54
%59 = load i64, i64* %8
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %73
61: ; preds = %58
%62 = load i32, i32* %5
%63 = load i32, i32* @SEEK_CUR
%64 = call i32 @lseek(i32 %62, i32 -1, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %67
66: ; preds = %61
br label %76
67: ; preds = %61
%68 = load i32, i32* %5
%69 = call i32 @write(i32 %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%70 = icmp ne i32 %69, 1
br i1 %70, label %71, label %72
71: ; preds = %67
br label %76
72: ; preds = %67
br label %73
73: ; preds = %72, %58
%74 = load i8*, i8** %6
%75 = call i32 @free(i8* %74)
store i32 0, i32* %3
br label %84
76: ; preds = %71, %66, %57, %46, %39, %15
%77 = load i8*, i8** %6
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i8*, i8** %6
%81 = call i32 @free(i8* %80)
br label %82
82: ; preds = %79, %76
%83 = load i32, i32* @errno
store i32 %83, i32* %3
br label %84
84: ; preds = %82, %73
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@.str = [2 x i8] zeroinitializer
@errno = global i32 0
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = tail call i64 @malloc(i32 1024)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %48, label %8
8: ; preds = %2
%9 = call i64 @read(i32 %0, i8* %6, i32 1024)
%10 = icmp eq i64 %9, 0
br i1 %10, label %44, label %11
11: ; preds = %8
%12 = bitcast i8** %3 to i8*
%13 = bitcast i64* %4 to i8*
br label %14
14: ; preds = %11, %31
%15 = phi i64 [ %9, %11 ], [ %32, %31 ]
store i8* %6, i8** %3
store i64 %15, i64* %4
br label %19
16: ; preds = %27
%17 = load i64, i64* %4
%18 = icmp eq i64 %17, 0
br i1 %18, label %31, label %19
19: ; preds = %14, %16
%20 = call i64 @skip_zeros(i8** %3, i64* %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = trunc i64 %20 to i32
%24 = load i32, i32* @SEEK_CUR
%25 = call i32 @lseek(i32 %1, i32 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %22, %19
%28 = call i32 @write_nonzeros(i32 %1, i8** %3, i64* %4)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %16
30: ; preds = %27, %22
br label %46
31: ; preds = %16
%32 = call i64 @read(i32 %0, i8* %6, i32 1024)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %14
34: ; preds = %31
%35 = icmp ne i32 %28, 0
%36 = select i1 %35, i1 true, i1 %21
br i1 %36, label %44, label %37
37: ; preds = %34
%38 = load i32, i32* @SEEK_CUR
%39 = call i32 @lseek(i32 %1, i32 -1, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %46, label %41
41: ; preds = %37
%42 = call i32 @write(i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %46
44: ; preds = %8, %41, %34
%45 = call i32 @free(i8* %6)
br label %50
46: ; preds = %37, %41, %30
%47 = call i32 @free(i8* %6)
br label %48
48: ; preds = %2, %46
%49 = load i32, i32* @errno
br label %50
50: ; preds = %48, %44
%51 = phi i32 [ %49, %48 ], [ 0, %44 ]
ret i32 %51
}