#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 htonl(int);
int /*<<< orphan*/ perror(char *);
int write(int, void const *, int);
void treat_request(int socket, int connection_counter) {
  int current, result, written = 0;
  current = htonl(connection_counter);
  while (written < sizeof(int)) {
    result = write(socket, (const void *)(&current + written),
                   sizeof(int) - written);
    if (result == -1) {
      perror("Write error");
      break;
    }
    written = written + result;
  }
}


x86 assembly (clang, -O0):
<treat_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0xc(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
jae    11af <treat_request+0x7f>
mov    -0x4(%rbp),%edi
movslq -0x14(%rbp),%rax
lea    -0xc(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x14(%rbp),%rcx
mov    $0x4,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a1 <treat_request+0x71>
lea    0xe69(%rip),%rdi
call   1050 <perror@plt>
jmp    11af <treat_request+0x7f>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1150 <treat_request+0x20>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<treat_request>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %esi,%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%eax
lea    (%rsp,%rax,4),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
sub    %ebp,%edx
mov    %ebx,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     117b <treat_request+0x4b>
add    %eax,%ebp
cmp    $0x3,%ebp
jbe    1150 <treat_request+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Write error\00"
define void @treat_request(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = call i32 @htonl(i32 %8)
store i32 %9, i32* %5
br label %10
10: ; preds = %29, %2
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = icmp ult i64 %12, 4
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = load i32, i32* %3
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %5, i64 %17
%19 = bitcast i32* %18 to i8*
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = sub i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i32 @write(i32 %15, i8* %19, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %33
29: ; preds = %14
%30 = load i32, i32* %7
%31 = load i32, i32* %6
%32 = add nsw i32 %30, %31
store i32 %32, i32* %7
br label %10
33: ; preds = %27, %10
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"Write error\00"
define void @treat_request(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @htonl(i32 %1)
store i32 %5, i32* %3
br label %8
6: ; preds = %8
%7 = icmp ult i32 %16, 4
br i1 %7, label %8, label %19
8: ; preds = %2, %6
%9 = phi i32 [ 0, %2 ], [ %16, %6 ]
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds i32, i32* %3, i64 %10
%12 = bitcast i32* %11 to i8*
%13 = sub nuw nsw i32 4, %9
%14 = call i32 @write(i32 %0, i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
%16 = add nsw i32 %14, %9
br i1 %15, label %17, label %6
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %6, %17
ret void
}