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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  struct sockaddr_in stSockAddr;
  int Res;
  int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == SocketFD) {
    perror("cannot create socket");
    exit(EXIT_FAILURE);
  }
  memset(&stSockAddr, 0, sizeof(stSockAddr));
  stSockAddr.sin_family = AF_INET;
  stSockAddr.sin_port = htons(1100);
  Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);
  if (0 > Res) {
    perror("error: first parameter is not a valid address family");
    close(SocketFD);
    exit(EXIT_FAILURE);
  } else if (0 == Res) {
    perror("char string (second parameter does not contain valid ipaddress)");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }
  if (-1 ==
      connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
    perror("connect failed");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }
  shutdown(SocketFD, SHUT_RDWR);
  close(SocketFD);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11ea <main+0x5a>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x44c,%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rdx
lea    0xdee(%rip),%rsi
call   1070 <inet_pton@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jle    125c <main+0xcc>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jne    1289 <main+0xf9>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    128e <main+0xfe>
mov    -0x18(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12d0 <main+0x140>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1248 <main+0xb8>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e03(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0x44c,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
lea    0xe19(%rip),%rsi
mov    %r14,%rdx
call   1070 <inet_pton@plt>
test   %eax,%eax
js     1256 <main+0xc6>
je     125f <main+0xcf>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1268 <main+0xd8>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <shutdown@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdb1(%rip),%rdi
call   1080 <perror@plt>
jmp    127b <main+0xeb>
lea    0xdc2(%rip),%rdi
jmp    126f <main+0xdf>
lea    0xdee(%rip),%rdi
jmp    126f <main+0xdf>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [21 x i8] c"cannot create socket\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [53 x i8] c"error: first parameter is not a valid address family\00"
@.str.3 = [64 x i8] c"char string (second parameter does not contain valid ipaddress)\00"
@.str.4 = [15 x i8] c"connect failed\00"
@SHUT_RDWR = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 1100)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%23 = call i32 @inet_pton(i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp sgt i32 0, %24
br i1 %25, label %26, label %32
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %15
%33 = load i32, i32* %3
%34 = icmp eq i32 0, %33
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0))
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %32
br label %42
42: ; preds = %41
%43 = load i32, i32* %4
%44 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%45 = call i32 @connect(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp eq i32 -1, %45
br i1 %46, label %47, label %53
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %42
%54 = load i32, i32* %4
%55 = load i32, i32* @SHUT_RDWR
%56 = call i32 @shutdown(i32 %54, i32 %55)
%57 = load i32, i32* %4
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @EXIT_SUCCESS
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [21 x i8] c"cannot create socket\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [53 x i8] c"error: first parameter is not a valid address family\00"
@.str.3 = [64 x i8] c"char string (second parameter does not contain valid ipaddress)\00"
@.str.4 = [15 x i8] c"connect failed\00"
@SHUT_RDWR = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 1100)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%20 = call i32 @inet_pton(i32 %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %6)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %12
%28 = icmp eq i32 %20, 0
br i1 %28, label %29, label %34
29: ; preds = %27
%30 = call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @close(i32 %6)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %27
%35 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%36 = call i32 @connect(i32 %6, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @close(i32 %6)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %34
%44 = load i32, i32* @SHUT_RDWR
%45 = call i32 @shutdown(i32 %6, i32 %44)
%46 = call i32 @close(i32 %6)
%47 = load i32, i32* @EXIT_SUCCESS
ret i32 %47
}