#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
typedef int ssize_t;

/* Variables and functions */
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ process_cmd(char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
void process_client(int clientfd, struct sockaddr_in *client_addr) {
  char buffer[1024];
  printf("%s:%d connected\n", inet_ntoa(client_addr->sin_addr),
         ntohs(client_addr->sin_port));
  while (true) {
    ssize_t msg_sz = recv(clientfd, buffer, 1024 - 1, 0);
    *(buffer + msg_sz) = '\0';
    if (msg_sz == 0 || !process_cmd(buffer, clientfd)) {
      printf("%s:%d connection closed\n", inet_ntoa(client_addr->sin_addr),
             ntohs(client_addr->sin_port));
      break;
    }
  }
}


x86 assembly (clang, -O0):
<process_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x420(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x420(%rbp),%rsi
mov    %eax,%edx
lea    0xe70(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x414(%rbp)
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
cmpl   $0x0,-0x414(%rbp)
je     11e5 <process_client+0x95>
lea    -0x410(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1060 <process_cmd@plt>
cmp    $0x0,%eax
jne    121d <process_client+0xcd>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x428(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
jmp    1222 <process_client+0xd2>
jmp    1195 <process_client+0x45>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_client>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x4(%rsi),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%r14),%edi
call   1070 <ntohs@plt>
lea    0xe86(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
test   %eax,%eax
je     11b9 <process_client+0x69>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <process_cmd@plt>
test   %eax,%eax
jne    1190 <process_client+0x40>
mov    0x4(%r14),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%r14),%edi
call   1070 <ntohs@plt>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@.str = [17 x i8] c"%s:%d connected\0A\00"
@.str.1 = [25 x i8] c"%s:%d connection closed\0A\00"
define void @process_client(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in*
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %4
%7 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%9 = load i32, i32* %8
%10 = call i8* @inet_ntoa(i32 %9)
%11 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @ntohs(i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %14)
br label %16
16: ; preds = %2, %41
%17 = load i32, i32* %3
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = call i32 @recv(i32 %17, i8* %18, i32 1023, i32 0)
store i32 %19, i32* %6
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %16
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %3
%29 = call i32 @process_cmd(i8* %27, i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %41, label %31
31: ; preds = %26, %16
%32 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = call i8* @inet_ntoa(i32 %34)
%36 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @ntohs(i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %35, i32 %39)
br label %42
41: ; preds = %26
br label %16
42: ; preds = %31
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@.str = [17 x i8] c"%s:%d connected\0A\00"
@.str.1 = [25 x i8] c"%s:%d connection closed\0A\00"
define void @process_client(i32 %0, %struct.sockaddr_in* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%6 = load i32, i32* %5
%7 = tail call i8* @inet_ntoa(i32 %6)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i32 @ntohs(i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %10)
br label %12
12: ; preds = %17, %2
%13 = call i32 @recv(i32 %0, i8* %4, i32 1023, i32 0)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %15
%16 = icmp eq i32 %13, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = call i32 @process_cmd(i8* %4, i32 %0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %12
20: ; preds = %12, %17
%21 = load i32, i32* %5
%22 = call i8* @inet_ntoa(i32 %21)
%23 = load i32, i32* %8
%24 = call i32 @ntohs(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %24)
ret void
}