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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
void writeFile(char *fname) {
  const char *buf = "SAAAALOO";
  ssize_t nr;
  int fd;
  fd = open(fname, O_WRONLY);
  if (fd == -1) {
    printf("Error opening file\n");
    return;
  }
  nr = write(fd, buf, strlen(buf));
  if (nr == -1) {
    printf("Error writing to file\n");
  } else {
    printf("Wrote to file %zu\n", nr);
  }
  if (close(fd) == -1) {
    printf("Error closing file\n");
  }
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1199 <writeFile+0x49>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    120e <writeFile+0xbe>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11de <writeFile+0x8e>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ef <writeFile+0x9f>
mov    -0x14(%rbp),%esi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    120e <writeFile+0xbe>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11ab <writeFile+0x4b>
mov    %eax,%ebx
lea    0xe80(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     11b4 <writeFile+0x54>
lea    0xe69(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11c0 <writeFile+0x60>
lea    0xe93(%rip),%rdi
jmp    11db <writeFile+0x7b>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     11d4 <writeFile+0x74>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe41(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"SAAAALOO\00"
@O_WRONLY = global i32 0
@.str.1 = [20 x i8] c"Error opening file\0A\00"
@.str.2 = [23 x i8] c"Error writing to file\0A\00"
@.str.3 = [19 x i8] c"Wrote to file %zu\0A\00"
@.str.4 = [20 x i8] c"Error closing file\0A\00"
define void @writeFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %3
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %32
13: ; preds = %1
%14 = load i32, i32* %5
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @write(i32 %14, i8* %15, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %26
23: ; preds = %13
%24 = load i32, i32* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %21
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %11, %30, %26
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"SAAAALOO\00"
@O_WRONLY = global i32 0
@.str.3 = [19 x i8] c"Wrote to file %zu\0A\00"
@str = [19 x i8] c"Error closing file\00"
@str.5 = [22 x i8] c"Error writing to file\00"
@str.6 = [19 x i8] c"Error opening file\00"
define void @writeFile(i8* nocapture %0) {
%2 = load i32, i32* @O_WRONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %16, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.5, i64 0, i64 0))
br label %13
11: ; preds = %5
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %7)
br label %13
13: ; preds = %11, %9
%14 = tail call i32 @close(i32 %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13, %1
%17 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0), %1 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0), %13 ]
%18 = tail call i32 @puts(i8* dereferenceable(1) %17)
br label %19
19: ; preds = %16, %13
ret void
}