#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 /*<<< orphan*/ serverAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int write(int, char *, int);
int main(int argc, const char *argv[]) {
  int ret = -1;
  int clientSock;
  ssize_t writeSize;
  char writeBuf[100];
  struct sockaddr_in serverAddr;
  clientSock = socket(PF_INET, SOCK_STREAM, 0);
  if (clientSock == -1) {
    perror("socket");
    goto leave;
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  serverAddr.sin_port = htons(3000);
  if ((ret = connect(clientSock, (struct sockaddr *)&serverAddr,
                     sizeof(serverAddr)))) {
    perror("socket");
    goto leave;
  }
  strcpy(writeBuf, "Welcome ~!");
  writeSize = write(clientSock, writeBuf, sizeof("Welcome ~!"));
  if (writeSize == -1) {
    perror("write");
    goto leave;
  }
  printf("보낸 바이트 수 %ld\n", writeSize);
  printf("%s\n", writeBuf);
  close(clientSock);
  getchar();
leave:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f7 <main+0x57>
lea    0xe13(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ca <main+0x12a>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x88(%rbp)
lea    0xdfa(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x8c(%rbp)
mov    $0xbb8,%edi
call   1050 <htons@plt>
mov    %eax,-0x90(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1259 <main+0xb9>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ca <main+0x12a>
lea    -0x80(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0xb,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1298 <main+0xf8>
lea    0xd8e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ca <main+0x12a>
mov    -0x1c(%rbp),%esi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
mov    -0x14(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1272 <main+0xc2>
mov    %eax,%ebx
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe19(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0xbb8,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %eax,%eax
jne    1285 <main+0xd5>
lea    0xdf3(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xb,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1295 <main+0xe5>
lea    0xddc(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x10(%rsp),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
mov    %ebp,%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd87(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1269 <main+0xb9>
mov    %eax,%ebp
lea    0xd72(%rip),%rdi
call   10b0 <perror@plt>
jmp    1269 <main+0xb9>
lea    0xd80(%rip),%rdi
call   10b0 <perror@plt>
xor    %ebp,%ebp
jmp    1269 <main+0xb9>

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
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [11 x i8] c"Welcome ~!\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [26 x i8] c"\EB\B3\B4\EB\82\B8 \EB\B0\94\EC\9D\B4\ED\8A\B8 \EC\88\98 %ld\0A\00"
@.str.5 = [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 i32
%8 = alloca i32
%9 = alloca [100 x i8]
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %50
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 3000)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %7
%27 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%28 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 12)
store i32 %28, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %50
32: ; preds = %18
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %7
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%37 = call i32 @write(i32 %35, i8* %36, i32 11)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %50
42: ; preds = %32
%43 = load i32, i32* %8
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = call i32 (...) @getchar()
br label %50
50: ; preds = %42, %40, %30, %16
%51 = load i32, i32* %6
ret i32 %51
}

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
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [11 x i8] c"Welcome ~!\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [26 x i8] c"\EB\B3\B4\EB\82\B8 \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 [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %36
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 3000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = call i32 @connect(i32 %9, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %36
25: ; preds = %13
%26 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @write(i32 %9, i8* %5, i32 11)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %36
31: ; preds = %25
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%33 = call i32 @puts(i8* %5)
%34 = call i32 @close(i32 %9)
%35 = call i32 (...) @getchar()
br label %36
36: ; preds = %31, %29, %23, %11
%37 = phi i32 [ -1, %11 ], [ %21, %23 ], [ 0, %29 ], [ 0, %31 ]
ret i32 %37
}