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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd;
  socklen_t clilen;
  int portnum = 12321;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  int sock_ret, n, m;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    error("ERROR opening socket");
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portnum);
  sock_ret = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (sock_ret < 0) {
    error("ERROR on binding");
  }
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  if (newsockfd < 0) {
    error("ERROR on accept");
  }
  do {
    bzero(buffer, 256);
    n = read(newsockfd, buffer, 255);
    if (n < 0) {
      error("ERROR reading from socket");
    }
  } while (n != 0);
  printf("closing sockets...\n");
  close(newsockfd);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3021,-0x20(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f2 <main+0x52>
lea    0xe13(%rip),%rdi
call   1090 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    1262 <main+0xc2>
lea    0xdb8(%rip),%rdi
call   1090 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a2 <main+0x102>
lea    0xd89(%rip),%rdi
call   1090 <error@plt>
jmp    12a7 <main+0x107>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0x0,-0x148(%rbp)
jge    12eb <main+0x14b>
lea    0xd50(%rip),%rdi
call   1090 <error@plt>
jmp    12f0 <main+0x150>
cmpl   $0x0,-0x148(%rbp)
jne    12a7 <main+0x107>
lea    0xd4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%rsp
mov    0x2e32(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11dc <main+0x3c>
lea    0xe29(%rip),%rdi
call   1090 <error@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x3021,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %eax,%eax
jns    1230 <main+0x90>
lea    0xdea(%rip),%rdi
call   1090 <error@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10b0 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    126a <main+0xca>
lea    0xdc1(%rip),%rdi
call   1090 <error@plt>
lea    0x30(%rsp),%rbx
lea    0xdc0(%rip),%r15
jmp    1284 <main+0xe4>
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
je     12b1 <main+0x111>
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1280 <main+0xe0>
mov    %r15,%rdi
call   1090 <error@plt>
jmp    1280 <main+0xe0>
lea    0xd98(%rip),%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [16 x i8] c"ERROR on accept\00"
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@.str.4 = [20 x i8] c"closing sockets...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12321, i32* %9
%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* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = bitcast %struct.sockaddr_in* %11 to i8*
%25 = call i32 @bzero(i8* %24, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %9
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%36 = call i32 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %23
%40 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %39, %23
%42 = load i32, i32* %6
%43 = call i32 @listen(i32 %42, i32 5)
store i32 12, i32* %8
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %8)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %41
br label %52
52: ; preds = %63, %51
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%54 = call i32 @bzero(i8* %53, i32 256)
%55 = load i32, i32* %7
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 255)
store i32 %57, i32* %14
%58 = load i32, i32* %14
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %52
br label %63
63: ; preds = %62
%64 = load i32, i32* %14
%65 = icmp ne i32 %64, 0
br i1 %65, label %52, label %66
66: ; preds = %63
%67 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [16 x i8] c"ERROR on accept\00"
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@str = [19 x i8] c"closing sockets...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = call i32 @bzero(i8* %9, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 12321)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i32 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %17
%31 = call i32 @listen(i32 %13, i32 5)
store i32 12, i32* %3
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i32 @accept(i32 %13, %struct.sockaddr* %32, i32* %3)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
br label %38
38: ; preds = %37, %44
%39 = call i32 @bzero(i8* %8, i32 256)
%40 = call i32 @read(i32 %33, i8* %8, i32 255)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %38, %42
%45 = icmp eq i32 %40, 0
br i1 %45, label %46, label %38
46: ; preds = %44
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%48 = call i32 @close(i32 %33)
%49 = call i32 @close(i32 %13)
ret i32 0
}