#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 socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ mesg;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EINVAL;
int EIO;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_in servaddr;
  socklen_t len = sizeof(servaddr);
  char mesg[1024];
  if (argc != 2) {
    printf("Usage: %s <ip_addr>\n", argv[0]);
    return -EINVAL;
  }
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("socket: ");
    return -EIO;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(54321);
  inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("connect: ");
    close(sockfd);
    return -EIO;
  }
  while (1) {
    fgets(mesg, sizeof(mesg), stdin);
    sendto(sockfd, mesg, strlen(mesg), 0, (const struct sockaddr *)&servaddr,
           len);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1200 <main+0x50>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe18(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2dc2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <main+0x17d>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1245 <main+0x95>
lea    0xde8(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
mov    0x2d95(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <main+0x17d>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <inet_pton@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12c8 <main+0x118>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    0x2d12(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    132d <main+0x17d>
jmp    12cd <main+0x11d>
lea    -0x430(%rbp),%rdi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x430(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    -0x20(%rbp),%r8
mov    -0x24(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
jmp    12cd <main+0x11d>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1282 <main+0xd2>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12aa <main+0xfa>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2de4(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
mov    0x8(%r14),%rsi
mov    %rbx,%rdx
call   1090 <inet_pton@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     12b8 <main+0x108>
mov    0x2d98(%rip),%r14
lea    0x10(%rsp),%rbx
mov    %rsp,%rbp
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
jmp    1250 <main+0xa0>
mov    (%r14),%rsi
lea    0xd74(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d20(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <main+0x11c>
lea    0xd5f(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    0x2cfd(%rip),%rcx
jmp    1298 <main+0xe8>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"Usage: %s <ip_addr>\0A\00"
@EINVAL = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [9 x i8] c"socket: \00"
@EIO = global i32 0
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"connect: \00"
@stdin = global i32 0
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
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EINVAL
%18 = sub nsw i32 0, %17
store i32 %18, i32* %3
br label %63
19: ; preds = %2
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EIO
%28 = sub nsw i32 0, %27
store i32 %28, i32* %3
br label %63
29: ; preds = %19
%30 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @htons(i32 54321)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* @AF_INET
%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* %7, i32 0, i32 0
%40 = call i32 @inet_pton(i32 %35, i8* %38, i32* %39)
%41 = load i32, i32* %6
%42 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%43 = call i64 @connect(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %29
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* @EIO
%50 = sub nsw i32 0, %49
store i32 %50, i32* %3
br label %63
51: ; preds = %29
br label %52
52: ; preds = %51, %52
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = load i32, i32* @stdin
%55 = call i32 @fgets(i8* %53, i32 1024, i32 %54)
%56 = load i32, i32* %6
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%59 = call i32 @strlen(i8* %58)
%60 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%61 = load i32, i32* %8
%62 = call i32 @sendto(i32 %56, i8* %57, i32 %59, i32 0, %struct.sockaddr* %60, i32 %61)
br label %52
63: ; preds = %45, %25, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"Usage: %s <ip_addr>\0A\00"
@EINVAL = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [9 x i8] c"socket: \00"
@EIO = global i32 0
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"connect: \00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %40
11: ; preds = %2
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %40
18: ; preds = %11
%19 = call i32 @bzero(%struct.sockaddr_in* %3, 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 = tail call i32 @htons(i32 54321)
%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 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @connect(i32 %14, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @close(i32 %14)
br label %40
35: ; preds = %18, %35
%36 = load i32, i32* @stdin
%37 = call i32 @fgets(i8* %6, i32 1024, i32 %36)
%38 = call i32 @strlen(i8* %6)
%39 = call i32 @sendto(i32 %14, i8* %6, i32 %38, i32 0, %struct.sockaddr* %29, i32 12)
br label %35
40: ; preds = %32, %16, %8
%41 = phi i32* [ @EINVAL, %8 ], [ @EIO, %16 ], [ @EIO, %32 ]
%42 = load i32, i32* %41
%43 = sub nsw i32 0, %42
ret i32 %43
}