#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 int /*<<< orphan*/ u_char;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, int /*<<< orphan*/ *, int);
void write_socket(int fd, void *ptr, int len) {
  u_char *nptr = (u_char *)ptr;
  int r;
  while (len > 0) {
    r = write(fd, nptr, len);
    if (r < 0) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      perror("socket croaked");
      exit(1);
    }
    len -= r;
    nptr += len;
  }
}


x86 assembly (clang, -O0):
<write_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11e4 <write_socket+0xb4>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11bf <write_socket+0x8f>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <write_socket+0x74>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11a9 <write_socket+0x79>
jmp    114a <write_socket+0x1a>
lea    0xe50(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    114a <write_socket+0x1a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11a5 <write_socket+0x75>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e93(%rip),%r15
mov    0x2e74(%rip),%r12
mov    0x2e7d(%rip),%r13
jmp    1170 <write_socket+0x40>
sub    %eax,%ebx
movslq %ebx,%rax
lea    (%r14,%rax,4),%r14
test   %eax,%eax
jle    11a5 <write_socket+0x75>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    115d <write_socket+0x2d>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1170 <write_socket+0x40>
cmp    0x0(%r13),%rax
je     1170 <write_socket+0x40>
lea    0xe6a(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"socket croaked\00"
define void @write_socket(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %7
br label %11
11: ; preds = %33, %29, %3
%12 = load i32, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %41
14: ; preds = %11
%15 = load i32, i32* %4
%16 = load i32*, i32** %7
%17 = load i32, i32* %6
%18 = call i32 @write(i32 %15, i32* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %14
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %22, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25, %21
br label %11
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %14
%34 = load i32, i32* %8
%35 = load i32, i32* %6
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = load i32*, i32** %7
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i32, i32* %38, i64 %39
store i32* %40, i32** %7
br label %11
41: ; preds = %11
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"socket croaked\00"
define void @write_socket(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %28
5: ; preds = %3
%6 = bitcast i8* %1 to i32*
br label %7
7: ; preds = %5, %23
%8 = phi i32 [ %24, %23 ], [ %2, %5 ]
%9 = phi i32* [ %26, %23 ], [ %6, %5 ]
br label %10
10: ; preds = %7, %13
%11 = tail call i32 @write(i32 %0, i32* %9, i32 %8)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %14, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %10, label %20
20: ; preds = %13
%21 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %10
%24 = sub nsw i32 %8, %11
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %9, i64 %25
%27 = icmp sgt i32 %24, 0
br i1 %27, label %7, label %28
28: ; preds = %23, %3
ret void
}