#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_client_ip(struct sockaddr_storage);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
void server_main(int sockfd) {
  while (1) {
    struct sockaddr_storage client_addr;
    socklen_t addr_size;
    int clientfd;
    int bytes_read;
    char message[256];
    addr_size = sizeof client_addr;
    clientfd = accept(sockfd, (struct sockaddr *)&client_addr, &addr_size);
    print_client_ip(client_addr);
    bytes_read = read(clientfd, message, sizeof message);
    message[bytes_read] = 0;
    if (bytes_read < 0) {
      perror("ERROR reading socket");
    }
    close(clientfd);
    printf("Message (%d): [%s]\n", bytes_read, message);
  }
}


x86 assembly (clang, -O0):
<server_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <print_client_ip@plt>
mov    -0x10(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
cmpl   $0x0,-0x14(%rbp)
jge    11c9 <server_main+0x69>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%esi
lea    -0x120(%rbp),%rdx
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
jmp    116e <server_main+0xe>

x86 assembly (clang, -O2):
<server_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebp
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%rbx
lea    0xe91(%rip),%r13
jmp    11a6 <server_main+0x46>
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r13,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
movl   $0x4,0xc(%rsp)
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
mov    %eax,%r12d
mov    0x8(%rsp),%edi
call   1060 <print_client_ip@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,%r14d
cltq
movb   $0x0,0x10(%rsp,%rax,1)
test   %r14d,%r14d
jns    1190 <server_main+0x30>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
jmp    1190 <server_main+0x30>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"ERROR reading socket\00"
@.str.1 = [20 x i8] c"Message (%d): [%s]\0A\00"
define void @server_main(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
store i32 %0, i32* %2
br label %8
8: ; preds = %1, %25
store i32 4, i32* %4
%9 = load i32, i32* %2
%10 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %4)
store i32 %11, i32* %5
%12 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @print_client_ip(i32 %13)
%15 = load i32, i32* %5
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 256)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %19
store i8 0, i8* %20
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %8
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %8
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %6
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %28, i8* %29)
br label %8
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"ERROR reading socket\00"
@.str.1 = [20 x i8] c"Message (%d): [%s]\0A\00"
define void @server_main(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_storage* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%9 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
br label %10
10: ; preds = %1, %20
store i32 4, i32* %3
%11 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %3)
%12 = load i32, i32* %9
%13 = call i32 @print_client_ip(i32 %12)
%14 = call i32 @read(i32 %11, i8* %7, i32 256)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %15
store i8 0, i8* %16
%17 = icmp slt i32 %14, 0
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %10
%21 = call i32 @close(i32 %11)
%22 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %14, i8* %7)
br label %10
}