#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int write(int, char *, size_t);
int main(void) {
  int fd;
  ssize_t nbcar;
  char buffer[80];
  fd = open("file.out", O_CREAT | O_WRONLY);
  if (fd == -1) {
    write(2, "Error opening a file\n", 25);
    exit(EXIT_FAILURE);
  }
  write(1, "File opened with success\n", 30);
  while ((nbcar = read(0, buffer, 80)) > 0) {
    if (write(fd, buffer, (size_t)nbcar) == -1) {
      exit(EXIT_FAILURE);
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119e <main+0x5e>
mov    $0x2,%edi
lea    0xe83(%rip),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    $0x1,%edi
lea    0xe75(%rip),%rsi
mov    $0x1e,%edx
call   1030 <write@plt>
lea    -0x60(%rbp),%rsi
xor    %edi,%edi
mov    $0x50,%edx
call   1040 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    11fe <main+0xbe>
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0xb9>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11b4 <main+0x74>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    0x2eab(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    1181 <main+0x41>
lea    0xe99(%rip),%rsi
mov    $0x19,%edx
mov    $0x2,%edi
call   1030 <write@plt>
jmp    11c6 <main+0x86>
mov    %eax,%ebp
lea    0xe95(%rip),%rsi
mov    $0x1e,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %rsp,%rbx
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11d4 <main+0x94>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11a0 <main+0x60>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"file.out\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [22 x i8] c"Error opening a file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [26 x i8] c"File opened with success\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca [80 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 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 %15
11: ; preds = %0
%12 = call i32 @write(i32 2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 25)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = call i32 @write(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 30)
br label %17
17: ; preds = %30, %15
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%19 = call i64 @read(i32 0, i8* %18, i32 80)
store i64 %19, i64* %3
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i32, i32* %2
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%24 = load i64, i64* %3
%25 = call i32 @write(i32 %22, i8* %23, i64 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
br label %17
31: ; preds = %17
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.out\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [22 x i8] c"Error opening a file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [26 x i8] c"File opened with success\0A\00"
@EXIT_SUCCESS = global i32 0
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_WRONLY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i64 25)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 30)
br label %14
14: ; preds = %17, %12
%15 = call i64 @read(i32 0, i8* %2, i32 80)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = call i32 @write(i32 %6, i8* %2, i64 %15)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %14
20: ; preds = %17
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14
%24 = load i32, i32* @EXIT_SUCCESS
%25 = tail call i32 @exit(i32 %24)
unreachable
}