#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;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int server_socket, connect_socket, retcode;
  socklen_t client_addr_len;
  struct sockaddr_in server_addr, client_addr;
  char buffer[1];
  char *client_address;
  if (((server_socket = socket(AF_INET, SOCK_STREAM, 0))) == -1) {
    perror("Error in server socket()");
    exit(-1);
  }
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(2050);
  server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  if ((retcode = bind(server_socket, (struct sockaddr *)&server_addr,
                      sizeof(server_addr))) == -1) {
    perror("Error in server socket bind()");
    exit(-1);
  }
  if ((retcode = listen(server_socket, 1)) == -1) {
    perror("Error in server socket listen()");
    exit(-1);
  }
  printf("Server ready (CTRL-C quits)\n");
  client_addr_len = sizeof(client_addr);
  while (1) {
    if ((connect_socket = accept(server_socket, (struct sockaddr *)&client_addr,
                                 &client_addr_len)) == -1) {
      perror("Error in accept()");
      close(server_socket);
      exit(-1);
    }
    client_address = inet_ntoa(client_addr.sin_addr);
    printf("\nClient @ %s connects on socket %d\n", client_address,
           connect_socket);
    while ((read(connect_socket, buffer, 1)) != -1) {
      printf("reading 1 byte\n");
      write(connect_socket, buffer, 1);
    }
    printf("exiting\n");
  }
}


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)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1221 <main+0x51>
lea    0xdee(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x802,%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
lea    0xdd8(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    127c <main+0xac>
lea    0xdb6(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12ab <main+0xdb>
lea    0xda5(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12fa <main+0x12a>
lea    0xd9b(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x40(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     1359 <main+0x189>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    131b <main+0x14b>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12c0 <main+0xf0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2df3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
jne    1215 <main+0x35>
lea    0xded(%rip),%rdi
jmp    1272 <main+0x92>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x802,%edi
call   1060 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0xde7(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    125a <main+0x7a>
lea    0xdcb(%rip),%rdi
jmp    1272 <main+0x92>
mov    %ebp,%edi
mov    $0x1,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1281 <main+0xa1>
lea    0xdcf(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10e0 <accept@plt>
cmp    $0xffffffff,%eax
jne    12c8 <main+0xe8>
lea    0xdaf(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>
mov    %eax,%r14d
lea    0x3(%rsp),%rbx
lea    0x18(%rsp),%r12
lea    0x4(%rsp),%r13
lea    0xdda(%rip),%r15
jmp    1311 <main+0x131>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10e0 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     12ab <main+0xcb>
mov    0x18(%rsp),%edi
call   1050 <inet_ntoa@plt>
lea    0xd52(%rip),%rdi
mov    %rax,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <printf@plt>
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0x110>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1330 <main+0x150>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [25 x i8] c"Error in server socket()\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [30 x i8] c"Error in server socket bind()\00"
@.str.3 = [32 x i8] c"Error in server socket listen()\00"
@.str.4 = [29 x i8] c"Server ready (CTRL-C quits)\0A\00"
@.str.5 = [18 x i8] c"Error in accept()\00"
@.str.6 = [36 x i8] c"\0AClient @ %s connects on socket %d\0A\00"
@.str.7 = [16 x i8] c"reading 1 byte\0A\00"
@.str.8 = [9 x i8] c"exiting\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 %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca [1 x i8]
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 2050)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %24, i32* %25
%26 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
store i32 %31, i32* %8
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %21
%37 = load i32, i32* %6
%38 = call i32 @listen(i32 %37, i32 1)
store i32 %38, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %9
br label %45
45: ; preds = %43, %73
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%48 = call i32 @accept(i32 %46, %struct.sockaddr* %47, i32* %9)
store i32 %48, i32* %7
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %45
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i8* @inet_ntoa(i32 %58)
store i8* %59, i8** %13
%60 = load i8*, i8** %13
%61 = load i32, i32* %7
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %60, i32 %61)
br label %63
63: ; preds = %68, %55
%64 = load i32, i32* %7
%65 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%66 = call i32 @read(i32 %64, i8* %65, i32 1)
%67 = icmp ne i32 %66, -1
br i1 %67, label %68, label %73
68: ; preds = %63
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%70 = load i32, i32* %7
%71 = getelementptr inbounds [1 x i8], [1 x i8]* %12, i64 0, i64 0
%72 = call i32 @write(i32 %70, i8* %71, i32 1)
br label %63
73: ; preds = %63
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
br label %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [25 x i8] c"Error in server socket()\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [30 x i8] c"Error in server socket bind()\00"
@.str.3 = [32 x i8] c"Error in server socket listen()\00"
@.str.5 = [18 x i8] c"Error in accept()\00"
@.str.6 = [36 x i8] c"\0AClient @ %s connects on socket %d\0A\00"
@str = [28 x i8] c"Server ready (CTRL-C quits)\00"
@str.9 = [8 x i8] c"exiting\00"
@str.10 = [15 x i8] c"reading 1 byte\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [1 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [1 x i8], [1 x i8]* %6, i64 0, i64 0
%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 eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 2050)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %18
%32 = call i32 @listen(i32 %13, i32 1)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %31
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
store i32 12, i32* %3
%39 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%40 = call i32 @accept(i32 %13, %struct.sockaddr* %39, i32* %3)
%41 = icmp eq i32 %40, -1
br i1 %41, label %44, label %42
42: ; preds = %37
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
br label %48
44: ; preds = %60, %37
%45 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @close(i32 %13)
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %42, %60
%49 = phi i32 [ %40, %42 ], [ %62, %60 ]
%50 = load i32, i32* %43
%51 = call i8* @inet_ntoa(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %51, i32 %49)
%53 = call i32 @read(i32 %49, i8* %10, i32 1)
%54 = icmp eq i32 %53, -1
br i1 %54, label %60, label %55
55: ; preds = %48, %55
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%57 = call i32 @write(i32 %49, i8* %10, i32 1)
%58 = call i32 @read(i32 %49, i8* %10, i32 1)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %55
60: ; preds = %55, %48
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0))
%62 = call i32 @accept(i32 %13, %struct.sockaddr* %39, i32* %3)
%63 = icmp eq i32 %62, -1
br i1 %63, label %44, label %48
}