#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__ EINTR;
scalar_t__ errno;
int strlen(char const *);
int write(int, char const *, int);
int send_response(int fd, const char *response) {
  if (!response)
    return -1;
  int len = strlen(response);
  int offset = 0;
  int ret;
  for (;;) {
    ret = write(fd, &response[offset], len - offset);
    if (ret < 0) {
      if (errno != EINTR)
        return ret;
      ret = 0;
    }
    offset += ret;
    if (offset + 1 >= len)
      break;
  }
  do {
    ret = write(fd, "\n", 1);
  } while (!ret || (ret == -1 && errno == EINTR));
  if (ret > 0)
    return 0;
  return ret;
}


x86 assembly (clang, -O0):
<send_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1146 <send_response+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <send_response+0x12e>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ab <send_response+0x8b>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <send_response+0x84>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124e <send_response+0x12e>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jl     11c8 <send_response+0xa8>
jmp    11cd <send_response+0xad>
jmp    1159 <send_response+0x39>
jmp    11d2 <send_response+0xb2>
mov    -0x8(%rbp),%edi
lea    0xe24(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
je     1227 <send_response+0x107>
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1e(%rbp)
jne    1221 <send_response+0x101>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11d2 <send_response+0xb2>
cmpl   $0x0,-0x1c(%rbp)
jle    1248 <send_response+0x128>
movl   $0x0,-0x4(%rbp)
jmp    124e <send_response+0x12e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_response>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11cd <send_response+0xad>
mov    %rsi,%r14
mov    %edi,%r13d
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    0x2e93(%rip),%r15
mov    0x2e84(%rip),%r12
jmp    1170 <send_response+0x50>
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%ecx
lea    (%rcx,%rbx,1),%eax
add    $0x1,%eax
add    %ebx,%ecx
mov    %ecx,%ebx
cmp    %ebp,%eax
jge    1194 <send_response+0x74>
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r13d,%edi
call   1030 <write@plt>
test   %eax,%eax
jns    1160 <send_response+0x40>
mov    (%r15),%rdx
xor    %ecx,%ecx
cmp    (%r12),%rdx
je     1162 <send_response+0x42>
jmp    11d2 <send_response+0xb2>
lea    0xe65(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
je     11a0 <send_response+0x80>
mov    %eax,%ecx
cmp    $0xffffffff,%eax
jne    11c4 <send_response+0xa4>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     11a0 <send_response+0x80>
mov    %ecx,%eax
sar    $0x1f,%eax
and    %ecx,%eax
jmp    11d2 <send_response+0xb2>
mov    $0xffffffff,%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
@.str = [2 x i8] c"\0A\00"
define i32 @send_response(i32 %0, i8* %1) {
%3 = alloca i32
%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
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 -1, i32* %3
br label %68
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %6
store i32 0, i32* %7
br label %15
15: ; preds = %43, %12
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %6
%22 = load i32, i32* %7
%23 = sub nsw i32 %21, %22
%24 = call i32 @write(i32 %16, i8* %20, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %15
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %8
store i32 %32, i32* %3
br label %68
33: ; preds = %27
store i32 0, i32* %8
br label %34
34: ; preds = %33, %15
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = add nsw i32 %36, %35
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
%40 = load i32, i32* %6
%41 = icmp sge i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %34
br label %44
43: ; preds = %34
br label %15
44: ; preds = %42
br label %45
45: ; preds = %60, %44
%46 = load i32, i32* %4
%47 = call i32 @write(i32 %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %47, i32* %8
br label %48
48: ; preds = %45
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i64, i64* @errno
%56 = load i64, i64* @EINTR
%57 = icmp eq i64 %55, %56
br label %58
58: ; preds = %54, %51
%59 = phi i1 [ false, %51 ], [ %57, %54 ]
br label %60
60: ; preds = %58, %48
%61 = phi i1 [ true, %48 ], [ %59, %58 ]
br i1 %61, label %45, label %62
62: ; preds = %60
%63 = load i32, i32* %8
%64 = icmp sgt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %62
store i32 0, i32* %3
br label %68
66: ; preds = %62
%67 = load i32, i32* %8
store i32 %67, i32* %3
br label %68
68: ; preds = %66, %65, %31, %11
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [2 x i8] c"\0A\00"
define i32 @send_response(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %35, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %1)
br label %6
6: ; preds = %17, %4
%7 = phi i32 [ 0, %4 ], [ %19, %17 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub nsw i32 %5, %7
%11 = tail call i32 @write(i32 %0, i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %35
17: ; preds = %13, %6
%18 = phi i32 [ %11, %6 ], [ 0, %13 ]
%19 = add nsw i32 %18, %7
%20 = add nsw i32 %19, 1
%21 = icmp slt i32 %20, %5
br i1 %21, label %6, label %22
22: ; preds = %17, %31
%23 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%24 = icmp eq i32 %23, 0
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = icmp eq i32 %23, -1
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
%30 = select i1 %26, i1 %29, i1 false
br i1 %30, label %31, label %32
31: ; preds = %25, %22
br label %22
32: ; preds = %25
%33 = icmp slt i32 %23, 0
%34 = select i1 %33, i32 %23, i32 0
br label %35
35: ; preds = %13, %32, %2
%36 = phi i32 [ -1, %2 ], [ %34, %32 ], [ %11, %13 ]
ret i32 %36
}