#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*/ close(int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main(int argc, char const *argv[]) {
  int file = open(argv[1], O_RDWR | O_CREAT);
  if (file == -1)
    perror("Creazione file fallita");
  else {
    char charbuf[1] = "a";
    for (int i = 1; i <= 1000; i++) {
      int w = write(file, charbuf, 1);
      if (w == -1)
        perror("Scrittura fallita.");
    }
    close(file);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e8a(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1193 <main+0x53>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
jmp    11f5 <main+0xb5>
mov    0xe91(%rip),%al
mov    %al,-0x15(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x3e8,-0x1c(%rbp)
jg     11ed <main+0xad>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11da <main+0x9a>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
jmp    11df <main+0x9f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a3 <main+0x63>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
mov    0x2e8e(%rip),%rax
mov    0x2e9f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11c7 <main+0x87>
mov    %eax,%ebx
movb   $0x61,0x7(%rsp)
mov    $0x3e8,%ebp
lea    0x7(%rsp),%r14
lea    0xe98(%rip),%r15
jmp    1195 <main+0x55>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
je     11b3 <main+0x73>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1190 <main+0x50>
mov    %r15,%rdi
call   1060 <perror@plt>
jmp    1190 <main+0x50>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
jmp    11ba <main+0x7a>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [23 x i8] c"Creazione file fallita\00"
@__const.main.charbuf = [1 x i8] c"a"
@.str.1 = [19 x i8] c"Scrittura fallita.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %41
21: ; preds = %2
%22 = bitcast [1 x i8]* %7 to i8*
store i32 1, i32* %8
br label %23
23: ; preds = %35, %21
%24 = load i32, i32* %8
%25 = icmp sle i32 %24, 1000
br i1 %25, label %26, label %38
26: ; preds = %23
%27 = load i32, i32* %6
%28 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%29 = call i32 @write(i32 %27, i8* %28, i32 1)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %26
br label %35
35: ; preds = %34
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %23
38: ; preds = %23
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %19
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [23 x i8] c"Creazione file fallita\00"
@.str.1 = [19 x i8] c"Scrittura fallita.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %26
13: ; preds = %2
%14 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
store i8 97, i8* %14
br label %17
15: ; preds = %23
%16 = tail call i32 @close(i32 %9)
br label %26
17: ; preds = %13, %23
%18 = phi i32 [ 1, %13 ], [ %24, %23 ]
%19 = call i32 @write(i32 %9, i8* %14, i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = add nuw nsw i32 %18, 1
%25 = icmp eq i32 %24, 1001
br i1 %25, label %15, label %17
26: ; preds = %15, %11
ret i32 0
}