#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
void *m_buf(int);
int m_len(int);
int /*<<< orphan*/ usleep(int);
int write(int, void *, int);
int sock_write(int fd, int msg) {
  void *buf = m_buf(msg);
  int cnt = m_len(msg);
  int written;
  int timer = 2000;
  while (cnt > 0) {
    written = write(fd, buf, cnt);
    if (written < 0 && timer) {
      if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK))
        usleep(1000);
      else
        return -1;
      timer--;
    }
    buf += written;
    cnt -= written;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sock_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <m_buf@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <m_len@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x7d0,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    122f <sock_write+0xef>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    120e <sock_write+0xce>
cmpl   $0x0,-0x24(%rbp)
je     120e <sock_write+0xce>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     11ea <sock_write+0xaa>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     11ea <sock_write+0xaa>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    11f9 <sock_write+0xb9>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
jmp    1205 <sock_write+0xc5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <sock_write+0xf6>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    116c <sock_write+0x2c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%r15d
mov    %esi,%edi
call   1050 <m_buf@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1030 <m_len@plt>
mov    %eax,%ebx
xor    %eax,%eax
test   %ebx,%ebx
jle    11e2 <sock_write+0xa2>
mov    $0x7d0,%r13d
mov    0x2e5a(%rip),%r14
jmp    1199 <sock_write+0x59>
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1060 <usleep@plt>
add    $0xffffffff,%r13d
movslq %r12d,%rax
add    %rax,%rbp
sub    %r12d,%ebx
jle    11e0 <sock_write+0xa0>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    118e <sock_write+0x4e>
test   %r13d,%r13d
je     118e <sock_write+0x4e>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
je     1180 <sock_write+0x40>
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <sock_write+0x40>
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <sock_write+0x40>
mov    $0xffffffff,%eax
jmp    11e2 <sock_write+0xa2>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @sock_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = call i8* @m_buf(i32 %10)
store i8* %11, i8** %6
%12 = load i32, i32* %5
%13 = call i32 @m_len(i32 %12)
store i32 %13, i32* %7
store i32 2000, i32* %9
br label %14
14: ; preds = %45, %2
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %53
17: ; preds = %14
%18 = load i32, i32* %4
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %45
24: ; preds = %17
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %39, label %31
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EWOULDBLOCK
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35, %31, %27
%40 = call i32 @usleep(i32 1000)
br label %42
41: ; preds = %35
store i32 -1, i32* %3
br label %54
42: ; preds = %39
%43 = load i32, i32* %9
%44 = add nsw i32 %43, -1
store i32 %44, i32* %9
br label %45
45: ; preds = %42, %24, %17
%46 = load i32, i32* %8
%47 = load i8*, i8** %6
%48 = sext i32 %46 to i64
%49 = getelementptr i8, i8* %47, i64 %48
store i8* %49, i8** %6
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = sub nsw i32 %51, %50
store i32 %52, i32* %7
br label %14
53: ; preds = %14
store i32 0, i32* %3
br label %54
54: ; preds = %53, %41
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @sock_write(i32 %0, i32 %1) {
%3 = tail call i8* @m_buf(i32 %1)
%4 = tail call i32 @m_len(i32 %1)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %33
6: ; preds = %2, %27
%7 = phi i32 [ %28, %27 ], [ 2000, %2 ]
%8 = phi i32 [ %31, %27 ], [ %4, %2 ]
%9 = phi i8* [ %30, %27 ], [ %3, %2 ]
%10 = tail call i32 @write(i32 %0, i8* %9, i32 %8)
%11 = icmp slt i32 %10, 0
%12 = icmp ne i32 %7, 0
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %27
14: ; preds = %6
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %15, %18
%20 = select i1 %17, i1 true, i1 %19
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %15, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %33
24: ; preds = %14
%25 = tail call i32 @usleep(i32 1000)
%26 = add nsw i32 %7, -1
br label %27
27: ; preds = %24, %6
%28 = phi i32 [ %26, %24 ], [ %7, %6 ]
%29 = sext i32 %10 to i64
%30 = getelementptr i8, i8* %9, i64 %29
%31 = sub nsw i32 %8, %10
%32 = icmp sgt i32 %31, 0
br i1 %32, label %6, label %33
33: ; preds = %14, %27, %2
%34 = phi i32 [ 0, %2 ], [ 0, %27 ], [ -1, %14 ]
ret i32 %34
}