#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*/ me;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  struct sockaddr_in me;
  int soc_waiting;
  int soc;
  char buf[512];
  memset((char *)&me, 0, sizeof(me));
  me.sin_family = AF_INET;
  me.sin_addr.s_addr = htonl(INADDR_ANY);
  me.sin_port = htons((in_port_t)50000);
  if ((soc_waiting = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return 1;
  }
  if (bind(soc_waiting, (struct sockaddr *)&me, sizeof(me)) == -1) {
    perror("bind");
    return 1;
  }
  listen(soc_waiting, 1);
  soc = accept(soc_waiting, NULL, NULL);
  close(soc_waiting);
  write(1, "go ahead\n", 10);
  do {
    int n;
    n = read(0, buf, 512);
    write(soc, buf, n);
    n = read(soc, buf, 512);
    write(1, buf, n);
  } while (strncmp(buf, "quit", 4) != 0);
  close(soc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    $0xc350,%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1249 <main+0x89>
lea    0xdc8(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x188>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0xbb>
lea    0xd9d(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <main+0x188>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
lea    0xd5e(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
lea    -0x220(%rbp),%rsi
xor    %edi,%edi
mov    $0x200,%edx
call   1090 <read@plt>
mov    %eax,-0x224(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1090 <read@plt>
mov    %eax,-0x224(%rbp)
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
lea    -0x220(%rbp),%rdi
lea    0xcf1(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12b8 <main+0xf8>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e07(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0xc350,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12e3 <main+0x123>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12ec <main+0x12c>
mov    %ebx,%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xdab(%rip),%rsi
mov    $0x1,%edi
mov    $0xa,%edx
call   1040 <write@plt>
lea    0x10(%rsp),%rbx
lea    0xd9a(%rip),%r14
nopl   0x0(%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1280 <main+0xc0>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd16(%rip),%rdi
jmp    12f3 <main+0x133>
lea    0xd14(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
jmp    12d7 <main+0x117>

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [10 x i8] c"go ahead\0A\00"
@.str.3 = [5 x i8] c"quit\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = call i32 @htonl(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = call i32 @htons(i32 50000)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %58
23: ; preds = %0
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %58
30: ; preds = %23
%31 = load i32, i32* %3
%32 = call i32 @listen(i32 %31, i32 1)
%33 = load i32, i32* %3
%34 = call i32 @accept(i32 %33, i32* null, i32* null)
store i32 %34, i32* %4
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
%37 = call i32 @write(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 10)
br label %38
38: ; preds = %51, %30
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%40 = call i32 @read(i32 0, i8* %39, i32 512)
store i32 %40, i32* %6
%41 = load i32, i32* %4
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%43 = load i32, i32* %6
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
%45 = load i32, i32* %4
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 512)
store i32 %47, i32* %6
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%49 = load i32, i32* %6
%50 = call i32 @write(i32 1, i8* %48, i32 %49)
br label %51
51: ; preds = %38
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%53 = call i64 @strncmp(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%54 = icmp ne i64 %53, 0
br i1 %54, label %38, label %55
55: ; preds = %51
%56 = load i32, i32* %4
%57 = call i32 @close(i32 %56)
store i32 0, i32* %1
br label %58
58: ; preds = %55, %28, %21
%59 = load i32, i32* %1
ret i32 %59
}

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [10 x i8] c"go ahead\0A\00"
@.str.3 = [5 x i8] c"quit\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [512 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = call i32 @memset(i8* %3, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = call i32 @htons(i32 50000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %39
19: ; preds = %0
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @bind(i32 %15, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %39
25: ; preds = %19
%26 = call i32 @listen(i32 %15, i32 1)
%27 = call i32 @accept(i32 %15, i32* null, i32* null)
%28 = call i32 @close(i32 %15)
%29 = call i32 @write(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 10)
br label %30
30: ; preds = %30, %25
%31 = call i32 @read(i32 0, i8* %4, i32 512)
%32 = call i32 @write(i32 %27, i8* %4, i32 %31)
%33 = call i32 @read(i32 %27, i8* %4, i32 512)
%34 = call i32 @write(i32 1, i8* %4, i32 %33)
%35 = call i64 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %30
37: ; preds = %30
%38 = call i32 @close(i32 %27)
br label %39
39: ; preds = %37, %23, %17
%40 = phi i32 [ 1, %17 ], [ 1, %23 ], [ 0, %37 ]
ret i32 %40
}