#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 */
struct filesystem {
  int dummy;
};
struct file {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
unsigned short file_read(struct file *, int, unsigned char *);
unsigned short file_write(struct file *, unsigned short, unsigned char *);
unsigned char filesys_clone_file(const struct filesystem *self,
                                 struct file *src, struct file *dst) {
  unsigned char buf[64];
  unsigned short bytes_read;
  while (0 < (bytes_read = file_read(src, sizeof(buf), buf))) {
    if (bytes_read != file_write(dst, bytes_read, buf)) {
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<filesys_clone_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x60(%rbp),%rdx
mov    $0x40,%esi
call   1030 <file_read@plt>
mov    %ax,-0x62(%rbp)
movzwl %ax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jge    1191 <filesys_clone_file+0x71>
movzwl -0x62(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x62(%rbp),%ax
lea    -0x60(%rbp),%rdx
movzwl %ax,%esi
call   1040 <file_write@plt>
mov    %ax,%cx
mov    -0x68(%rbp),%eax
movzwl %cx,%ecx
cmp    %ecx,%eax
je     118c <filesys_clone_file+0x6c>
movb   $0x0,-0x1(%rbp)
jmp    1195 <filesys_clone_file+0x75>
jmp    1134 <filesys_clone_file+0x14>
movb   $0x1,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filesys_clone_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r15,%rdx
call   1030 <file_read@plt>
test   %ax,%ax
je     116e <filesys_clone_file+0x4e>
mov    %eax,%ebp
movzwl %ax,%esi
mov    %r14,%rdi
mov    %r15,%rdx
call   1040 <file_write@plt>
cmp    %ax,%bp
je     1140 <filesys_clone_file+0x20>
xor    %eax,%eax
jmp    1170 <filesys_clone_file+0x50>
mov    $0x1,%al
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.filesystem = type { i32 }
%struct.file = type { i32 }
define zeroext i8 @filesys_clone_file(%struct.filesystem* %0, %struct.file* %1, %struct.file* %2) {
%4 = alloca i8
%5 = alloca %struct.filesystem*
%6 = alloca %struct.file*
%7 = alloca %struct.file*
%8 = alloca [64 x i8]
%9 = alloca i16
store %struct.filesystem* %0, %struct.filesystem** %5
store %struct.file* %1, %struct.file** %6
store %struct.file* %2, %struct.file** %7
br label %10
10: ; preds = %26, %3
%11 = load %struct.file*, %struct.file** %6
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%13 = call zeroext i16 @file_read(%struct.file* %11, i32 64, i8* %12)
store i16 %13, i16* %9
%14 = zext i16 %13 to i32
%15 = icmp slt i32 0, %14
br i1 %15, label %16, label %27
16: ; preds = %10
%17 = load i16, i16* %9
%18 = zext i16 %17 to i32
%19 = load %struct.file*, %struct.file** %7
%20 = load i16, i16* %9
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%22 = call zeroext i16 @file_write(%struct.file* %19, i16 zeroext %20, i8* %21)
%23 = zext i16 %22 to i32
%24 = icmp ne i32 %18, %23
br i1 %24, label %25, label %26
25: ; preds = %16
store i8 0, i8* %4
br label %28
26: ; preds = %16
br label %10
27: ; preds = %10
store i8 1, i8* %4
br label %28
28: ; preds = %27, %25
%29 = load i8, i8* %4
ret i8 %29
}

LLVM IR (-O2):
%struct.filesystem = type { i32 }
%struct.file = type { i32 }
define zeroext i8 @filesys_clone_file(%struct.filesystem* nocapture readnone %0, %struct.file* %1, %struct.file* %2) {
%4 = alloca [64 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
br label %6
6: ; preds = %9, %3
%7 = call zeroext i16 @file_read(%struct.file* %1, i32 64, i8* %5)
%8 = icmp eq i16 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = call zeroext i16 @file_write(%struct.file* %2, i16 zeroext %7, i8* %5)
%11 = icmp eq i16 %7, %10
br i1 %11, label %6, label %12
12: ; preds = %6, %9
%13 = phi i8 [ 0, %9 ], [ 1, %6 ]
ret i8 %13
}