#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 */
int /*<<< orphan*/ printf(char *);
int strlen(char *);
int write(int, char *, int);
int send_query(int sockfd, char *query, char *response) {
  int sent = 0, write_res, size = strlen(query), res;
  while (sent < strlen(query)) {
    res = write(sockfd, query + sent, size - sent);
    if (res == -1) {
      printf("Can't write\n");
      return -1;
    }
    sent += res;
  }
  return sent;
}


x86 assembly (clang, -O0):
<send_query>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    11be <send_query+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11b0 <send_query+0x80>
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <send_query+0x94>
mov    -0x28(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1156 <send_query+0x26>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_query>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %ebp,%ebp
test   %eax,%eax
jle    119a <send_query+0x6a>
cs nopw 0x0(%rax,%rax,1)
movslq %ebp,%rax
lea    (%rbx,%rax,1),%rsi
mov    %r15d,%edx
sub    %eax,%edx
mov    %r14d,%edi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1189 <send_query+0x59>
add    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebp
jl     1160 <send_query+0x30>
jmp    119a <send_query+0x6a>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@str = [12 x i8] c"Can't write\00"
define i32 @send_query(i32 %0, i8* %1, i8* nocapture readnone %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %10, label %20
7: ; preds = %10
%8 = tail call i32 @strlen(i8* %1)
%9 = icmp slt i32 %17, %8
br i1 %9, label %10, label %20
10: ; preds = %3, %7
%11 = phi i32 [ %17, %7 ], [ 0, %3 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = sub nsw i32 %4, %11
%15 = tail call i32 @write(i32 %0, i8* %13, i32 %14)
%16 = icmp eq i32 %15, -1
%17 = add nsw i32 %15, %11
br i1 %16, label %18, label %7
18: ; preds = %10
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %7, %3, %18
%21 = phi i32 [ -1, %18 ], [ 0, %3 ], [ %17, %7 ]
ret i32 %21
}