#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int safe_write(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
void do_write(int fd, int len) {
  char *buf = malloc(len);
  int rc;
  if (!buf) {
    printf("not enough memory\n");
    exit(1);
  }
  memset(buf, 0, len);
  rc = safe_write(fd, buf, len);
  if (rc) {
    fprintf(stderr, "safe_write failed with error %d (%s)\n", rc, strerror(rc));
    exit(1);
  }
  if (rc != len) {
    printf("invalid number of bytes written\n");
    exit(1);
  }
  free(buf);
}


x86 assembly (clang, -O0):
<do_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bb <do_write+0x3b>
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1080 <safe_write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     121e <do_write+0x9e>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    %rax,%rcx
lea    0xe04(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1240 <do_write+0xc0>
lea    0xe08(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edi,%r14d
mov    %esi,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11c5 <do_write+0x45>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1080 <safe_write@plt>
test   %eax,%eax
jne    11ce <do_write+0x4e>
test   %ebp,%ebp
jne    1200 <do_write+0x80>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <free@plt>
lea    0xe5a(%rip),%rdi
jmp    1207 <do_write+0x87>
mov    %eax,%r14d
mov    0x2e08(%rip),%rax
mov    (%rax),%ebx
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xe17(%rip),%rsi
mov    %ebx,%edi
mov    %r14d,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe31(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"not enough memory\0A\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"safe_write failed with error %d (%s)\0A\00"
@.str.2 = [33 x i8] c"invalid number of bytes written\0A\00"
define void @do_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i8* @malloc(i32 %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = load i32, i32* %4
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
%18 = load i32, i32* %3
%19 = load i8*, i8** %5
%20 = load i32, i32* %4
%21 = call i32 @safe_write(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = load i32, i32* %6
%27 = load i32, i32* %6
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %26, i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %14
%32 = load i32, i32* %6
%33 = load i32, i32* %4
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = load i8*, i8** %5
%40 = call i32 @free(i8* %39)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [38 x i8] c"safe_write failed with error %d (%s)\0A\00"
@str = [18 x i8] c"not enough memory\00"
@str.3 = [32 x i8] c"invalid number of bytes written\00"
define void @do_write(i32 %0, i32 %1) {
%3 = tail call i8* @malloc(i32 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @memset(i8* %3, i32 0, i32 %1)
%10 = tail call i32 @safe_write(i32 %0, i8* %3, i32 %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i8* @strerror(i32 %10)
%15 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = icmp eq i32 %1, 0
br i1 %18, label %22, label %19
19: ; preds = %17
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.3, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %17
%23 = tail call i32 @free(i8* %3)
ret void
}