#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int f_write(const char *filename, char *str) {
  int fd, ret;
  fd = open(filename, O_WRONLY);
  if (fd < 0) {
    printf("File %s open failure, errno=%d\n", filename, errno);
    return -1;
  }
  printf("Before write function\n");
  ret = write(fd, str, strlen(str));
  printf("After write function\n");
  if (ret < 0) {
    printf("File %s write failure, errno=%d\n", filename, errno);
  } else if (ret == 0) {
    printf("No data was writen into file %s\n", filename);
  } else {
    printf("Write %d(bytes): %s\n", ret, str);
  }
  close(fd);
  return ret < 0 ? -1 : 0;
}


x86 assembly (clang, -O0):
<f_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a6 <f_write+0x56>
mov    -0x10(%rbp),%rsi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <f_write+0x11a>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jge    1214 <f_write+0xc4>
mov    -0x10(%rbp),%rsi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124f <f_write+0xff>
cmpl   $0x0,-0x20(%rbp)
jne    1235 <f_write+0xe5>
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124a <f_write+0xfa>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124f <f_write+0xff>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     11ca <f_write+0x6a>
mov    %eax,%ebx
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebp
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
js     11eb <f_write+0x8b>
je     1207 <f_write+0xa7>
lea    0xea6(%rip),%rdi
mov    %ebp,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1218 <f_write+0xb8>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
lea    0xe26(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
jmp    1222 <f_write+0xc2>
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
lea    0xe25(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1218 <f_write+0xb8>
lea    0xe33(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
sar    $0x1f,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [32 x i8] c"File %s open failure, errno=%d\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"Before write function\0A\00"
@.str.2 = [22 x i8] c"After write function\0A\00"
@.str.3 = [33 x i8] c"File %s write failure, errno=%d\0A\00"
@.str.4 = [33 x i8] c"No data was writen into file %s\0A\00"
@.str.5 = [21 x i8] c"Write %d(bytes): %s\0A\00"
define i32 @f_write(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
store i32 -1, i32* %3
br label %49
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %6
%20 = load i8*, i8** %5
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @write(i32 %19, i8* %20, i32 %22)
store i32 %23, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = load i32, i32* @errno
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %28, i32 %29)
br label %42
31: ; preds = %17
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i8*, i8** %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %35)
br label %41
37: ; preds = %31
%38 = load i32, i32* %7
%39 = load i8*, i8** %5
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %38, i8* %39)
br label %41
41: ; preds = %37, %34
br label %42
42: ; preds = %41, %27
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
%47 = zext i1 %46 to i64
%48 = select i1 %46, i32 -1, i32 0
store i32 %48, i32* %3
br label %49
49: ; preds = %42, %13
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [32 x i8] c"File %s open failure, errno=%d\0A\00"
@errno = global i32 0
@.str.3 = [33 x i8] c"File %s write failure, errno=%d\0A\00"
@.str.4 = [33 x i8] c"No data was writen into file %s\0A\00"
@.str.5 = [21 x i8] c"Write %d(bytes): %s\0A\00"
@str = [22 x i8] c"Before write function\00"
@str.6 = [21 x i8] c"After write function\00"
define i32 @f_write(i8* %0, i8* %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %7)
br label %27
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @strlen(i8* %1)
%12 = tail call i32 @write(i32 %4, i8* %1, i32 %11)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%14 = icmp slt i32 %12, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %16)
br label %24
18: ; preds = %9
%19 = icmp eq i32 %12, 0
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %24
22: ; preds = %18
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %12, i8* %1)
br label %24
24: ; preds = %20, %22, %15
%25 = tail call i32 @close(i32 %4)
%26 = ashr i32 %12, 31
br label %27
27: ; preds = %24, %6
%28 = phi i32 [ -1, %6 ], [ %26, %24 ]
ret i32 %28
}