#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*/ string;

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int O_NONBLOCK;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ logIT(int /*<<< orphan*/, char *, ...);
scalar_t__ readn(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int writen(int, char *, int);
int my_send(int fd, char *s_buf, int len) {
  int i;
  char string[256];
  fcntl(fd, F_SETFL, O_NONBLOCK);
  while (readn(fd, string, sizeof(string)) > 0)
    ;
  fcntl(fd, F_SETFL, !O_NONBLOCK);
  tcflush(fd, TCIFLUSH);
  if (writen(fd, s_buf, len) != len) {
    logIT(LOG_ERR, "%s", "io - my_send - writen fail");
    return (0);
  }
  for (i = 0; i < len; i++) {
    unsigned char byte = s_buf[i] & 255;
    logIT(LOG_INFO, ">SEND: %02X", (int)byte);
  }
  return (1);
}


x86 assembly (clang, -O0):
<my_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <readn@plt>
cmp    $0x0,%rax
jle    11a2 <my_send+0x52>
jmp    117f <my_send+0x2f>
mov    -0x8(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
cmpl   $0x0,(%rax)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <writen@plt>
cmp    -0x14(%rbp),%eax
je     121a <my_send+0xca>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
lea    0xdfc(%rip),%rdx
mov    $0x0,%al
call   1070 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1277 <my_send+0x127>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1270 <my_send+0x120>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xff,%eax
mov    %al,-0x121(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
movzbl -0x121(%rbp),%edx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1070 <logIT@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1221 <my_send+0xd1>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e60(%rip),%r14
mov    (%r14),%esi
mov    0x2e6e(%rip),%r13
mov    0x0(%r13),%edx
call   1030 <fcntl@plt>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <readn@plt>
test   %rax,%rax
jg     1190 <my_send+0x40>
mov    (%r14),%esi
xor    %edx,%edx
cmpl   $0x0,0x0(%r13)
sete   %dl
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15d,%edx
call   1060 <writen@plt>
cmp    %r15d,%eax
jne    121d <my_send+0xcd>
mov    $0x1,%r14d
test   %r15d,%r15d
jle    123e <my_send+0xee>
mov    %r15d,%ebx
mov    0x2dd9(%rip),%r15
lea    0xe28(%rip),%r13
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%rbp,1),%edx
mov    (%r15),%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <logIT@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1200 <my_send+0xb0>
jmp    123e <my_send+0xee>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
lea    0xdcf(%rip),%rdx
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <logIT@plt>
mov    %r14d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@TCIFLUSH = global i32 0
@LOG_ERR = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [27 x i8] c"io - my_send - writen fail\00"
@LOG_INFO = global i32 0
@.str.2 = [12 x i8] c">SEND: %02X\00"
define i32 @my_send(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %5
%12 = load i32, i32* @F_SETFL
%13 = load i32, i32* @O_NONBLOCK
%14 = call i32 @fcntl(i32 %11, i32 %12, i32 %13)
br label %15
15: ; preds = %20, %3
%16 = load i32, i32* %5
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%18 = call i64 @readn(i32 %16, i8* %17, i32 256)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
br label %15
21: ; preds = %15
%22 = load i32, i32* %5
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* @O_NONBLOCK
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
%27 = zext i1 %26 to i32
%28 = call i32 @fcntl(i32 %22, i32 %23, i32 %27)
%29 = load i32, i32* %5
%30 = load i32, i32* @TCIFLUSH
%31 = call i32 @tcflush(i32 %29, i32 %30)
%32 = load i32, i32* %5
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = call i32 @writen(i32 %32, i8* %33, i32 %34)
%36 = load i32, i32* %7
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %21
%39 = load i32, i32* @LOG_ERR
%40 = call i32 (i32, i8*, ...) @logIT(i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %63
41: ; preds = %21
store i32 0, i32* %8
br label %42
42: ; preds = %59, %41
%43 = load i32, i32* %8
%44 = load i32, i32* %7
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %62
46: ; preds = %42
%47 = load i8*, i8** %6
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = and i32 %52, 255
%54 = trunc i32 %53 to i8
store i8 %54, i8* %10
%55 = load i32, i32* @LOG_INFO
%56 = load i8, i8* %10
%57 = zext i8 %56 to i32
%58 = call i32 (i32, i8*, ...) @logIT(i32 %55, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %46
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %42
62: ; preds = %42
store i32 1, i32* %4
br label %63
63: ; preds = %62, %38
%64 = load i32, i32* %4
ret i32 %64
}

LLVM IR (-O2):
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@TCIFLUSH = global i32 0
@LOG_ERR = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [27 x i8] c"io - my_send - writen fail\00"
@LOG_INFO = global i32 0
@.str.2 = [12 x i8] c">SEND: %02X\00"
define i32 @my_send(i32 %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = tail call i32 @fcntl(i32 %0, i32 %6, i32 %7)
br label %9
9: ; preds = %9, %3
%10 = call i64 @readn(i32 %0, i8* %5, i32 256)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %9, label %12
12: ; preds = %9
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @O_NONBLOCK
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @fcntl(i32 %0, i32 %13, i32 %16)
%18 = load i32, i32* @TCIFLUSH
%19 = call i32 @tcflush(i32 %0, i32 %18)
%20 = call i32 @writen(i32 %0, i8* %1, i32 %2)
%21 = icmp eq i32 %20, %2
br i1 %21, label %22, label %26
22: ; preds = %12
%23 = icmp sgt i32 %2, 0
br i1 %23, label %24, label %38
24: ; preds = %22
%25 = zext i32 %2 to i64
br label %29
26: ; preds = %12
%27 = load i32, i32* @LOG_ERR
%28 = call i32 (i32, i8*, ...) @logIT(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %38
29: ; preds = %24, %29
%30 = phi i64 [ 0, %24 ], [ %36, %29 ]
%31 = getelementptr inbounds i8, i8* %1, i64 %30
%32 = load i8, i8* %31
%33 = load i32, i32* @LOG_INFO
%34 = zext i8 %32 to i32
%35 = call i32 (i32, i8*, ...) @logIT(i32 %33, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = add nuw nsw i64 %30, 1
%37 = icmp eq i64 %36, %25
br i1 %37, label %38, label %29
38: ; preds = %29, %22, %26
%39 = phi i32 [ 0, %26 ], [ 1, %22 ], [ 1, %29 ]
ret i32 %39
}