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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
char *strerror(int);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd, n;
  char recvline[4096], sendline[4096];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    printf("usage: ./client <ipaddress>\n");
    printf("example: ./socket_c 127.0.0.1\n");
    exit(0);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(6666);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
    printf("inet_pton error for %s\n", argv[1]);
    exit(0);
  }
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  printf("send msg to server: \n");
  fgets(sendline, 4096, stdin);
  if (send(sockfd, sendline, strlen(sendline), 0) < 0) {
    printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  close(sockfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1206 <main+0x46>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    125a <main+0x9a>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    -0x2030(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2028(%rbp)
mov    $0x1a0a,%edi
call   1040 <htons@plt>
mov    %eax,-0x202c(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x2030(%rbp),%rdx
call   10a0 <inet_pton@plt>
cmp    $0x0,%rax
jg     12d0 <main+0x110>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    131d <main+0x15d>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x2020(%rbp),%rdi
mov    0x2ca7(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   1090 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x203c(%rbp)
lea    -0x2020(%rbp),%rax
mov    %rax,-0x2038(%rbp)
lea    -0x2020(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x203c(%rbp),%edi
mov    -0x2038(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0x0,%rax
jge    13b7 <main+0x1f7>
mov    0x2c39(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c28(%rip),%rax
mov    (%rax),%edx
lea    0xd03(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
cmp    $0x2,%edi
jne    122b <main+0x5b>
mov    %rsi,%rbx
mov    0x2dfc(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
jns    124a <main+0x7a>
mov    0x2dbe(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xde6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %eax,%r15d
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x1a0a,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   10b0 <inet_pton@plt>
test   %rax,%rax
jg     12a1 <main+0xd1>
mov    0x8(%rbx),%rsi
lea    0xd91(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jns    12d2 <main+0x102>
mov    0x2d0b(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd6f(%rip),%rdi
jmp    121a <main+0x4a>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %rax,%rax
jns    1331 <main+0x161>
mov    0x2cac(%rip),%rbx
mov    (%rbx),%edi
call   10e0 <strerror@plt>
mov    (%rbx),%edx
lea    0xd2e(%rip),%rdi
jmp    121a <main+0x4a>
mov    %r15d,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"usage: ./client <ipaddress>\0A\00"
@.str.1 = [31 x i8] c"example: ./socket_c 127.0.0.1\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@.str.3 = [24 x i8] c"inet_pton error for %s\0A\00"
@.str.4 = [30 x i8] c"connect error: %s(errno: %d)\0A\00"
@.str.5 = [22 x i8] c"send msg to server: \0A\00"
@stdin = global i32 0
@.str.6 = [31 x i8] c"send msg error: %s(errno: %d)\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 [4096 x i8]
%9 = alloca [4096 x i8]
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %25)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %17
%29 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %30, i32* %31
%32 = call i32 @htons(i32 6666)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @AF_INET
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%39 = call i64 @inet_pton(i32 %34, i8* %37, i32* %38)
%40 = icmp sle i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %28
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %28
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%50 = call i64 @connect(i32 %48, %struct.sockaddr* %49, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = load i32, i32* @errno
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %54, i32 %55)
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %47
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%61 = load i32, i32* @stdin
%62 = call i32 @fgets(i8* %60, i32 4096, i32 %61)
%63 = load i32, i32* %6
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%66 = call i32 @strlen(i8* %65)
%67 = call i64 @send(i32 %63, i8* %64, i32 %66, i32 0)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %58
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = load i32, i32* @errno
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %71, i32 %72)
%74 = call i32 @exit(i32 0)
unreachable
75: ; preds = %58
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
%78 = call i32 @exit(i32 0)
unreachable
}

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.2 = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@.str.3 = [24 x i8] c"inet_pton error for %s\0A\00"
@.str.4 = [30 x i8] c"connect error: %s(errno: %d)\0A\00"
@stdin = global i32 0
@.str.6 = [31 x i8] c"send msg error: %s(errno: %d)\0A\00"
@str = [21 x i8] c"send msg to server: \00"
@str.7 = [28 x i8] c"usage: ./client <ipaddress>\00"
@str.8 = [30 x i8] c"example: ./socket_c 127.0.0.1\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = load i32, i32* @errno
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %19, i32 %20)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %12
%24 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %25, i32* %26
%27 = call i32 @htons(i32 6666)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds i8*, i8** %1, i64 1
%31 = load i8*, i8** %30
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%33 = call i64 @inet_pton(i32 %29, i8* %31, i32* %32)
%34 = icmp slt i64 %33, 1
br i1 %34, label %35, label %39
35: ; preds = %23
%36 = load i8*, i8** %30
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %23
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i64 @connect(i32 %15, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = load i32, i32* @errno
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %45, i32 %46)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %39
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%51 = load i32, i32* @stdin
%52 = call i32 @fgets(i8* %5, i32 4096, i32 %51)
%53 = call i32 @strlen(i8* %5)
%54 = call i64 @send(i32 %15, i8* %5, i32 %53, i32 0)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %49
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = load i32, i32* @errno
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %58, i32 %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %49
%63 = call i32 @close(i32 %15)
%64 = call i32 @exit(i32 0)
unreachable
}