#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ str;
typedef int socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
int /*<<< orphan*/ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char toupper(char);
int /*<<< orphan*/ write(int, char *, int);
void inet_server() {
  struct sockaddr_in servaddr, cliaddr;
  socklen_t cliaddr_len;
  int listenfd, connfd;
  char buf[80];
  char str[INET_ADDRSTRLEN];
  int i, n;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8000);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, 20);
  printf("Accepting connections ...\n");
  while (1) {
    cliaddr_len = sizeof(cliaddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
    n = read(connfd, buf, 80);
    printf("received from %s at PORT %d\n",
           inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
           ntohs(cliaddr.sin_port));
    for (i = 0; i < n; i++)
      buf[i] = toupper(buf[i]);
    write(connfd, buf, n);
    close(connfd);
  }
}


x86 assembly (clang, -O0):
<inet_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xa8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x28(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1f40,%edi
call   1060 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    -0x28(%rbp),%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12ac <inet_server+0xcc>
movl   $0xc,-0x24(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x50,%edx
call   10a0 <read@plt>
mov    -0xa0(%rbp),%rdx
mov    %eax,%ecx
mov    -0xa8(%rbp),%rax
mov    %ecx,-0x98(%rbp)
mov    0x2cf4(%rip),%rcx
mov    (%rcx),%edi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0xb0(%rbp)
mov    -0x20(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x94(%rbp)
mov    -0x94(%rbp),%eax
cmp    -0x98(%rbp),%eax
jge    1379 <inet_server+0x199>
movslq -0x94(%rbp),%rax
movsbl -0x80(%rbp,%rax,1),%edi
call   1040 <toupper@plt>
mov    %al,%cl
movslq -0x94(%rbp),%rax
mov    %cl,-0x80(%rbp,%rax,1)
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x94(%rbp)
jmp    1335 <inet_server+0x155>
mov    -0x2c(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x98(%rbp),%edx
call   1050 <write@plt>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
jmp    12a7 <inet_server+0xc7>

x86 assembly (clang, -O2):
<inet_server>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rax,-0x50(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    0x2dbb(%rip),%r15
mov    (%r15),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
lea    -0x40(%rbp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10f0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x1f40,%edi
call   1070 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    %ebx,-0x2c(%rbp)
mov    %ebx,%edi
mov    $0x14,%esi
call   10c0 <listen@plt>
lea    0xd81(%rip),%rdi
call   1050 <puts@plt>
lea    -0xb0(%rbp),%r13
jmp    12c6 <inet_server+0xd6>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1060 <write@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
movl   $0xc,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x60(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,%r12d
mov    %eax,%edi
mov    %r13,%rsi
mov    $0x50,%edx
call   10b0 <read@plt>
mov    %eax,%r14d
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    -0x5c(%rbp),%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
call   1030 <inet_ntop@plt>
mov    %rax,%r15
mov    -0x60(%rbp),%edi
call   10d0 <ntohs@plt>
lea    0xce2(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <printf@plt>
test   %r14d,%r14d
jle    12b0 <inet_server+0xc0>
mov    %r14d,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl -0xb0(%rbp,%rbx,1),%edi
ff
call   1040 <toupper@plt>
mov    %al,-0xb0(%rbp,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1340 <inet_server+0x150>
jmp    12b0 <inet_server+0xc0>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [27 x i8] c"Accepting connections ...\0A\00"
@.str.1 = [29 x i8] c"received from %s at PORT %d\0A\00"
define void @inet_server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [80 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = load i32, i32* @INET_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %4
%18 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @htons(i32 8000)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %4
%28 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = load i32, i32* %4
%31 = call i32 @listen(i32 %30, i32 20)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %65, %0
br label %34
34: ; preds = %33
store i32 12, i32* %3
%35 = load i32, i32* %4
%36 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%37 = call i32 @accept(i32 %35, %struct.sockaddr* %36, i32* %3)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 80)
store i32 %40, i32* %10
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%43 = trunc i64 %12 to i32
%44 = call i8* @inet_ntop(i32 %41, %struct.TYPE_2__* %42, i8* %14, i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %44, i32 %47)
store i32 0, i32* %9
br label %49
49: ; preds = %62, %34
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %65
53: ; preds = %49
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = call signext i8 @toupper(i8 signext %57)
%59 = load i32, i32* %9
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %60
store i8 %58, i8* %61
br label %62
62: ; preds = %53
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
br label %49
65: ; preds = %49
%66 = load i32, i32* %5
%67 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%68 = load i32, i32* %10
%69 = call i32 @write(i32 %66, i8* %67, i32 %68)
%70 = load i32, i32* %5
%71 = call i32 @close(i32 %70)
br label %33
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"received from %s at PORT %d\0A\00"
@str = [26 x i8] c"Accepting connections ...\00"
define void @inet_server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @INET_ADDRSTRLEN
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 8000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @bind(i32 %14, %struct.sockaddr* %23, i32 12)
%25 = call i32 @listen(i32 %14, i32 20)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%27 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %30
30: ; preds = %48, %0
store i32 12, i32* %3
%31 = call i32 @accept(i32 %14, %struct.sockaddr* %27, i32* %3)
%32 = call i32 @read(i32 %31, i8* %8, i32 80)
%33 = load i32, i32* @AF_INET
%34 = call i8* @inet_ntop(i32 %33, %struct.TYPE_2__* %28, i8* %11, i32 %9)
%35 = load i32, i32* %29
%36 = call i32 @ntohs(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %34, i32 %36)
%38 = icmp sgt i32 %32, 0
br i1 %38, label %39, label %48
39: ; preds = %30
%40 = zext i32 %32 to i64
br label %41
41: ; preds = %39, %41
%42 = phi i64 [ 0, %39 ], [ %46, %41 ]
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = call signext i8 @toupper(i8 signext %44)
store i8 %45, i8* %43
%46 = add nuw nsw i64 %42, 1
%47 = icmp eq i64 %46, %40
br i1 %47, label %48, label %41
48: ; preds = %41, %30
%49 = call i32 @write(i32 %31, i8* %8, i32 %32)
%50 = call i32 @close(i32 %31)
br label %30
}