#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__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ INADDR_NONE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Usage: %s ip port", argv[0]);
    exit(1);
  }
  printf("This is a UDP client\n");
  struct sockaddr_in addr;
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(atoi(argv[2]));
  addr.sin_addr.s_addr = inet_addr(argv[1]);
  if (addr.sin_addr.s_addr == INADDR_NONE) {
    printf("Incorrect ip address!");
    close(sock);
    exit(1);
  }
  char buff[512];
  int len = sizeof(addr);
  while (1) {
    gets(buff);
    int n;
    n = sendto(sock, buff, strlen(buff), 0, (struct sockaddr *)&addr,
               sizeof(addr));
    if (n < 0) {
      perror("sendto");
      close(sock);
      break;
    }
    n = recvfrom(sock, buff, 512, 0, (struct sockaddr *)&addr, &len);
    if (n > 0) {
      buff[n] = 0;
      printf("received:");
      puts(buff);
    } else if (n == 0) {
      printf("server closed\n");
      close(sock);
      break;
    } else if (n == -1) {
      perror("recvfrom");
      close(sock);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1212 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    125b <main+0x8b>
lea    0xddc(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <inet_addr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
jne    12c3 <main+0xf3>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
movl   $0x10,-0x234(%rbp)
lea    -0x230(%rbp),%rdi
call   10a0 <gets@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x244(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x240(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x244(%rbp),%edi
mov    -0x240(%rbp),%rsi
mov    %eax,%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0x0,-0x238(%rbp)
jge    1348 <main+0x178>
lea    0xd0f(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    1407 <main+0x237>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x234(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0x0,-0x238(%rbp)
jle    13aa <main+0x1da>
movslq -0x238(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x230(%rbp),%rdi
call   1030 <puts@plt>
jmp    1402 <main+0x232>
cmpl   $0x0,-0x238(%rbp)
jne    13d2 <main+0x202>
lea    0xc98(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    1407 <main+0x237>
cmpl   $0xffffffff,-0x238(%rbp)
jne    13f8 <main+0x228>
lea    0xc7f(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    1407 <main+0x237>
jmp    13fd <main+0x22d>
jmp    1402 <main+0x232>
jmp    12cd <main+0xfd>
xor    %eax,%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x220,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1355 <main+0x185>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dea(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1370 <main+0x1a0>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0x1c(%rsp)
mov    0x10(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x8(%rbx),%rdi
call   1090 <inet_addr@plt>
mov    %rax,0x10(%rsp)
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%rax
je     1386 <main+0x1b6>
movl   $0x10,0xc(%rsp)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <gets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x10(%rsp),%r8
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
js     132f <main+0x15f>
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r15
lea    0xd8c(%rip),%r12
jmp    12f5 <main+0x125>
nopl   0x0(%rax)
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   10a0 <gets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    $0x10,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
js     132f <main+0x15f>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   10b0 <recvfrom@plt>
test   %eax,%eax
jg     12b0 <main+0xe0>
je     1321 <main+0x151>
cmp    $0xffffffff,%eax
jne    12c9 <main+0xf9>
lea    0xd21(%rip),%rdi
jmp    1336 <main+0x166>
lea    0xd36(%rip),%rdi
call   1030 <puts@plt>
jmp    133b <main+0x16b>
lea    0xcf9(%rip),%rdi
call   10c0 <perror@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xca1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xc9b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xc8c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Usage: %s ip port\00"
@.str.1 = [22 x i8] c"This is a UDP client\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@INADDR_NONE = global i64 0
@.str.3 = [22 x i8] c"Incorrect ip address!\00"
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [10 x i8] c"received:\00"
@.str.6 = [15 x i8] c"server closed\0A\00"
@.str.7 = [9 x i8] c"recvfrom\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca i32
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, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i64 @inet_addr(i8* %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i64 %40, i64* %42
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = load i64, i64* @INADDR_NONE
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %53
48: ; preds = %28
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %28
store i32 16, i32* %9
br label %54
54: ; preds = %53, %99
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%56 = call i32 @gets(i8* %55)
%57 = load i32, i32* %7
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
%61 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%62 = call i32 @sendto(i32 %57, i8* %58, i32 %60, i32 0, %struct.sockaddr* %61, i32 16)
store i32 %62, i32* %10
%63 = load i32, i32* %10
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %54
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
br label %100
69: ; preds = %54
%70 = load i32, i32* %7
%71 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%72 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%73 = call i32 @recvfrom(i32 %70, i8* %71, i32 512, i32 0, %struct.sockaddr* %72, i32* %9)
store i32 %73, i32* %10
%74 = load i32, i32* %10
%75 = icmp sgt i32 %74, 0
br i1 %75, label %76, label %83
76: ; preds = %69
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %78
store i8 0, i8* %79
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%82 = call i32 @puts(i8* %81)
br label %99
83: ; preds = %69
%84 = load i32, i32* %10
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%88 = load i32, i32* %7
%89 = call i32 @close(i32 %88)
br label %100
90: ; preds = %83
%91 = load i32, i32* %10
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %97
93: ; preds = %90
%94 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%95 = load i32, i32* %7
%96 = call i32 @close(i32 %95)
br label %100
97: ; preds = %90
br label %98
98: ; preds = %97
br label %99
99: ; preds = %98, %76
br label %54
100: ; preds = %93, %86, %65
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Usage: %s ip port\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@INADDR_NONE = global i64 0
@.str.3 = [22 x i8] c"Incorrect ip address!\00"
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [10 x i8] c"received:\00"
@.str.7 = [9 x i8] c"recvfrom\00"
@str = [21 x i8] c"This is a UDP client\00"
@str.8 = [14 x i8] c"server closed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = icmp eq i32 %0, 3
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i32 @atoi(i8* %25)
%27 = tail call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = tail call i64 @inet_addr(i8* %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 %31, i64* %32
%33 = load i64, i64* @INADDR_NONE
%34 = icmp eq i64 %31, %33
br i1 %34, label %35, label %39
35: ; preds = %21
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @close(i32 %16)
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %21
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%41 = bitcast i32* %5 to i8*
store i32 16, i32* %5
%42 = call i32 @gets(i8* %40)
%43 = call i32 @strlen(i8* %40)
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%45 = call i32 @sendto(i32 %16, i8* %40, i32 %43, i32 0, %struct.sockaddr* %44, i32 16)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %62, %39
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %67
49: ; preds = %39, %62
%50 = call i32 @recvfrom(i32 %16, i8* %40, i32 512, i32 0, %struct.sockaddr* %44, i32* %5)
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = zext i32 %50 to i64
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %53
store i8 0, i8* %54
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%56 = call i32 @puts(i8* %40)
br label %62
57: ; preds = %49
switch i32 %50, label %62 [
i32 0, label %58
i32 -1, label %60
]
58: ; preds = %57
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
br label %67
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %67
62: ; preds = %52, %57
%63 = call i32 @gets(i8* %40)
%64 = call i32 @strlen(i8* %40)
%65 = call i32 @sendto(i32 %16, i8* %40, i32 %64, i32 0, %struct.sockaddr* %44, i32 16)
%66 = icmp slt i32 %65, 0
br i1 %66, label %47, label %49
67: ; preds = %47, %58, %60
%68 = call i32 @close(i32 %16)
ret i32 0
}