#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 socklen_t;
typedef int /*<<< orphan*/ raddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int accept(int, void *, int *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ server_job(int);
void server_loop(int sd) {
  int newsd;
  struct sockaddr_in raddr;
  socklen_t raddr_len;
  char ipstr[1024];
  raddr_len = sizeof(raddr);
  while (1) {
    newsd = accept(sd, (void *)&raddr, &raddr_len);
    if (newsd < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("accept()");
      exit(1);
    }
    inet_ntop(AF_INET, &raddr.sin_addr, ipstr, 1024);
    printf("-----MESSAGE FROM:[%s:%d]-----\n", ipstr, ntohs(raddr.sin_port));
    server_job(newsd);
    close(newsd);
  }
  close(sd);
  exit(0);
}


x86 assembly (clang, -O0):
<server_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x8,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1201 <server_loop+0x81>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     11e6 <server_loop+0x66>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
jne    11eb <server_loop+0x6b>
jmp    1195 <server_loop+0x15>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
add    $0x4,%rsi
lea    -0x420(%rbp),%rdx
mov    $0x400,%ecx
call   1030 <inet_ntop@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <ntohs@plt>
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <server_job@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    1195 <server_loop+0x15>

x86 assembly (clang, -O2):
<server_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebx
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xe58(%rip),%rbp
jmp    1204 <server_loop+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%r14d
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
mov    %r13,%rdx
mov    $0x400,%ecx
call   1030 <inet_ntop@plt>
mov    0x8(%rsp),%edi
call   1070 <ntohs@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
mov    %r14d,%edi
call   1040 <server_job@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <accept@plt>
test   %eax,%eax
jns    11c0 <server_loop+0x40>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <server_loop+0x84>
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <server_loop+0x84>
lea    0xdc2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [9 x i8] c"accept()\00"
@AF_INET = global i32 0
@.str.1 = [32 x i8] c"-----MESSAGE FROM:[%s:%d]-----\0A\00"
define void @server_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
store i32 %0, i32* %2
store i32 8, i32* %5
br label %7
7: ; preds = %1, %21, %25
%8 = load i32, i32* %2
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = call i32 @accept(i32 %8, i8* %9, i32* %5)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %7
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17, %13
br label %7
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %7
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = call i32 @inet_ntop(i32 %26, i32* %27, i8* %28, i32 1024)
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 %33)
%35 = load i32, i32* %3
%36 = call i32 @server_job(i32 %35)
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
br label %7
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [9 x i8] c"accept()\00"
@AF_INET = global i32 0
@.str.1 = [32 x i8] c"-----MESSAGE FROM:[%s:%d]-----\0A\00"
define void @server_loop(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i32 8, i32* %3
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %10
10: ; preds = %31, %1
%11 = call i32 @accept(i32 %0, i8* %5, i32* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = load i64, i64* @EAGAIN
%18 = icmp eq i64 %14, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %31, label %20
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %10
%24 = load i32, i32* @AF_INET
%25 = call i32 @inet_ntop(i32 %24, i32* %8, i8* %7, i32 1024)
%26 = load i32, i32* %9
%27 = call i32 @ntohs(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 %27)
%29 = call i32 @server_job(i32 %11)
%30 = call i32 @close(i32 %11)
br label %31
31: ; preds = %23, %13
br label %10
}