#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  char buf[10];
  int s, n, ns, len;
  struct sockaddr_in name;
  s = socket(AF_INET, SOCK_STREAM, 0);
  name.sin_family = AF_INET;
  name.sin_port = htons(12345);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  len = sizeof(struct sockaddr_in);
  bind(s, (struct sockaddr *)&name, len);
  listen(s, 5);
  ns = accept(s, (struct sockaddr *)&name, &len);
  printf("Connection from : %s\n", inet_ntoa(name.sin_addr));
  while ((n = recv(ns, buf, sizeof(buf), 0)) > 0)
    write(1, buf, n);
  close(ns);
  close(s);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x3039,%edi
call   1060 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x30(%rbp)
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   10b0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xda3(%rip),%rdi
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1297 <main+0xd7>
lea    -0xe(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    1262 <main+0xa2>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e19(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x3039,%edi
call   1060 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x8(%rsp)
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   10c0 <accept@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0xdaa(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
lea    0x16(%rsp),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12a4 <main+0xe4>
lea    0x16(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1280 <main+0xc0>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r14d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [22 x i8] c"Connection from : %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 12345)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
store i32 12, i32* %6
%19 = load i32, i32* %3
%20 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%21 = load i32, i32* %6
%22 = call i32 @bind(i32 %19, %struct.sockaddr* %20, i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @listen(i32 %23, i32 5)
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @accept(i32 %25, %struct.sockaddr* %26, i32* %6)
store i32 %27, i32* %5
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i8* @inet_ntoa(i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %38, %0
%34 = load i32, i32* %5
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%36 = call i32 @recv(i32 %34, i8* %35, i32 10, i32 0)
store i32 %36, i32* %4
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%40 = load i32, i32* %4
%41 = call i32 @write(i32 1, i8* %39, i32 %40)
br label %33
42: ; preds = %33
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [22 x i8] c"Connection from : %s\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 12345)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = tail call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %15, i32* %16
store i32 12, i32* %2
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @bind(i32 %9, %struct.sockaddr* %17, i32 12)
%19 = call i32 @listen(i32 %9, i32 5)
%20 = call i32 @accept(i32 %9, %struct.sockaddr* %17, i32* %2)
%21 = load i32, i32* %16
%22 = call i8* @inet_ntoa(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @recv(i32 %20, i8* %4, i32 10, i32 0)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %0, %26
%27 = phi i32 [ %29, %26 ], [ %24, %0 ]
%28 = call i32 @write(i32 1, i8* %4, i32 %27)
%29 = call i32 @recv(i32 %20, i8* %4, i32 10, i32 0)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %26, label %31
31: ; preds = %26, %0
%32 = call i32 @close(i32 %20)
%33 = call i32 @close(i32 %9)
%34 = call i32 @exit(i32 0)
unreachable
}