#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int write(int, char const *, size_t);
int echo(const char *out, const char *file) {
  if (!out)
    return -2;
  size_t len = strlen(out);
  if (!len)
    return -5;
  int fd = open(file, O_WRONLY);
  if (fd == -1) {
    fprintf(stderr, "Opening %s failed: ", file);
    perror(0);
    return -3;
  }
  ssize_t r = write(fd, out, len);
  if (r == -1) {
    fprintf(stderr, "Writing to %s failed: ", file);
    perror(0);
    return -1;
  }
  if (r != len) {
    fprintf(stderr, "Wrote only %zd of %zu bytes ", r, len);
    return -1;
  }
  int ret = close(fd);
  if (ret == -1) {
    fprintf(stderr, "Closing %s failed: ", file);
    perror(0);
    return -4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1187 <echo+0x27>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12c5 <echo+0x165>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ab <echo+0x4b>
movl   $0xfffffffb,-0x4(%rbp)
jmp    12c5 <echo+0x165>
mov    -0x18(%rbp),%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f8 <echo+0x98>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12c5 <echo+0x165>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1243 <echo+0xe3>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <echo+0x165>
movslq -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     127b <echo+0x11b>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <echo+0x165>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12be <echo+0x15e>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12c5 <echo+0x165>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11f5 <echo+0x95>
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
test   %rax,%rax
je     11fc <echo+0x9c>
mov    %rax,%r15
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1203 <echo+0xa3>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122b <echo+0xcb>
movslq %eax,%rcx
cmp    %rcx,%r15
jne    124e <echo+0xee>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    126f <echo+0x10f>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xfffffffc,%eax
jmp    126f <echo+0x10f>
mov    $0xfffffffe,%eax
jmp    126f <echo+0x10f>
mov    $0xfffffffb,%eax
jmp    126f <echo+0x10f>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xfffffffd,%eax
jmp    126f <echo+0x10f>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
jmp    126a <echo+0x10a>
mov    0x2d8b(%rip),%rcx
mov    (%rcx),%edi
lea    0xdcd(%rip),%rsi
mov    %eax,%edx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%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 = [20 x i8] c"Opening %s failed: \00"
@.str.1 = [23 x i8] c"Writing to %s failed: \00"
@.str.2 = [29 x i8] c"Wrote only %zd of %zu bytes \00"
@.str.3 = [20 x i8] c"Closing %s failed: \00"
define i32 @echo(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 -2, i32* %3
br label %63
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp ne i64 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %13
store i32 -5, i32* %3
br label %63
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %5
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @perror(i32 0)
store i32 -3, i32* %3
br label %63
30: ; preds = %19
%31 = load i32, i32* %7
%32 = load i8*, i8** %4
%33 = load i64, i64* %6
%34 = call i32 @write(i32 %31, i8* %32, i64 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %5
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = call i32 @perror(i32 0)
store i32 -1, i32* %3
br label %63
42: ; preds = %30
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = load i64, i64* %6
%46 = icmp ne i64 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i32, i32* %8
%50 = load i64, i64* %6
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %49, i64 %50)
store i32 -1, i32* %3
br label %63
52: ; preds = %42
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %5
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %59)
%61 = call i32 @perror(i32 0)
store i32 -4, i32* %3
br label %63
62: ; preds = %52
store i32 0, i32* %3
br label %63
63: ; preds = %62, %57, %47, %37, %25, %18, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Opening %s failed: \00"
@.str.1 = [23 x i8] c"Writing to %s failed: \00"
@.str.2 = [29 x i8] c"Wrote only %zd of %zu bytes \00"
@.str.3 = [20 x i8] c"Closing %s failed: \00"
define i32 @echo(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %35, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %35, label %7
7: ; preds = %4
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* %1, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
%14 = tail call i32 @perror(i32 0)
br label %35
15: ; preds = %7
%16 = tail call i32 @write(i32 %9, i8* %0, i64 %5)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%21 = tail call i32 @perror(i32 0)
br label %35
22: ; preds = %15
%23 = sext i32 %16 to i64
%24 = icmp eq i64 %5, %23
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %16, i64 %5)
br label %35
28: ; preds = %22
%29 = tail call i32 @close(i32 %9)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%34 = tail call i32 @perror(i32 0)
br label %35
35: ; preds = %4, %18, %25, %28, %31, %11, %2
%36 = phi i32 [ -2, %2 ], [ -5, %4 ], [ -3, %11 ], [ -1, %18 ], [ -1, %25 ], [ -4, %31 ], [ 0, %28 ]
ret i32 %36
}