#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 {
  scalar_t__ sin_family;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ s;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpeername(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ printf(char *);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_in s;
  struct sockaddr sa;
  struct sockaddr_in *sa_in = (struct sockaddr_in *)&sa;
  socklen_t sa_len = sizeof(struct sockaddr_in);
  if (argc != 3) {
    fprintf(stderr, "Usage: %s <ip> <port>\n", argv[0]);
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "Failed to create socket! (%s)\n", strerror(errno));
    exit(1);
  }
  memset(&s, 0x0, sizeof(s));
  s.sin_family = AF_INET;
  s.sin_port = htons(atoi(argv[2]));
  if (inet_aton(argv[1], &s.sin_addr) == 0) {
    fprintf(stderr, "%s is not a valid address\n", argv[1]);
    exit(2);
  }
  if (connect(sockfd, (const struct sockaddr *)&s, sizeof(s)) != 0) {
    fprintf(stderr, "Failed to connect! (%s)\n", strerror(errno));
    exit(2);
  }
  if (getpeername(sockfd, &sa, &sa_len) != 0) {
    fprintf(stderr, "getpeername failed! (%s)\n", strerror(errno));
  }
  if ((sa_in->sin_family != AF_INET) ||
      (sa_len != sizeof(struct sockaddr_in))) {
    fprintf(stderr, "WTF!\n");
  }
  printf("Test suceeded.\n");
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x10,-0x3c(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1217 <main+0x57>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1275 <main+0xb5>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    12f2 <main+0x132>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
je     1345 <main+0x185>
mov    0x2cc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xd1d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1030 <getpeername@plt>
cmp    $0x0,%rax
je     138d <main+0x1cd>
mov    0x2c72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xce4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    0x2c4d(%rip),%rcx
cmp    (%rcx),%rax
jne    13b2 <main+0x1f2>
movslq -0x3c(%rbp),%rax
cmp    $0x10,%rax
je     13c9 <main+0x209>
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
lea    0xcc2(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    0xcba(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
movl   $0x10,0xc(%rsp)
cmp    $0x3,%edi
jne    12e9 <main+0x129>
mov    0x2e03(%rip),%r15
mov    (%r15),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12fe <main+0x13e>
mov    %eax,%ebx
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    (%r15),%rax
mov    %rax,0x18(%rsp)
mov    0x10(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x24(%rsp)
mov    0x8(%r14),%rdi
lea    0x20(%rsp),%rsi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     1332 <main+0x172>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10c0 <connect@plt>
test   %rax,%rax
jne    1348 <main+0x188>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <getpeername@plt>
test   %rax,%rax
je     12a1 <main+0xe1>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebp
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdd5(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x10(%rsp),%rax
cmp    (%r15),%rax
jne    12b2 <main+0xf2>
cmpl   $0x10,0xc(%rsp)
je     12c9 <main+0x109>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd04(%rip),%rsi
jmp    1321 <main+0x161>
mov    0x2cd3(%rip),%rax
mov    (%rax),%ebx
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xcfb(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xcf0(%rip),%rsi
jmp    136b <main+0x1ab>
mov    0x2c89(%rip),%rax
mov    (%rax),%ebx
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xceb(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s <ip> <port>\0A\00"
@AF_INET = global i64 0
@SOCK_STREAM = global i32 0
@.str.1 = [31 x i8] c"Failed to create socket! (%s)\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"%s is not a valid address\0A\00"
@.str.3 = [25 x i8] c"Failed to connect! (%s)\0A\00"
@.str.4 = [26 x i8] c"getpeername failed! (%s)\0A\00"
@.str.5 = [6 x i8] c"WTF!\0A\00"
@.str.6 = [16 x i8] c"Test suceeded.\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 %struct.sockaddr
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.sockaddr* %8 to %struct.sockaddr_in*
store %struct.sockaddr_in* %11, %struct.sockaddr_in** %9
store i32 16, i32* %10
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i64, i64* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i64 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 16)
%34 = load i64, i64* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i64 %34, i64* %35
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %40, i32* %41
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%46 = call i64 @inet_aton(i8* %44, i32* %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %32
%49 = load i32, i32* @stderr
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 2)
unreachable
55: ; preds = %32
%56 = load i32, i32* %6
%57 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%58 = call i64 @connect(i32 %56, %struct.sockaddr* %57, i32 16)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 2)
unreachable
66: ; preds = %55
%67 = load i32, i32* %6
%68 = call i64 @getpeername(i32 %67, %struct.sockaddr* %8, i32* %10)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %66
%71 = load i32, i32* @stderr
%72 = load i32, i32* @errno
%73 = call i8* @strerror(i32 %72)
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %70, %66
%76 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %76, i32 0, i32 0
%78 = load i64, i64* %77
%79 = load i64, i64* @AF_INET
%80 = icmp ne i64 %78, %79
br i1 %80, label %85, label %81
81: ; preds = %75
%82 = load i32, i32* %10
%83 = sext i32 %82 to i64
%84 = icmp ne i64 %83, 16
br i1 %84, label %85, label %88
85: ; preds = %81, %75
%86 = load i32, i32* @stderr
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %88
88: ; preds = %85, %81
%89 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s <ip> <port>\0A\00"
@AF_INET = global i64 0
@SOCK_STREAM = global i32 0
@.str.1 = [31 x i8] c"Failed to create socket! (%s)\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"%s is not a valid address\0A\00"
@.str.3 = [25 x i8] c"Failed to connect! (%s)\0A\00"
@.str.4 = [26 x i8] c"getpeername failed! (%s)\0A\00"
@.str.5 = [6 x i8] c"WTF!\0A\00"
@str = [15 x i8] c"Test suceeded.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 16, i32* %5
%9 = icmp eq i32 %0, 3
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i64, i64* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i64 %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 16)
%28 = load i64, i64* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i64 %28, i64* %29
%30 = getelementptr inbounds i8*, i8** %1, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %33, i32* %34
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%38 = call i64 @inet_aton(i8* %36, i32* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %26
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %35
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @exit(i32 2)
unreachable
45: ; preds = %26
%46 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%47 = call i64 @connect(i32 %18, %struct.sockaddr* %46, i32 16)
%48 = icmp eq i64 %47, 0
br i1 %48, label %55, label %49
49: ; preds = %45
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 2)
unreachable
55: ; preds = %45
%56 = call i64 @getpeername(i32 %18, %struct.sockaddr* %4, i32* %5)
%57 = icmp eq i64 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %58, %55
%64 = bitcast %struct.sockaddr* %4 to i64*
%65 = load i64, i64* %64
%66 = load i64, i64* @AF_INET
%67 = icmp ne i64 %65, %66
%68 = load i32, i32* %5
%69 = icmp ne i32 %68, 16
%70 = select i1 %67, i1 true, i1 %69
br i1 %70, label %71, label %74
71: ; preds = %63
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %74
74: ; preds = %63, %71
%75 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%76 = call i32 @close(i32 %18)
ret i32 0
}