#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in my_addr = {0}, th_addr = {0};
  int svfd, connfd, th_sz = 2 * sizeof th_addr, i;
  static char buf[1024];
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(7500);
  inet_aton("127.0.0.1", &my_addr.sin_addr);
  svfd = socket(AF_INET, SOCK_STREAM, 0);
  i = fcntl(svfd, F_GETFD, 0);
  fcntl(svfd, F_SETFD, i | O_NONBLOCK);
  bind(svfd, (struct sockaddr *)&my_addr, sizeof my_addr);
  listen(svfd, 512);
  fcntl(svfd, F_SETFD, i | O_NONBLOCK);
  connfd = accept(svfd, (struct sockaddr *)&th_addr, &th_sz);
  printf("Conn with %s; fd  %d\n", inet_ntoa(th_addr.sin_addr), connfd);
  i = recv(connfd, buf, 4, 0);
  buf[5] = 0;
  printf("Rec: %s; %d bytes\n", buf, i);
  close(connfd);
  close(svfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
movl   $0x20,-0x34(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1d4c,%edi
call   1060 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xde6(%rip),%rdi
call   10a0 <inet_aton@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%edx
mov    0x2d79(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10c0 <bind@plt>
mov    -0x2c(%rbp),%edi
mov    $0x200,%esi
call   10b0 <listen@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%edx
mov    0x2d3e(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rsi
mov    -0x30(%rbp),%edx
lea    0xd39(%rip),%rdi
call   1070 <printf@plt>
mov    -0x30(%rbp),%edi
lea    0x2db0(%rip),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x38(%rbp)
movb   $0x0,0x2d9f(%rip)
mov    -0x38(%rbp),%edx
lea    0xd20(%rip),%rdi
lea    0x2d89(%rip),%rsi
call   1070 <printf@plt>
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
movl   $0x20,0xc(%rsp)
mov    0x2e10(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x1d4c,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xe0d(%rip),%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1090 <inet_aton@plt>
mov    (%rbx),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%ebp
mov    0x2da1(%rip),%r15
mov    (%r15),%esi
mov    0x2da7(%rip),%r12
mov    (%r12),%edx
or     %eax,%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
mov    %ebx,%edi
mov    $0x200,%esi
call   10a0 <listen@plt>
mov    (%r15),%esi
or     (%r12),%ebp
mov    %ebx,%edi
mov    %ebp,%edx
call   1040 <fcntl@plt>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0xd76(%rip),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1070 <printf@plt>
lea    0x2deb(%rip),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
movb   $0x0,0x2dd8(%rip)
lea    0xd5c(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@main.buf = internal global [1024 x i8] zeroinitializer
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"Conn with %s; fd %d\0A\00"
@.str.2 = [19 x i8] c"Rec: %s; %d bytes\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast %struct.sockaddr_in* %2 to i8*
%9 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 32, i32* %6
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 7500)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%15 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %14)
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = load i32, i32* @F_GETFD
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %7
%22 = load i32, i32* %4
%23 = load i32, i32* @F_SETFD
%24 = load i32, i32* %7
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %24, %25
%27 = call i32 @fcntl(i32 %22, i32 %23, i32 %26)
%28 = load i32, i32* %4
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 16)
%31 = load i32, i32* %4
%32 = call i32 @listen(i32 %31, i32 512)
%33 = load i32, i32* %4
%34 = load i32, i32* @F_SETFD
%35 = load i32, i32* %7
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 @fcntl(i32 %33, i32 %34, i32 %37)
%39 = load i32, i32* %4
%40 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i32* %6)
store i32 %41, i32* %5
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i8* @inet_ntoa(i32 %43)
%45 = load i32, i32* %5
%46 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %44, i32 %45)
%47 = load i32, i32* %5
%48 = call i32 @recv(i32 %47, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.buf, i64 0, i64 0), i32 4, i32 0)
store i32 %48, i32* %7
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.buf, i64 0, i64 5)
%49 = load i32, i32* %7
%50 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.buf, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %5
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@main.buf = internal global [1024 x i8] zeroinitializer
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"Conn with %s; fd %d\0A\00"
@.str.2 = [19 x i8] c"Rec: %s; %d bytes\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 32, i32* %3
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 7500)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%12 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* @F_GETFD
%17 = call i32 @fcntl(i32 %15, i32 %16, i32 0)
%18 = load i32, i32* @F_SETFD
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %19, %17
%21 = call i32 @fcntl(i32 %15, i32 %18, i32 %20)
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i32 @bind(i32 %15, %struct.sockaddr* %22, i32 16)
%24 = call i32 @listen(i32 %15, i32 512)
%25 = load i32, i32* @F_SETFD
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %26, %17
%28 = call i32 @fcntl(i32 %15, i32 %25, i32 %27)
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @accept(i32 %15, %struct.sockaddr* %29, i32* %3)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i8* @inet_ntoa(i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %30)
%35 = call i32 @recv(i32 %30, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.buf, i64 0, i64 0), i32 4, i32 0)
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.buf, i64 0, i64 5)
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @main.buf, i64 0, i64 0), i32 %35)
%37 = call i32 @close(i32 %30)
%38 = call i32 @close(i32 %15)
ret i32 0
}