#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, ...);
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);
  errno = 0;
  ret = write(999, "Hello World", strlen("Hello World"));
  printf("write 2: ret = %d, errno = %d, %s\n", ret, errno, strerrno(errno));
  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);
}


x86 assembly (clang, -O0):
<test_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e69(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     122a <test_write+0xca>
mov    0x2e3a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe5f(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe4e(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    0xe27(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %eax,%r8d
lea    0xe5e(%rip),%rcx
lea    0xe36(%rip),%rax
cmp    %r8d,%edi
cmove  %rax,%rcx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    1244 <test_write+0xe4>
mov    -0xc(%rbp),%esi
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d8d(%rip),%rax
movl   $0x0,(%rax)
lea    0xdb8(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%edx
mov    $0x3e7,%edi
lea    0xda5(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d27(%rip),%rax
movl   $0x0,(%rax)
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
lea    0xd39(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1348 <test_write+0x1e8>
mov    0x2cf8(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    0xd1d(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xd0c(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1362 <test_write+0x202>
mov    -0xc(%rbp),%esi
mov    0x2c86(%rip),%rax
mov    (%rax),%edx
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@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    0x2e69(%rip),%r12
movl   $0x0,(%r12)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f3 <test_write+0x93>
mov    %eax,%ebx
movl   $0x0,(%r12)
lea    0xe6f(%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    0xe72(%rip),%rax
lea    0xe8c(%rip),%rcx
cmove  %rax,%rcx
lea    0xe3e(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1205 <test_write+0xa5>
mov    (%r12),%edx
lea    0xe43(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,(%r12)
lea    0xdfc(%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    0xe21(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,(%r12)
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    0xd9b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12b7 <test_write+0x157>
mov    %eax,%ebp
movl   $0x0,(%r12)
lea    0xd91(%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    0xddc(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12cb <test_write+0x16b>
mov    (%r12),%edx
lea    0xde3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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
store i32 0, i32* @errno
%27 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @write(i32 999, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = load i32, i32* @errno
%31 = load i32, i32* @errno
%32 = call i8* @strerrno(i32 %31)
%33 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %29, i32 %30, i8* %32)
store i32 0, i32* @errno
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %34)
store i32 %35, i32* %2
%36 = load i32, i32* %2
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %47
38: ; preds = %26
store i32 0, i32* @errno
%39 = load i32, i32* %2
%40 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @write(i32 %39, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %40)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = load i32, i32* @errno
%44 = load i32, i32* @errno
%45 = call i8* @strerrno(i32 %44)
%46 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %42, i32 %43, i8* %45)
br label %51
47: ; preds = %26
%48 = load i32, i32* %3
%49 = load i32, i32* @errno
%50 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %48, i32 %49)
br label %51
51: ; preds = %47, %38
%52 = load i32, i32* %1
ret i32 %52
}

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
store i32 0, i32* @errno
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @write(i32 999, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @errno
%20 = tail call i8* @strerrno(i32 %19)
%21 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %18, i32 %19, i8* %20)
store i32 0, i32* @errno
%22 = load i32, i32* @O_RDONLY
%23 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %16
store i32 0, i32* @errno
%26 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @errno
%29 = tail call i8* @strerrno(i32 %28)
%30 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i32 %27, i32 %28, i8* %29)
br label %34
31: ; preds = %16
%32 = load i32, i32* @errno
%33 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i32 %18, i32 %32)
br label %34
34: ; preds = %31, %25
ret i32 undef
}