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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t strlen(char *);
size_t write(int, char *, size_t);
int file_write(char *path, char *str) {
  int fd;
  ssize_t bytes_written;
  size_t str_sz;
  fd = open(path, O_WRONLY);
  if (fd == -1) {
    perror("Error");
    return -1;
  }
  str_sz = strlen(str);
  bytes_written = write(fd, str, str_sz);
  if (bytes_written != str_sz) {
    if (bytes_written == -1) {
      perror("Error");
    }
    close(fd);
    return -1;
  }
  if (-1 == close(fd)) {
    perror("Error");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1197 <file_write+0x47>
lea    0xe7a(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <file_write+0xd7>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     11f1 <file_write+0xa1>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11dd <file_write+0x8d>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <file_write+0xd7>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1220 <file_write+0xd0>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <file_write+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1199 <file_write+0x49>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    11a7 <file_write+0x57>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11b9 <file_write+0x69>
lea    0xe60(%rip),%rdi
call   1070 <perror@plt>
jmp    11b4 <file_write+0x64>
cmp    $0xffffffffffffffff,%rax
je     11be <file_write+0x6e>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <file_write+0x5d>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [6 x i8] c"Error\00"
define i32 @file_write(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %8
%19 = load i32, i32* %6
%20 = load i8*, i8** %5
%21 = load i64, i64* %8
%22 = call i64 @write(i32 %19, i8* %20, i64 %21)
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = load i64, i64* %8
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %34
26: ; preds = %16
%27 = load i64, i64* %7
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %3
br label %41
34: ; preds = %16
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
%37 = icmp eq i32 -1, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
40: ; preds = %34
store i32 0, i32* %3
br label %41
41: ; preds = %40, %38, %31, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [6 x i8] c"Error\00"
define i32 @file_write(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
8: ; preds = %2
%9 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%10 = tail call i64 @write(i32 %4, i8* %1, i64 %9)
%11 = icmp eq i64 %10, %9
br i1 %11, label %18, label %12
12: ; preds = %8
%13 = icmp eq i64 %10, -1
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
%17 = tail call i32 @close(i32 %4)
br label %23
18: ; preds = %8
%19 = tail call i32 @close(i32 %4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %18, %21, %16, %6
%24 = phi i32 [ -1, %6 ], [ -1, %16 ], [ -1, %21 ], [ 0, %18 ]
ret i32 %24
}