#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ STDIN_FILENO;
int atoi(char *);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                    int *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int conn_fd, port;
  char buf[1024];
  socklen_t addr_len;
  struct sockaddr_in serv_addr;
  if (argc != 2) {
    printf("Usage: %s portnumber\n", argv[0]);
    return -1;
  }
  port = atoi(argv[1]);
  memset(&serv_addr, 0, sizeof(struct sockaddr_in));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if ((conn_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    goto err;
  }
  bzero(buf, sizeof(buf));
  if ((read(STDIN_FILENO, buf, sizeof(buf))) < 0) {
    perror("read");
    goto err;
  }
  if ((sendto(conn_fd, buf, sizeof(buf), 0, (struct sockaddr *)&serv_addr,
              sizeof(struct sockaddr_in))) < 0) {
    perror("sendto");
    goto err;
  }
  bzero(buf, sizeof(buf));
  addr_len = sizeof(struct sockaddr_in);
  if ((recvfrom(conn_fd, buf, sizeof(buf), 0, (struct sockaddr *)&serv_addr,
                &addr_len)) < 0) {
    perror("recvfrom");
    goto err;
  }
  printf("Receive data from server: %s\n", buf);
  close(conn_fd);
  return 0;
err:
  close(conn_fd);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1202 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a0 <main+0x1e0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x18(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x42c(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x430(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    128c <main+0xcc>
lea    0xd94(%rip),%rdi
call   10b0 <perror@plt>
jmp    1391 <main+0x1d1>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jge    12d2 <main+0x112>
lea    0xd55(%rip),%rdi
call   10b0 <perror@plt>
jmp    1391 <main+0x1d1>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x430(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    $0x0,%rax
jge    1310 <main+0x150>
lea    0xd1c(%rip),%rdi
call   10b0 <perror@plt>
jmp    1391 <main+0x1d1>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
movl   $0xc,-0x424(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x430(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x424(%rbp),%r9
call   10a0 <recvfrom@plt>
cmp    $0x0,%rax
jge    136a <main+0x1aa>
lea    0xcc9(%rip),%rdi
call   10b0 <perror@plt>
jmp    1391 <main+0x1d1>
lea    -0x420(%rbp),%rsi
lea    0xcba(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a0 <main+0x1e0>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
cmp    $0x2,%edi
jne    12df <main+0x11f>
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df3(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1301 <main+0x141>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %rax,%rax
js     130a <main+0x14a>
lea    0x10(%rsp),%rsi
mov    %rsp,%r8
mov    %ebx,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
test   %rax,%rax
js     1313 <main+0x153>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
movl   $0xc,0xc(%rsp)
xor    %ebp,%ebp
mov    %rsp,%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   10a0 <recvfrom@plt>
test   %rax,%rax
js     131c <main+0x15c>
lea    0xd66(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    12f3 <main+0x133>
mov    (%rsi),%rsi
lea    0xd17(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd0e(%rip),%rdi
jmp    1323 <main+0x163>
lea    0xd0c(%rip),%rdi
jmp    1323 <main+0x163>
lea    0xd08(%rip),%rdi
jmp    1323 <main+0x163>
lea    0xd06(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    12ee <main+0x12e>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Usage: %s portnumber\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@STDIN_FILENO = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [7 x i8] c"sendto\00"
@.str.4 = [9 x i8] c"recvfrom\00"
@.str.5 = [30 x i8] c"Receive data from server: %s\0A\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 [1024 x i8]
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %74
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %7
%23 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %7
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = call i32 @htonl(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_DGRAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
store i32 %35, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %18
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %71
39: ; preds = %18
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = call i32 @bzero(i8* %40, i32 1024)
%42 = load i32, i32* @STDIN_FILENO
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = call i64 @read(i32 %42, i8* %43, i32 1024)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %71
48: ; preds = %39
%49 = load i32, i32* %6
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%51 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%52 = call i64 @sendto(i32 %49, i8* %50, i32 1024, i32 0, %struct.sockaddr* %51, i32 12)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %71
56: ; preds = %48
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%58 = call i32 @bzero(i8* %57, i32 1024)
store i32 12, i32* %9
%59 = load i32, i32* %6
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%62 = call i64 @recvfrom(i32 %59, i8* %60, i32 1024, i32 0, %struct.sockaddr* %61, i32* %9)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %56
%65 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %71
66: ; preds = %56
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%68 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %67)
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
store i32 0, i32* %3
br label %74
71: ; preds = %64, %54, %46, %37
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
store i32 -1, i32* %3
br label %74
74: ; preds = %71, %66, %13
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Usage: %s portnumber\0A\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@STDIN_FILENO = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [7 x i8] c"sendto\00"
@.str.4 = [9 x i8] c"recvfrom\00"
@.str.5 = [30 x i8] c"Receive data from server: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %49
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %16)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_DGRAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %45, label %29
29: ; preds = %13
%30 = call i32 @bzero(i8* %6, i32 1024)
%31 = load i32, i32* @STDIN_FILENO
%32 = call i64 @read(i32 %31, i8* %6, i32 1024)
%33 = icmp slt i64 %32, 0
br i1 %33, label %45, label %34
34: ; preds = %29
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i64 @sendto(i32 %27, i8* %6, i32 1024, i32 0, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %34
%39 = call i32 @bzero(i8* %6, i32 1024)
store i32 12, i32* %4
%40 = call i64 @recvfrom(i32 %27, i8* %6, i32 1024, i32 0, %struct.sockaddr* %35, i32* %4)
%41 = icmp slt i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%44 = call i32 @close(i32 %27)
br label %49
45: ; preds = %38, %34, %29, %13
%46 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %29 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %34 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), %38 ]
%47 = call i32 @perror(i8* %46)
%48 = call i32 @close(i32 %27)
br label %49
49: ; preds = %45, %42, %10
%50 = phi i32 [ -1, %10 ], [ -1, %45 ], [ 0, %42 ]
ret i32 %50
}