#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ ch;

/* Variables and functions */
scalar_t__ EPIPE;
int /*<<< orphan*/ err(int, char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, char const *, scalar_t__);
scalar_t__ write(int, char *, int);
void test_write(const char *testname, int sock) {
  ssize_t len;
  char ch;
  ch = 0;
  len = write(sock, &ch, sizeof(ch));
  if (len < 0) {
    if (errno == EPIPE)
      return;
    err(-1, "%s: write", testname);
  }
  errx(-1, "%s: write: returned %zd", testname, len);
}


x86 assembly (clang, -O0):
<test_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1197 <test_write+0x67>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    1182 <test_write+0x52>
jmp    11b0 <test_write+0x80>
mov    -0x8(%rbp),%rdx
mov    $0xffffffff,%edi
lea    0xe6e(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0xffffffff,%edi
lea    0xe5f(%rip),%rsi
call   1040 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%eax
mov    %rdi,%r14
movb   $0x0,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1189 <test_write+0x59>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    1175 <test_write+0x45>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe84(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r14,%rdx
call   1050 <err@plt>
lea    0xe7a(%rip),%rsi
mov    $0xffffffff,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <errx@plt>

LLVM IR (-O0):
@errno = global i64 0
@EPIPE = global i64 0
@.str = [10 x i8] c"%s: write\00"
@.str.1 = [24 x i8] c"%s: write: returned %zd\00"
define void @test_write(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8 0, i8* %6
%7 = load i32, i32* %4
%8 = call i64 @write(i32 %7, i8* %6, i32 1)
store i64 %8, i64* %5
%9 = load i64, i64* %5
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i64, i64* @errno
%13 = load i64, i64* @EPIPE
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
br label %23
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 @err(i32 -1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %2
%20 = load i8*, i8** %3
%21 = load i64, i64* %5
%22 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %20, i64 %21)
br label %23
23: ; preds = %19, %15
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EPIPE = global i64 0
@.str = [10 x i8] c"%s: write\00"
@.str.1 = [24 x i8] c"%s: write: returned %zd\00"
define void @test_write(i8* %0, i32 %1) {
%3 = alloca i8
store i8 0, i8* %3
%4 = call i64 @write(i32 %1, i8* %3, i32 1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = load i64, i64* @errno
%8 = load i64, i64* @EPIPE
%9 = icmp eq i64 %7, %8
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %2
%13 = tail call i32 @errx(i32 -1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %4)
br label %14
14: ; preds = %6, %12
ret void
}