#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*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int copy(int in, int out) {
  int nb_lus, nb_ecrits;
  char tampon[512];
  if (in == -1) {
    perror("Fichier nul");
    return -1;
  }
  if (out == -1) {
    perror("Fichier nul");
    return -1;
  }
  while (nb_lus > 0) {
    nb_lus = read(in, tampon, 512);
    if (nb_lus <= 0)
      break;
    nb_ecrits = write(out, tampon, nb_lus);
    if (nb_ecrits != nb_lus)
      perror("Probleme d'ecriture");
  };
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1163 <copy+0x33>
lea    0xeae(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <copy+0xc3>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1185 <copy+0x55>
lea    0xe8c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <copy+0xc3>
jmp    118a <copy+0x5a>
cmpl   $0x0,-0x10(%rbp)
jle    11ec <copy+0xbc>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     11ba <copy+0x8a>
jmp    11ec <copy+0xbc>
mov    -0xc(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11e7 <copy+0xb7>
lea    0xe2a(%rip),%rdi
call   1050 <perror@plt>
jmp    118a <copy+0x5a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
cmp    $0xffffffff,%edi
je     11b1 <copy+0x81>
mov    %esi,%r15d
cmp    $0xffffffff,%esi
je     11b1 <copy+0x81>
mov    %edi,%r13d
mov    %rsp,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    11c3 <copy+0x93>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe9b(%rip),%r12
jmp    1196 <copy+0x66>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11c3 <copy+0x93>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1180 <copy+0x50>
mov    %r12,%rdi
call   1050 <perror@plt>
jmp    1180 <copy+0x50>
lea    0xe48(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Fichier nul\00"
@.str.1 = [20 x i8] c"Probleme d'ecriture\00"
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
18: ; preds = %13
br label %19
19: ; preds = %39, %18
%20 = load i32, i32* %6
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = load i32, i32* %4
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 512)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
br label %40
29: ; preds = %22
%30 = load i32, i32* %5
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%32 = load i32, i32* %6
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = load i32, i32* %6
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %29
br label %19
40: ; preds = %28, %19
store i32 0, i32* %3
br label %41
41: ; preds = %40, %16, %11
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [12 x i8] c"Fichier nul\00"
@.str.1 = [20 x i8] c"Probleme d'ecriture\00"
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, -1
%6 = icmp eq i32 %1, -1
%7 = or i1 %5, %6
br i1 %7, label %20, label %8
8: ; preds = %2
%9 = call i32 @read(i32 %0, i8* %4, i32 512)
%10 = icmp slt i32 %9, 1
br i1 %10, label %22, label %11
11: ; preds = %8, %17
%12 = phi i32 [ %18, %17 ], [ %9, %8 ]
%13 = call i32 @write(i32 %1, i8* %4, i32 %12)
%14 = icmp eq i32 %13, %12
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 @read(i32 %0, i8* %4, i32 512)
%19 = icmp slt i32 %18, 1
br i1 %19, label %22, label %11
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %17, %20, %8
%23 = phi i32 [ 0, %8 ], [ -1, %20 ], [ 0, %17 ]
ret i32 %23
}