#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 *);
scalar_t__ 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 *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd;
  socklen_t clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  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(1234);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 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");
  }
  bzero(buffer, 256);
  int n = read(newsockfd, buffer, 255);
  if (n < 0) {
    error("ERROR reading from socket");
  }
  printf("Server received: %s\n", buffer);
  n = write(newsockfd, "Some message", 12);
  if (n < 0) {
    error("ERROR writing to socket");
  }
  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)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0x4b>
lea    0xe0a(%rip),%rdi
call   10a0 <error@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1264 <main+0xb4>
lea    0xdb6(%rip),%rdi
call   10a0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a4 <main+0xf4>
lea    0xd87(%rip),%rdi
call   10a0 <error@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    -0x18(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    12e8 <main+0x138>
lea    0xd53(%rip),%rdi
call   10a0 <error@plt>
lea    -0x120(%rbp),%rsi
lea    0xd5a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd60(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    132e <main+0x17e>
lea    0xd49(%rip),%rdi
call   10a0 <error@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x130,%rsp
mov    0x2e26(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    11e7 <main+0x37>
lea    0xe1e(%rip),%rdi
call   10a0 <error@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    123b <main+0x8b>
lea    0xddf(%rip),%rdi
call   10a0 <error@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1274 <main+0xc4>
lea    0xdb7(%rip),%rdi
call   10a0 <error@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    12a5 <main+0xf5>
lea    0xd96(%rip),%rdi
call   10a0 <error@plt>
lea    0xda4(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1050 <printf@plt>
lea    0xda8(%rip),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    12d9 <main+0x129>
lea    0xd9e(%rip),%rdi
call   10a0 <error@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
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 = [21 x i8] c"Server received: %s\0A\00"
@.str.5 = [13 x i8] c"Some message\00"
@.str.6 = [24 x i8] c"ERROR writing to socket\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 [256 x i8]
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = bitcast %struct.sockaddr_in* %10 to i8*
%22 = call i32 @bzero(i8* %21, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 1234)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %20
%37 = load i32, i32* %6
%38 = call i32 @listen(i32 %37, i32 5)
store i32 12, i32* %8
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i32* %8)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %36
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%48 = call i32 @bzero(i8* %47, i32 256)
%49 = load i32, i32* %7
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 255)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %46
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%58 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %7
%60 = call i32 @write(i32 %59, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 12)
store i32 %60, i32* %12
%61 = load i32, i32* %12
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %56
%64 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %65
65: ; preds = %63, %56
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
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.4 = [21 x i8] c"Server received: %s\0A\00"
@.str.5 = [13 x i8] c"Some message\00"
@.str.6 = [24 x i8] c"ERROR writing to socket\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 1234)
%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 i64 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %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
%38 = call i32 @bzero(i8* %8, i32 256)
%39 = call i32 @read(i32 %33, i8* %8, i32 255)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%45 = call i32 @write(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 12)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = call i32 @close(i32 %33)
%51 = call i32 @close(i32 %13)
ret i32 0
}