#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*/ ClientGreet(int);
int EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_in servAddr;
  int err;
  if (argc != 2) {
    printf("usage: ./client-tcp  <IP address>\n");
    return EXIT_FAILURE;
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("Failed to create socket. errono: %i\n", errno);
    return EXIT_FAILURE;
  }
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_port = htons(11111);
  if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) < 1) {
    err = errno;
    printf("Invalid Address. errno: %i\n", err);
    return EXIT_FAILURE;
  }
  if (connect(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    err = errno;
    printf("Connect error. errno: %i\n", err);
    return EXIT_FAILURE;
  }
  ClientGreet(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11af <main+0x3f>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12cf <main+0x15f>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fd <main+0x8d>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12cf <main+0x15f>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x2b67,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
jge    1277 <main+0x107>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12cf <main+0x15f>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    12c0 <main+0x150>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12cf <main+0x15f>
mov    -0x14(%rbp),%edi
call   1070 <ClientGreet@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1213 <main+0x93>
mov    %rsi,%rbx
mov    0x2e4b(%rip),%r15
mov    (%r15),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1221 <main+0xa1>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    $0x2b67,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r15),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   1070 <inet_pton@plt>
test   %eax,%eax
jle    1233 <main+0xb3>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1245 <main+0xc5>
mov    %ebp,%edi
call   1080 <ClientGreet@plt>
xor    %eax,%eax
jmp    1265 <main+0xe5>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
jmp    125c <main+0xdc>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    0xdcf(%rip),%rdi
jmp    1255 <main+0xd5>
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
lea    0xde2(%rip),%rdi
jmp    1255 <main+0xd5>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
lea    0xdec(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"usage: ./client-tcp <IP address>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [37 x i8] c"Failed to create socket. errono: %i\0A\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"Invalid Address. errno: %i\0A\00"
@.str.3 = [26 x i8] c"Connect error. errno: %i\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %3
br label %55
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @EXIT_FAILURE
store i32 %23, i32* %3
br label %55
24: ; preds = %14
%25 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @htons(i32 11111)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @AF_INET
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%35 = call i32 @inet_pton(i32 %30, i8* %33, i32* %34)
%36 = icmp slt i32 %35, 1
br i1 %36, label %37, label %42
37: ; preds = %24
%38 = load i32, i32* @errno
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* @EXIT_FAILURE
store i32 %41, i32* %3
br label %55
42: ; preds = %24
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%45 = call i64 @connect(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = load i32, i32* @errno
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %3
br label %55
52: ; preds = %42
%53 = load i32, i32* %6
%54 = call i32 @ClientGreet(i32 %53)
store i32 0, i32* %3
br label %55
55: ; preds = %52, %47, %37, %20, %11
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [37 x i8] c"Failed to create socket. errono: %i\0A\00"
@errno = global i32 0
@.str.2 = [28 x i8] c"Invalid Address. errno: %i\0A\00"
@.str.3 = [26 x i8] c"Connect error. errno: %i\0A\00"
@str = [34 x i8] c"usage: ./client-tcp <IP address>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
br label %44
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
br label %44
18: ; preds = %9
%19 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 11111)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = call i32 @inet_pton(i32 %24, i8* %26, i32* %27)
%29 = icmp slt i32 %28, 1
br i1 %29, label %30, label %34
30: ; preds = %18
%31 = load i32, i32* @errno
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
br label %44
34: ; preds = %18
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i64 @connect(i32 %12, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = load i32, i32* @EXIT_FAILURE
br label %44
42: ; preds = %34
%43 = call i32 @ClientGreet(i32 %12)
br label %44
44: ; preds = %42, %38, %30, %14, %6
%45 = phi i32 [ %8, %6 ], [ %17, %14 ], [ %33, %30 ], [ %41, %38 ], [ 0, %42 ]
ret i32 %45
}