#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ srvaddr;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
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 const *argv[]) {
  int sockfd, newfd, nbytes;
  char buf[1024];
  struct sockaddr_in srvaddr;
  struct sockaddr_in cliaddr;
  int sin_size;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket error");
    exit(1);
  }
  bzero(&srvaddr, sizeof(srvaddr));
  srvaddr.sin_family = AF_INET;
  srvaddr.sin_port = htons(3491);
  if ((bind(sockfd, (struct sockaddr *)&srvaddr, sizeof(srvaddr))) == -1) {
    perror("bind error");
    exit(1);
  }
  if ((listen(sockfd, 5)) == -1) {
    perror("listen error");
    exit(1);
  }
  printf("%s\n", "Wait connect");
  for (;;) {
    sin_size = sizeof(struct sockaddr_in);
    if ((newfd = accept(sockfd, (struct sockaddr *)&cliaddr,
                        (socklen_t *)&sin_size)) == -1) {
      perror("accept error");
      continue;
    }
    printf("server: got connection from %s \n", inet_ntoa(cliaddr.sin_addr));
    if (write(newfd, "Hello, World\n", 12) == -1) {
      perror("write error");
    }
    if ((nbytes = read(newfd, buf, 500)) == -1) {
      perror("read error");
      exit(1);
    }
    buf[nbytes] = '\0';
    printf("read: %s\n", buf);
    close(newfd);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1224 <main+0x54>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    $0xda3,%edi
call   1050 <htons@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1287 <main+0xb7>
lea    0xd95(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b3 <main+0xe3>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xdd1(%rip),%rdi
lea    0xd64(%rip),%rsi
call   1060 <printf@plt>
movl   $0xc,-0x444(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x444(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1303 <main+0x133>
lea    0xd39(%rip),%rdi
call   10b0 <perror@plt>
jmp    12c6 <main+0xf6>
mov    -0x440(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd27(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd39(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1346 <main+0x176>
lea    0xd2d(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1f4,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    137c <main+0x1ac>
lea    0xd0d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xcef(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    12c6 <main+0xf6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2e00(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
jne    120c <main+0x3c>
lea    0xdf9(%rip),%rdi
jmp    1340 <main+0x170>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0xda3,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1255 <main+0x85>
lea    0xdbd(%rip),%rdi
jmp    1340 <main+0x170>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1272 <main+0xa2>
lea    0xdab(%rip),%rdi
jmp    1340 <main+0x170>
lea    0xe12(%rip),%rdi
lea    0xda5(%rip),%rsi
call   1060 <printf@plt>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x30(%rsp),%r12
lea    0xdea(%rip),%r13
jmp    12b0 <main+0xe0>
lea    0xd8e(%rip),%rdi
call   10b0 <perror@plt>
nopl   0x0(%rax)
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     129d <main+0xcd>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xd63(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
lea    0xd73(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     132b <main+0x15b>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1f4,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     1339 <main+0x169>
cltq
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12b0 <main+0xe0>
lea    0xd3c(%rip),%rdi
call   10b0 <perror@plt>
jmp    12fc <main+0x12c>
lea    0xd3a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [13 x i8] c"Wait connect\00"
@.str.5 = [13 x i8] c"accept error\00"
@.str.6 = [33 x i8] c"server: got connection from %s \0A\00"
@.str.7 = [14 x i8] c"Hello, World\0A\00"
@.str.8 = [12 x i8] c"write error\00"
@.str.9 = [11 x i8] c"read error\00"
@.str.10 = [10 x i8] c"read: %s\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 [1024 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 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%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 3491)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %20
%34 = load i32, i32* %6
%35 = call i32 @listen(i32 %34, i32 5)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %67, %47, %40
store i32 12, i32* %12
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %12)
store i32 %45, i32* %7
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %42
49: ; preds = %42
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %52)
%54 = load i32, i32* %7
%55 = call i32 @write(i32 %54, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 12)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = load i32, i32* %7
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%62 = call i32 @read(i32 %60, i8* %61, i32 500)
store i32 %62, i32* %8
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %59
%68 = load i32, i32* %8
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %69
store i8 0, i8* %70
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%72 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %71)
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
br label %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [13 x i8] c"Wait connect\00"
@.str.5 = [13 x i8] c"accept error\00"
@.str.6 = [33 x i8] c"server: got connection from %s \0A\00"
@.str.7 = [14 x i8] c"Hello, World\0A\00"
@.str.8 = [12 x i8] c"write error\00"
@.str.9 = [11 x i8] c"read error\00"
@.str.10 = [10 x i8] c"read: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %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 eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 3491)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @bind(i32 %13, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = call i32 @listen(i32 %13, i32 5)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
br label %40
40: ; preds = %45, %36
store i32 12, i32* %6
%41 = call i32 @accept(i32 %13, %struct.sockaddr* %38, i32* %6)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %43, %60
br label %40
46: ; preds = %40
%47 = load i32, i32* %39
%48 = call i8* @inet_ntoa(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %48)
%50 = call i32 @write(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 12)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
%55 = call i32 @read(i32 %41, i8* %7, i32 500)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %54
%61 = sext i32 %55 to i64
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %61
store i8 0, i8* %62
%63 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i8* %7)
%64 = call i32 @close(i32 %41)
br label %45
}