#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*/ uint8_t;
typedef size_t ssize_t;
typedef scalar_t__ int64_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int);
size_t write(int, int /*<<< orphan*/ const *, size_t);
ssize_t tcp_send(int64_t sockfd, const uint8_t *buf, int64_t start,
                 size_t nbyte) {
  int ret;
  ssize_t wo = 0;
  ssize_t w;
  while ((nbyte - wo) > 0) {
    w = write((int)sockfd, buf + (int)start + (int)wo, nbyte - wo);
    if (w < 0) {
      ret = -errno;
      fprintf(stderr, "%s +%d: socket: %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/iamyuiwong/swift-socket/refs/heads/"
              "master/socket/tcp.c",
              207, strerror(errno));
      return (ssize_t)ret;
    } else {
      wo += w;
    }
  }
  return wo;
}


x86 assembly (clang, -O0):
<tcp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x38(%rbp),%rax
cmp    $0x0,%rax
jbe    1206 <tcp_send+0xd6>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x38(%rbp),%rax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jae    11f5 <tcp_send+0xc5>
xor    %eax,%eax
mov    0x2e2d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%r8
lea    0xe29(%rip),%rsi
lea    0xe36(%rip),%rdx
mov    $0xcf,%ecx
call   1040 <fprintf@plt>
movslq -0x2c(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120e <tcp_send+0xde>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1150 <tcp_send+0x20>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_send>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rcx,%rcx
je     114c <tcp_send+0x3c>
mov    %rcx,%r15
mov    %rdi,%r14
movslq %edx,%rax
lea    (%rsi,%rax,4),%r12
xor    %ebx,%ebx
mov    %rcx,%rdx
nop
movslq %ebx,%rax
lea    (%r12,%rax,4),%rsi
mov    %r14d,%edi
call   1030 <write@plt>
add    %rax,%rbx
mov    %r15,%rdx
sub    %rbx,%rdx
jne    1130 <tcp_send+0x20>
jmp    114e <tcp_send+0x3e>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"%s +%d: socket: %s\0A\00"
@.str.1 = [145 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/iamyuiwong/swift-socket/refs/heads/master/socket/tcp.c\00"
define i64 @tcp_send(i64 %0, i32* %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
store i64 %0, i64* %6
store i32* %1, i32** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i64 0, i64* %11
br label %13
13: ; preds = %49, %4
%14 = load i64, i64* %9
%15 = load i64, i64* %11
%16 = sub i64 %14, %15
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %50
18: ; preds = %13
%19 = load i64, i64* %6
%20 = trunc i64 %19 to i32
%21 = load i32*, i32** %7
%22 = load i64, i64* %8
%23 = trunc i64 %22 to i32
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %21, i64 %24
%26 = load i64, i64* %11
%27 = trunc i64 %26 to i32
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %25, i64 %28
%30 = load i64, i64* %9
%31 = load i64, i64* %11
%32 = sub i64 %30, %31
%33 = call i64 @write(i32 %20, i32* %29, i64 %32)
store i64 %33, i64* %12
%34 = load i64, i64* %12
%35 = icmp ult i64 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %18
%37 = load i32, i32* @errno
%38 = sub nsw i32 0, %37
store i32 %38, i32* %10
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([145 x i8], [145 x i8]* @.str.1, i64 0, i64 0), i32 207, i8* %41)
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
store i64 %44, i64* %5
br label %52
45: ; preds = %18
%46 = load i64, i64* %12
%47 = load i64, i64* %11
%48 = add i64 %47, %46
store i64 %48, i64* %11
br label %49
49: ; preds = %45
br label %13
50: ; preds = %13
%51 = load i64, i64* %11
store i64 %51, i64* %5
br label %52
52: ; preds = %50, %36
%53 = load i64, i64* %5
ret i64 %53
}

LLVM IR (-O2):
@errno = global i32 0
@stderr = global i32 0
define i64 @tcp_send(i64 %0, i32* nocapture %1, i64 %2, i64 %3) {
%5 = icmp eq i64 %3, 0
br i1 %5, label %21, label %6
6: ; preds = %4
%7 = trunc i64 %0 to i32
%8 = shl i64 %2, 32
%9 = ashr exact i64 %8, 32
%10 = getelementptr inbounds i32, i32* %1, i64 %9
br label %11
11: ; preds = %6, %11
%12 = phi i64 [ %3, %6 ], [ %19, %11 ]
%13 = phi i64 [ 0, %6 ], [ %18, %11 ]
%14 = shl i64 %13, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds i32, i32* %10, i64 %15
%17 = tail call i64 @write(i32 %7, i32* %16, i64 %12)
%18 = add i64 %17, %13
%19 = sub i64 %3, %18
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %11
21: ; preds = %11, %4
%22 = phi i64 [ 0, %4 ], [ %18, %11 ]
ret i64 %22
}