#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main(void) {
  int fd;
  if ((fd = open("/home/l3miage/debaerdemaekem/Documents/L3-Miage/PDS/Test",
                 O_CREAT | O_RDWR, S_IRWXU)) == -1) {
    perror("Open error");
    return 1;
  }
  int i;
  char *tab = "a";
  for (i = 0; i < 1000000; i++) {
    if (write(fd, tab, 1) != 1) {
      perror("write error");
      return 1;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
lea    0xe8f(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    119a <main+0x5a>
lea    0xeb0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <main+0xcd>
lea    0xea3(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xf4240,-0xc(%rbp)
jge    11fe <main+0xbe>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11eb <main+0xab>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <main+0xcd>
jmp    11f0 <main+0xb0>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11ac <main+0x6c>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ead(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a7 <main+0x67>
mov    %eax,%ebx
mov    $0xf4240,%ebp
lea    0xec6(%rip),%r14
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11b0 <main+0x70>
add    $0xffffffff,%ebp
jne    1180 <main+0x40>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe8b(%rip),%rdi
jmp    11b7 <main+0x77>
lea    0xe8f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    11a2 <main+0x62>

LLVM IR (-O0):
@.str = [57 x i8] c"/home/l3miage/debaerdemaekem/Documents/L3-Miage/PDS/Test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [11 x i8] c"Open error\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRWXU
%9 = call i32 @open(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
store i32 %9, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %31
13: ; preds = %0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %4
store i32 0, i32* %3
br label %14
14: ; preds = %25, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 1000000
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i8*, i8** %4
%20 = call i32 @write(i32 %18, i8* %19, i32 1)
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %31
24: ; preds = %17
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %14
28: ; preds = %14
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
store i32 0, i32* %1
br label %31
31: ; preds = %28, %22, %11
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [57 x i8] c"/home/l3miage/debaerdemaekem/Documents/L3-Miage/PDS/Test\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [11 x i8] c"Open error\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRWXU
%5 = tail call i32 @open(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %20
9: ; preds = %12
%10 = add nuw nsw i32 %13, 1
%11 = icmp eq i32 %10, 1000000
br i1 %11, label %18, label %12
12: ; preds = %0, %9
%13 = phi i32 [ %10, %9 ], [ 0, %0 ]
%14 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %20
18: ; preds = %9
%19 = tail call i32 @close(i32 %5)
br label %20
20: ; preds = %16, %18, %7
%21 = phi i32 [ 1, %7 ], [ 1, %16 ], [ 0, %18 ]
ret i32 %21
}