#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 int8_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int open(char *, int);
int read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(void) {
  int8_t fd, nbcar;
  char buf[80];
  fd = open("fich", O_CREAT | O_RDWR);
  if (fd == -1) {
    write(2, "Erreur d’ouverture \n", 25);
    return -1;
  }
  write(1, "Ouverture avec succès \n", 30);
  while ((nbcar = read(0, buf, 80)) > 0) {
    if (write(fd, buf, nbcar) == -1) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118c <main+0x5c>
mov    $0x2,%edi
lea    0xe8f(%rip),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <main+0xbe>
mov    $0x1,%edi
lea    0xe84(%rip),%rsi
mov    $0x1e,%edx
call   1030 <write@plt>
lea    -0x60(%rbp),%rsi
xor    %edi,%edi
mov    $0x50,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    11e7 <main+0xb7>
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11e2 <main+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <main+0xbe>
jmp    11a2 <main+0x72>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    0x2eb9(%rip),%rax
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a7 <main+0x77>
mov    %eax,%r14d
lea    0xeb7(%rip),%rsi
mov    $0x1,%edi
mov    $0x1e,%edx
call   1030 <write@plt>
mov    %rsp,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11c2 <main+0x92>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1180 <main+0x50>
jmp    11bd <main+0x8d>
lea    0xe57(%rip),%rsi
mov    $0x2,%edi
mov    $0x19,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fich\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Erreur d\E2\80\99ouverture \0A\00"
@.str.2 = [25 x i8] c"Ouverture avec succ\C3\A8s \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [80 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @write(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 25)
store i32 -1, i32* %1
br label %28
13: ; preds = %0
%14 = call i32 @write(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 30)
br label %15
15: ; preds = %26, %13
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%17 = call i32 @read(i32 0, i8* %16, i32 80)
store i32 %17, i32* %3
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i32, i32* %2
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %3
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %1
br label %28
26: ; preds = %19
br label %15
27: ; preds = %15
store i32 0, i32* %1
br label %28
28: ; preds = %27, %25, %11
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [5 x i8] c"fich\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Erreur d\E2\80\99ouverture \0A\00"
@.str.2 = [25 x i8] c"Ouverture avec succ\C3\A8s \0A\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 25)
br label %18
10: ; preds = %0
%11 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 30)
br label %12
12: ; preds = %15, %10
%13 = call i32 @read(i32 0, i8* %2, i32 80)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @write(i32 %6, i8* %2, i32 %13)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %12
18: ; preds = %12, %15, %8
%19 = phi i32 [ -1, %8 ], [ 0, %12 ], [ -1, %15 ]
ret i32 %19
}