#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*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *,
                           int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int);
scalar_t__ strlen(char const *);
scalar_t__ write(int, char const *, scalar_t__);
bool writeStr(const char *filename, const char *str) {
  int fd = open(filename, O_WRONLY);
  if (fd == -1) {
    fprintf(stderr, "error opening %s: %s (%d)\n", filename, strerror(errno),
            errno);
    return false;
  }
  bool ok = true;
  ssize_t len = strlen(str);
  if (write(fd, str, len) != len) {
    fprintf(stderr, "error writing to %s: %s (%d)\n", filename, strerror(errno),
            errno);
    ok = false;
  }
  close(fd);
  return ok;
}


x86 assembly (clang, -O0):
<writeStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11dd <writeStr+0x7d>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
mov    0x2e16(%rip),%rax
mov    (%rax),%r8d
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1262 <writeStr+0x102>
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     1254 <writeStr+0xf4>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
mov    0x2d9a(%rip),%rax
mov    (%rax),%r8d
lea    0xdd3(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeStr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e2 <writeStr+0x82>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    $0x1,%ebx
cmp    %rbp,%rax
je     11d8 <writeStr+0x78>
mov    0x2e33(%rip),%rax
mov    (%rax),%ebx
mov    0x2e22(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <strerror@plt>
mov    0x0(%rbp),%r8d
lea    0xe52(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %r15d,%edi
call   1050 <close@plt>
jmp    1214 <writeStr+0xb4>
mov    0x2df7(%rip),%rax
mov    (%rax),%ebx
mov    0x2de6(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <strerror@plt>
mov    0x0(%rbp),%r8d
lea    0xdfb(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"error opening %s: %s (%d)\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"error writing to %s: %s (%d)\0A\00"
define i32 @writeStr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%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 %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = load i32, i32* @errno
%20 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18, i32 %19)
store i32 0, i32* %3
br label %41
21: ; preds = %2
store i32 1, i32* %7
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %8
%24 = load i32, i32* %6
%25 = load i8*, i8** %5
%26 = load i64, i64* %8
%27 = call i64 @write(i32 %24, i8* %25, i64 %26)
%28 = load i64, i64* %8
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %37
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %4
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = load i32, i32* @errno
%36 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %34, i32 %35)
store i32 0, i32* %7
br label %37
37: ; preds = %30, %21
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %41
41: ; preds = %37, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"error opening %s: %s (%d)\0A\00"
@errno = global i32 0
@.str.1 = [30 x i8] c"error writing to %s: %s (%d)\0A\00"
define i32 @writeStr(i8* %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 %12
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = load i32, i32* @errno
%11 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %9, i32 %10)
br label %25
12: ; preds = %2
%13 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%14 = tail call i64 @write(i32 %4, i8* %1, i64 %13)
%15 = icmp eq i64 %14, %13
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = load i32, i32* @errno
%21 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %19, i32 %20)
br label %22
22: ; preds = %16, %12
%23 = phi i32 [ 0, %16 ], [ 1, %12 ]
%24 = tail call i32 @close(i32 %4)
br label %25
25: ; preds = %22, %6
%26 = phi i32 [ 0, %6 ], [ %23, %22 ]
ret i32 %26
}