#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int serve_new_conn(int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int sk, port, ret;
  struct sockaddr_in addr;
  if (argc < 3) {
    printf("Need addr, port and iters\n");
    return -1;
  }
  sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sk < 0) {
    perror("Can't create socket");
    return -1;
  }
  port = atoi(argv[2]);
  printf("Connecting to %s:%d\n", argv[1], port);
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  ret = inet_aton(argv[1], &addr.sin_addr);
  if (ret < 0) {
    perror("Can't convert addr");
    return -1;
  }
  addr.sin_port = htons(port);
  ret = connect(sk, (struct sockaddr *)&addr, sizeof(addr));
  if (ret < 0) {
    perror("Can't connect");
    return -1;
  }
  return serve_new_conn(0, sk);
}


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   $0x3,-0x8(%rbp)
jge    11ca <main+0x3a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <main+0x14c>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120f <main+0x7f>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
call   1060 <inet_aton@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128e <main+0xfe>
lea    0xdc7(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <main+0x14c>
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12cf <main+0x13f>
lea    0xd99(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <main+0x14c>
mov    -0x14(%rbp),%esi
xor    %edi,%edi
call   10a0 <serve_new_conn@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jg     11c3 <main+0x23>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1266 <main+0xc6>
mov    %rsi,%rbx
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     126f <main+0xcf>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
lea    0xe0d(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%rdi
lea    0x4(%rsp),%rsi
call   1070 <inet_aton@plt>
test   %eax,%eax
js     1278 <main+0xd8>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     1281 <main+0xe1>
xor    %edi,%edi
mov    %r14d,%esi
call   10b0 <serve_new_conn@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8a(%rip),%rdi
jmp    1288 <main+0xe8>
lea    0xdaa(%rip),%rdi
jmp    1288 <main+0xe8>
lea    0xdb4(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1266 <main+0xc6>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Need addr, port and iters\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [20 x i8] c"Can't create socket\00"
@.str.2 = [21 x i8] c"Connecting to %s:%d\0A\00"
@AF_INET = global i32 0
@.str.3 = [19 x i8] c"Can't convert addr\00"
@.str.4 = [14 x i8] c"Can't connect\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 %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
14: ; preds = %2
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = load i32, i32* @IPPROTO_TCP
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
23: ; preds = %14
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %7
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* %7
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32 %31)
%33 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%40 = call i32 @inet_aton(i8* %38, i32* %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %23
%44 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
45: ; preds = %23
%46 = load i32, i32* %7
%47 = call i32 @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%51 = call i32 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
56: ; preds = %45
%57 = load i32, i32* %6
%58 = call i32 @serve_new_conn(i32 0, i32 %57)
store i32 %58, i32* %3
br label %59
59: ; preds = %56, %54, %43, %21, %12
%60 = load i32, i32* %3
ret i32 %60
}

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.1 = [20 x i8] c"Can't create socket\00"
@.str.2 = [21 x i8] c"Connecting to %s:%d\0A\00"
@AF_INET = global i32 0
@.str.3 = [19 x i8] c"Can't convert addr\00"
@.str.4 = [14 x i8] c"Can't connect\00"
@str = [26 x i8] c"Need addr, port and iters\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %42
8: ; preds = %2
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = tail call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %42
16: ; preds = %8
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 %19)
%23 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i8*, i8** %20
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%28 = call i32 @inet_aton(i8* %26, i32* %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %42
32: ; preds = %16
%33 = call i32 @htons(i32 %19)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @connect(i32 %12, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %42
40: ; preds = %32
%41 = call i32 @serve_new_conn(i32 0, i32 %12)
br label %42
42: ; preds = %40, %38, %30, %14, %6
%43 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %30 ], [ -1, %38 ], [ %41, %40 ]
ret i32 %43
}