#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;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;
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 *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int ntohs(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sock;
  socklen_t length;
  struct sockaddr_in server;
  int msgsock;
  char buf[1024];
  int rval;
  struct sockaddr_in client;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("opening stream socket");
    exit(1);
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = 0;
  if (bind(sock, (struct sockaddr *)&server, sizeof(server))) {
    perror("binding stream socket");
    exit(1);
  }
  length = sizeof(server);
  if (getsockname(sock, (struct sockaddr *)&server, &length)) {
    perror("getting socket name");
    exit(1);
  }
  printf("Socket has port #%d\n", ntohs(server.sin_port));
  listen(sock, 5);
  do {
    length = sizeof(client);
    msgsock = accept(sock, (struct sockaddr *)&client, &length);
    if (msgsock == -1)
      perror("accept");
    else
      do {
        bzero(buf, sizeof(buf));
        if ((rval = read(msgsock, buf, 1024)) < 0)
          perror("reading stream message");
        if (rval == 0)
          printf("Ending connection\n");
        else
          printf("Client (%s) sent: %s", inet_ntoa(client.sin_addr), buf);
      } while (rval != 0);
    close(msgsock);
  } while (1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    121e <main+0x4e>
lea    0xdf1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     126f <main+0x9f>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
movl   $0x18,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   10d0 <getsockname@plt>
cmp    $0x0,%rax
je     12a6 <main+0xd6>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ntohs@plt>
mov    %eax,%esi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0x18,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x450(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1304 <main+0x134>
lea    0xd5b(%rip),%rdi
call   10a0 <perror@plt>
jmp    13a3 <main+0x1d3>
jmp    1309 <main+0x139>
lea    -0x430(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <bzero@plt>
mov    -0x2c(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
jge    1349 <main+0x179>
lea    0xd18(%rip),%rdi
call   10a0 <perror@plt>
cmpl   $0x0,-0x434(%rbp)
jne    1369 <main+0x199>
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138c <main+0x1bc>
mov    -0x450(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    -0x430(%rbp),%rdx
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1391 <main+0x1c1>
cmpl   $0x0,-0x434(%rbp)
jne    1309 <main+0x139>
jmp    13a3 <main+0x1d3>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%al
test   $0x1,%al
jne    12cc <main+0xfc>
jmp    13ba <main+0x1ea>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    0x2df0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     133c <main+0x15c>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x20(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    1345 <main+0x165>
movl   $0x18,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10e0 <getsockname@plt>
test   %rax,%rax
jne    134e <main+0x16e>
mov    0x18(%rsp),%rdi
call   1090 <ntohs@plt>
lea    0xdc3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
lea    0xc(%rsp),%r15
lea    0x40(%rsp),%rbx
lea    0xdd0(%rip),%r12
lea    0xdde(%rip),%r13
jmp    12c0 <main+0xe0>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1060 <close@plt>
movl   $0x18,0xc(%rsp)
mov    %ebp,%edi
lea    0x28(%rsp),%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     132b <main+0x14b>
mov    %rbx,%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
js     131d <main+0x13d>
je     12b0 <main+0xd0>
mov    0x28(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12df <main+0xff>
lea    0xd38(%rip),%rdi
call   10b0 <perror@plt>
jmp    1302 <main+0x122>
lea    0xd23(%rip),%rdi
call   10b0 <perror@plt>
jmp    12b8 <main+0xd8>
lea    0xcbd(%rip),%rdi
jmp    1355 <main+0x175>
lea    0xcca(%rip),%rdi
jmp    1355 <main+0x175>
lea    0xcd7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"opening stream socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"binding stream socket\00"
@.str.2 = [20 x i8] c"getting socket name\00"
@.str.3 = [21 x i8] c"Socket has port #%d\0A\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [23 x i8] c"reading stream message\00"
@.str.6 = [19 x i8] c"Ending connection\0A\00"
@.str.7 = [21 x i8] c"Client (%s) sent: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 24)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %17
store i32 24, i32* %3
%32 = load i32, i32* %2
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i64 @getsockname(i32 %32, %struct.sockaddr* %33, i32* %3)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%41 = load i64, i64* %40
%42 = call i32 @ntohs(i64 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %2
%45 = call i32 @listen(i32 %44, i32 5)
br label %46
46: ; preds = %84, %39
store i32 24, i32* %3
%47 = load i32, i32* %2
%48 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%49 = call i32 @accept(i32 %47, %struct.sockaddr* %48, i32* %3)
store i32 %49, i32* %5
%50 = load i32, i32* %5
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %81
54: ; preds = %46
br label %55
55: ; preds = %77, %54
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%57 = call i32 @bzero(i8* %56, i32 1024)
%58 = load i32, i32* %5
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 1024)
store i32 %60, i32* %7
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %55
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %76
69: ; preds = %64
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%71 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i8* @inet_ntoa(i32 %72)
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i8* %73, i8* %74)
br label %76
76: ; preds = %69, %67
br label %77
77: ; preds = %76
%78 = load i32, i32* %7
%79 = icmp ne i32 %78, 0
br i1 %79, label %55, label %80
80: ; preds = %77
br label %81
81: ; preds = %80, %52
%82 = load i32, i32* %5
%83 = call i32 @close(i32 %82)
br label %84
84: ; preds = %81
br i1 true, label %46, label %85
85: ; preds = %84
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"opening stream socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"binding stream socket\00"
@.str.2 = [20 x i8] c"getting socket name\00"
@.str.3 = [21 x i8] c"Socket has port #%d\0A\00"
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [23 x i8] c"reading stream message\00"
@.str.7 = [21 x i8] c"Client (%s) sent: %s\00"
@str = [18 x i8] c"Ending connection\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i64 0, i64* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %11, %struct.sockaddr* %22, i32 24)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
store i32 24, i32* %1
%29 = call i64 @getsockname(i32 %11, %struct.sockaddr* %22, i32* %1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %28
%35 = load i64, i64* %21
%36 = call i32 @ntohs(i64 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = call i32 @listen(i32 %11, i32 5)
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %41
41: ; preds = %60, %34
store i32 24, i32* %1
%42 = call i32 @accept(i32 %11, %struct.sockaddr* %39, i32* %1)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %60
46: ; preds = %41, %56
%47 = call i32 @bzero(i8* %7, i32 1024)
%48 = call i32 @read(i32 %42, i8* %7, i32 1024)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %56
52: ; preds = %46
%53 = icmp eq i32 %48, 0
br i1 %53, label %54, label %56
54: ; preds = %52
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %60
56: ; preds = %52, %50
%57 = load i32, i32* %40
%58 = call i8* @inet_ntoa(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i8* %58, i8* %7)
br label %46
60: ; preds = %54, %44
%61 = call i32 @close(i32 %42)
br label %41
}