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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ htons(int);
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 main(int argc, const char *argv[]) {
  int ret = -1;
  struct sockaddr_in serverAddr;
  int serverSock;
  int acceptedSock;
  struct sockaddr_in clientAddr;
  char readBuf[100];
  ssize_t readSize;
  socklen_t clientAddrSize = sizeof(clientAddr);
  serverSock = socket(PF_INET, SOCK_STREAM, 0);
  if (serverSock == -1) {
    perror("socket");
    goto leave;
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = (INADDR_ANY);
  serverAddr.sin_port = htons(3000);
  if ((ret = bind(serverSock, (struct sockaddr *)&serverAddr,
                  sizeof(serverAddr)))) {
    perror("bind");
    goto leave;
  }
  if (listen(serverSock, 1) == -1) {
    perror("listen");
    goto leave;
  }
  acceptedSock =
      accept(serverSock, (struct sockaddr *)&clientAddr, &clientAddrSize);
  if (acceptedSock == -1) {
    perror("accept");
    goto leave;
  }
  readSize = read(acceptedSock, readBuf, 100);
  if (readSize == -1) {
    perror("read");
    goto leave;
  }
  printf("읽은 바이트 수 %ld\n", readSize);
  printf("%s\n", readBuf);
  close(acceptedSock);
  close(serverSock);
  getchar();
leave:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xc,-0xa8(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1201 <main+0x61>
lea    0xe09(%rip),%rdi
call   10a0 <perror@plt>
jmp    1324 <main+0x184>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0xbb8,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1254 <main+0xb4>
lea    0xdbd(%rip),%rdi
call   10a0 <perror@plt>
jmp    1324 <main+0x184>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0xdb>
lea    0xd9b(%rip),%rdi
call   10a0 <perror@plt>
jmp    1324 <main+0x184>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0xa8(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12ac <main+0x10c>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
jmp    1324 <main+0x184>
mov    -0x28(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    12e4 <main+0x144>
lea    0xd40(%rip),%rdi
call   10a0 <perror@plt>
jmp    1324 <main+0x184>
mov    -0xa4(%rbp),%esi
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    -0x14(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12b3 <main+0x103>
mov    %eax,%r14d
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0xbb8,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
jne    12c6 <main+0x116>
mov    %r14d,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x126>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x12f>
mov    %eax,%ebx
lea    0x30(%rsp),%rsi
mov    %eax,%edi
mov    $0x64,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     12e8 <main+0x138>
lea    0xda5(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x30(%rsp),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    %ebp,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd46(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    12a5 <main+0xf5>
mov    %eax,%ebp
lea    0xd38(%rip),%rdi
call   10b0 <perror@plt>
jmp    12a5 <main+0xf5>
lea    0xd2f(%rip),%rdi
jmp    12ef <main+0x13f>
lea    0xd2d(%rip),%rdi
jmp    12ef <main+0x13f>
lea    0xd2b(%rip),%rdi
call   10b0 <perror@plt>
xor    %ebp,%ebp
jmp    12a5 <main+0xf5>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [26 x i8] c"\EC\9D\BD\EC\9D\80 \EB\B0\94\EC\9D\B4\ED\8A\B8 \EC\88\98 %ld\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca [100 x i8]
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 12, i32* %13
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %67
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 3000)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %8
%30 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
store i32 %31, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %67
35: ; preds = %21
%36 = load i32, i32* %8
%37 = call i32 @listen(i32 %36, i32 1)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %67
41: ; preds = %35
%42 = load i32, i32* %8
%43 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%44 = call i32 @accept(i32 %42, %struct.sockaddr* %43, i32* %13)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %67
49: ; preds = %41
%50 = load i32, i32* %9
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 100)
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %67
57: ; preds = %49
%58 = load i32, i32* %12
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %58)
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %9
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
%66 = call i32 (...) @getchar()
br label %67
67: ; preds = %57, %55, %47, %39, %33, %19
%68 = load i32, i32* %6
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [26 x i8] c"\EC\9D\BD\EC\9D\80 \EB\B0\94\EC\9D\B4\ED\8A\B8 \EC\88\98 %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [100 x i8]
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%10 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%11 = load i32, i32* @PF_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 %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %51
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 3000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %13, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %51
29: ; preds = %17
%30 = call i32 @listen(i32 %13, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %51
34: ; preds = %29
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @accept(i32 %13, %struct.sockaddr* %35, i32* %6)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %51
40: ; preds = %34
%41 = call i32 @read(i32 %36, i8* %9, i32 100)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %51
45: ; preds = %40
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%47 = call i32 @puts(i8* %9)
%48 = call i32 @close(i32 %36)
%49 = call i32 @close(i32 %13)
%50 = call i32 (...) @getchar()
br label %51
51: ; preds = %45, %43, %38, %32, %27, %15
%52 = phi i32 [ -1, %15 ], [ %25, %27 ], [ 0, %32 ], [ 0, %38 ], [ 0, %43 ], [ 0, %45 ]
ret i32 %52
}