#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_DGRAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(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_DGRAM, 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);
  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     11e0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    0x2de2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <main+0x136>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1225 <main+0x95>
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
mov    0x2dad(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12c6 <main+0x136>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1080 <inet_pton@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <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    1266 <main+0xd6>
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,%r15
cmp    $0x2,%edi
jne    1251 <main+0xc1>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1279 <main+0xe9>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2e05(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%edi
mov    0x8(%r15),%rsi
mov    %r14,%rdx
call   1080 <inet_pton@plt>
mov    0x2dd4(%rip),%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
jmp    1220 <main+0x90>
mov    (%r15),%rsi
lea    0xda5(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d51(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd95(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d3c(%rip),%rcx
jmp    1267 <main+0xd7>

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_DGRAM = global i32 0
@.str.1 = [9 x i8] c"socket: \00"
@EIO = global i32 0
@AF_INET = global i32 0
@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 %52
19: ; preds = %2
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_DGRAM
%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 %52
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)
br label %41
41: ; preds = %29, %41
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%43 = load i32, i32* @stdin
%44 = call i32 @fgets(i8* %42, i32 1024, i32 %43)
%45 = load i32, i32* %6
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%50 = load i32, i32* %8
%51 = call i32 @sendto(i32 %45, i8* %46, i32 %48, i32 0, %struct.sockaddr* %49, i32 %50)
br label %41
52: ; preds = %25, %12
%53 = load i32, i32* %3
ret i32 %53
}

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_DGRAM = global i32 0
@.str.1 = [9 x i8] c"socket: \00"
@EIO = global i32 0
@AF_INET = global i32 0
@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 %35
11: ; preds = %2
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_DGRAM
%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 %35
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*
br label %30
30: ; preds = %30, %18
%31 = load i32, i32* @stdin
%32 = call i32 @fgets(i8* %6, i32 1024, i32 %31)
%33 = call i32 @strlen(i8* %6)
%34 = call i32 @sendto(i32 %14, i8* %6, i32 %33, i32 0, %struct.sockaddr* %29, i32 12)
br label %30
35: ; preds = %16, %8
%36 = phi i32* [ @EINVAL, %8 ], [ @EIO, %16 ]
%37 = load i32, i32* %36
%38 = sub nsw i32 0, %37
ret i32 %38
}