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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "usage: %s port\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  int socket_desc, socket_new, socket_len;
  struct sockaddr_in server, client;
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == socket_desc) {
    fprintf(stderr, "create socket failure.\n");
    exit(EXIT_FAILURE);
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(atoi(argv[1]));
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
    fprintf(stderr, "bind failure.\n");
    exit(EXIT_FAILURE);
  }
  fprintf(stdout, "bind done.\n");
  listen(socket_desc, 3);
  fprintf(stdout, "waiting for incoming connections ...\n");
  socket_len = sizeof(struct sockaddr_in);
  while (socket_new = accept(socket_desc, (struct sockaddr *)&client,
                             (socklen_t *)&socket_len)) {
    fprintf(stdout, "connection accepted.\n");
    const char *msg = "Hi, client, this is server!\n";
    write(socket_new, msg, strlen(msg));
  }
  if (socket_new < 0) {
    fprintf(stderr, "accept failure.\n");
    exit(EXIT_FAILURE);
  }
  close(socket_desc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fc <main+0x4c>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    124b <main+0x9b>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12ba <main+0x10a>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   1080 <listen@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1364 <main+0x1b4>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    0xd49(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    12fc <main+0x14c>
cmpl   $0x0,-0x18(%rbp)
jge    1393 <main+0x1e3>
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
lea    0xd1e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    12f4 <main+0x144>
mov    0x2e17(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1310 <main+0x160>
mov    %eax,%r13d
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1322 <main+0x172>
mov    0x2d7e(%rip),%rbx
mov    (%rbx),%edi
lea    0xdf4(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r13d,%edi
mov    $0x3,%esi
call   1080 <listen@plt>
mov    (%rbx),%edi
lea    0xde3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r13d,%edi
call   10a0 <accept@plt>
test   %eax,%eax
je     12db <main+0x12b>
mov    %eax,%ebp
lea    0xddb(%rip),%r14
lea    0xdea(%rip),%rbx
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%r12
nop
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    12a0 <main+0xf0>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcf9(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1339 <main+0x189>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
lea    0xcf0(%rip),%rsi
jmp    1332 <main+0x182>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"usage: %s port\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"create socket failure.\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [15 x i8] c"bind failure.\0A\00"
@stdout = global i32 0
@.str.3 = [12 x i8] c"bind done.\0A\00"
@.str.4 = [38 x i8] c"waiting for incoming connections ...\0A\00"
@.str.5 = [22 x i8] c"connection accepted.\0A\00"
@.str.6 = [29 x i8] c"Hi, client, this is server!\0A\00"
@.str.7 = [17 x i8] c"accept failure.\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 -1, %26
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %22
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%47 = call i64 @bind(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %33
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %33
%55 = load i32, i32* @stdout
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = call i32 @listen(i32 %57, i32 3)
%59 = load i32, i32* @stdout
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %8
br label %61
61: ; preds = %66, %54
%62 = load i32, i32* %6
%63 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%64 = call i32 @accept(i32 %62, %struct.sockaddr* %63, i32* %8)
store i32 %64, i32* %7
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %61
%67 = load i32, i32* @stdout
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i8** %11
%69 = load i32, i32* %7
%70 = load i8*, i8** %11
%71 = load i8*, i8** %11
%72 = call i32 @strlen(i8* %71)
%73 = call i32 @write(i32 %69, i8* %70, i32 %72)
br label %61
74: ; preds = %61
%75 = load i32, i32* %7
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %74
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %74
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"usage: %s port\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"create socket failure.\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [15 x i8] c"bind failure.\0A\00"
@stdout = global i32 0
@.str.3 = [12 x i8] c"bind done.\0A\00"
@.str.4 = [38 x i8] c"waiting for incoming connections ...\0A\00"
@.str.5 = [22 x i8] c"connection accepted.\0A\00"
@.str.6 = [29 x i8] c"Hi, client, this is server!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = icmp eq i32 %0, 2
br i1 %6, label %13, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = bitcast i32* %3 to i8*
%15 = bitcast %struct.sockaddr_in* %4 to i8*
%16 = bitcast %struct.sockaddr_in* %5 to i8*
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %13
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = tail call i32 @atoi(i8* %32)
%34 = tail call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i64 @bind(i32 %19, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %26
%45 = load i32, i32* @stdout
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @listen(i32 %19, i32 3)
%48 = load i32, i32* @stdout
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %3
%50 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%51 = call i32 @accept(i32 %19, %struct.sockaddr* %50, i32* %3)
%52 = icmp eq i32 %51, 0
br i1 %52, label %61, label %53
53: ; preds = %44, %53
%54 = phi i32 [ %59, %53 ], [ %51, %44 ]
%55 = load i32, i32* @stdout
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%58 = call i32 @write(i32 %54, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %57)
%59 = call i32 @accept(i32 %19, %struct.sockaddr* %50, i32* %3)
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %53
61: ; preds = %53, %44
%62 = call i32 @close(i32 %19)
ret i32 0
}