#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_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ stop();
char *strerrno(int);
int strlen(char *);
int write(int, char *, int);
int test_write() {
  int fd, ret;
  errno = 0;
  fd = open("foo.fileio.test", O_WRONLY);
  if (fd >= 0) {
    errno = 0;
    ret = write(fd, "Hello World", strlen("Hello World"));
    printf("write 1: ret = %d, errno = %d %s\n", ret, errno,
           ret == strlen("Hello World") ? "OK" : "");
    close(fd);
  } else
    printf("write 1: ret = %d, errno = %d\n", ret, errno);
  stop();
  errno = 0;
  ret = write(999, "Hello World", strlen("Hello World"));
  printf("write 2: ret = %d, errno = %d, %s\n", ret, errno, strerrno(errno));
  stop();
  errno = 0;
  fd = open("foo.fileio.test", O_RDONLY);
  if (fd >= 0) {
    errno = 0;
    ret = write(fd, "Hello World", strlen("Hello World"));
    printf("write 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  } else
    printf("write 3: ret = %d, errno = %d\n", ret, errno);
  stop();
}


x86 assembly (clang, -O0):
<test_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e59(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     123a <test_write+0xca>
mov    0x2e2a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe4f(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe3e(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    0xe17(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %eax,%r8d
lea    0xe4e(%rip),%rcx
lea    0xe26(%rip),%rax
cmp    %r8d,%edi
cmove  %rax,%rcx
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    1254 <test_write+0xe4>
mov    -0xc(%rbp),%esi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <stop@plt>
mov    0x2d76(%rip),%rax
movl   $0x0,(%rax)
lea    0xda1(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%edx
mov    $0x3e7,%edi
lea    0xd8e(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <stop@plt>
mov    0x2d09(%rip),%rax
movl   $0x0,(%rax)
mov    0x2d0c(%rip),%rax
mov    (%rax),%esi
lea    0xd1b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1366 <test_write+0x1f6>
mov    0x2cda(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    0xcff(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xcee(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xd29(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1380 <test_write+0x210>
mov    -0xc(%rbp),%esi
mov    0x2c68(%rip),%rax
mov    (%rax),%edx
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <stop@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e59(%rip),%r12
movl   $0x0,(%r12)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1203 <test_write+0x93>
mov    %eax,%ebx
movl   $0x0,(%r12)
lea    0xe5f(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    (%r12),%r15d
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebp
lea    0xe62(%rip),%rax
lea    0xe7c(%rip),%rcx
cmove  %rax,%rcx
lea    0xe2e(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    1215 <test_write+0xa5>
mov    (%r12),%edx
lea    0xe33(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <stop@plt>
movl   $0x0,(%r12)
lea    0xde5(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x3e7,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    (%r12),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xe0a(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <stop@plt>
movl   $0x0,(%r12)
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
lea    0xd7d(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12d5 <test_write+0x165>
mov    %eax,%ebp
movl   $0x0,(%r12)
lea    0xd73(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    (%r12),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xdbe(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12e9 <test_write+0x179>
mov    (%r12),%edx
lea    0xdc5(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <stop@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_WRONLY = global i32 0
@.str.1 = [12 x i8] c"Hello World\00"
@.str.2 = [34 x i8] c"write 1: ret = %d, errno = %d %s\0A\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [31 x i8] c"write 1: ret = %d, errno = %d\0A\00"
@.str.6 = [35 x i8] c"write 2: ret = %d, errno = %d, %s\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [34 x i8] c"write 3: ret = %d, errno = %d %s\0A\00"
@.str.8 = [31 x i8] c"write 3: ret = %d, errno = %d\0A\00"
define i32 @test_write() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* @errno
%4 = load i32, i32* @O_WRONLY
%5 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %0
store i32 0, i32* @errno
%9 = load i32, i32* %2
%10 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @write(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = load i32, i32* @errno
%14 = load i32, i32* %3
%15 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32 %14, %15
%17 = zext i1 %16 to i64
%18 = select i1 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%19 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13, i8* %18)
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %26
22: ; preds = %0
%23 = load i32, i32* %3
%24 = load i32, i32* @errno
%25 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %23, i32 %24)
br label %26
26: ; preds = %22, %8
%27 = call i32 (...) @stop()
store i32 0, i32* @errno
%28 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @write(i32 999, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = load i32, i32* @errno
%32 = load i32, i32* @errno
%33 = call i8* @strerrno(i32 %32)
%34 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %30, i32 %31, i8* %33)
%35 = call i32 (...) @stop()
store i32 0, i32* @errno
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %36)
store i32 %37, i32* %2
%38 = load i32, i32* %2
%39 = icmp sge i32 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %26
store i32 0, i32* @errno
%41 = load i32, i32* %2
%42 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @write(i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %42)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = load i32, i32* @errno
%46 = load i32, i32* @errno
%47 = call i8* @strerrno(i32 %46)
%48 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %44, i32 %45, i8* %47)
br label %53
49: ; preds = %26
%50 = load i32, i32* %3
%51 = load i32, i32* @errno
%52 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %50, i32 %51)
br label %53
53: ; preds = %49, %40
%54 = call i32 (...) @stop()
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [16 x i8] c"foo.fileio.test\00"
@O_WRONLY = global i32 0
@.str.1 = [12 x i8] c"Hello World\00"
@.str.2 = [34 x i8] c"write 1: ret = %d, errno = %d %s\0A\00"
@.str.3 = [3 x i8] c"OK\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [31 x i8] c"write 1: ret = %d, errno = %d\0A\00"
@.str.6 = [35 x i8] c"write 2: ret = %d, errno = %d, %s\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [34 x i8] c"write 3: ret = %d, errno = %d %s\0A\00"
@.str.8 = [31 x i8] c"write 3: ret = %d, errno = %d\0A\00"
define i32 @test_write() {
store i32 0, i32* @errno
%1 = load i32, i32* @O_WRONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %13
4: ; preds = %0
store i32 0, i32* @errno
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @errno
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32 %6, %8
%10 = select i1 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0)
%11 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %6, i32 %7, i8* %10)
%12 = tail call i32 @close(i32 %2)
br label %16
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 undef, i32 %14)
br label %16
16: ; preds = %13, %4
%17 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @write(i32 999, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @errno
%21 = tail call i8* @strerrno(i32 %20)
%22 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %19, i32 %20, i8* %21)
%23 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %16
store i32 0, i32* @errno
%28 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @write(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @errno
%31 = tail call i8* @strerrno(i32 %30)
%32 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %29, i32 %30, i8* %31)
br label %36
33: ; preds = %16
%34 = load i32, i32* @errno
%35 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %19, i32 %34)
br label %36
36: ; preds = %33, %27
%37 = tail call i32 (...) @stop()
ret i32 undef
}