#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 /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
void test_write_null() {
  int fd, count_written, count = 5;
  char *string = "Hello World";
  fd = open("/dev/NULL", O_WRONLY);
  if (fd < 0) {
    printf("open /dev/NULL is not possible, s%s.\n", strerror(errno));
    return;
  }
  count_written = write(fd, string, count);
  if (count_written != count) {
    printf("Error wrte /dev/NULL only %s.\n", strerror(errno));
    return;
  }
  printf("Should write %d and written %d.\n", count, count_written);
  close(fd);
}


x86 assembly (clang, -O0):
<test_write_null>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x5,-0xc(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11b0 <test_write_null+0x60>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120e <test_write_null+0xbe>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11f2 <test_write_null+0xa2>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    120e <test_write_null+0xbe>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write_null>:
push   %rbx
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11a4 <test_write_null+0x54>
mov    %eax,%ebx
lea    0xe8d(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x5,%eax
jne    11bb <test_write_null+0x6b>
lea    0xed0(%rip),%rdi
mov    $0x5,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5d(%rip),%rdi
jmp    11d0 <test_write_null+0x80>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe6c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Hello World\00"
@.str.1 = [10 x i8] c"/dev/NULL\00"
@O_WRONLY = global i32 0
@.str.2 = [38 x i8] c"open /dev/NULL is not possible, s%s.\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"Error wrte /dev/NULL only %s.\0A\00"
@.str.4 = [33 x i8] c"Should write %d and written %d.\0A\00"
define void @test_write_null() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 5, i32* %3
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = load i32, i32* @O_WRONLY
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %31
13: ; preds = %0
%14 = load i32, i32* %1
%15 = load i8*, i8** %4
%16 = load i32, i32* %3
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %31
25: ; preds = %13
%26 = load i32, i32* %3
%27 = load i32, i32* %2
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %27)
%29 = load i32, i32* %1
%30 = call i32 @close(i32 %29)
br label %31
31: ; preds = %25, %21, %9
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"Hello World\00"
@.str.1 = [10 x i8] c"/dev/NULL\00"
@O_WRONLY = global i32 0
@.str.2 = [38 x i8] c"open /dev/NULL is not possible, s%s.\0A\00"
@errno = global i32 0
@.str.3 = [31 x i8] c"Error wrte /dev/NULL only %s.\0A\00"
@.str.4 = [33 x i8] c"Should write %d and written %d.\0A\00"
define void @test_write_null() {
%1 = load i32, i32* @O_WRONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = tail call i8* @strerror(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %18
8: ; preds = %0
%9 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 5)
%10 = icmp eq i32 %9, 5
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %18
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 5, i32 5)
%17 = tail call i32 @close(i32 %2)
br label %18
18: ; preds = %15, %11, %4
ret void
}