#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ to_addr;
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
struct TYPE_4__ {
  int /*<<< orphan*/ p_proto;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
TYPE_2__ *getprotobyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int sendto(int, void *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  int sock_fd;
  struct sockaddr_in to_addr;
  char to_host[256];
  struct in_addr to_ip;
  int to_port = 2000;
  char buf[4096];
  if (argc != 3) {
    usage(argv[0]);
    exit(1);
  }
  strcpy(to_host, argv[1]);
  int pktsize = atoi(argv[2]);
  if ((pktsize < 0) || (pktsize > 4096)) {
    fprintf(stderr, "Packet size has to be >= 0 and <= %d\n", 4096);
    exit(2);
  }
  sock_fd = socket(PF_INET, SOCK_DGRAM, getprotobyname("udp")->p_proto);
  if (sock_fd < 0) {
    perror("socket failed");
    exit(20);
  }
  fprintf(stderr, "socket successful\n");
  struct hostent *server = gethostbyname(to_host);
  bzero((char *)&to_addr, sizeof(to_addr));
  bcopy((char *)server->h_addr, (char *)&to_addr.sin_addr.s_addr,
        server->h_length);
  to_addr.sin_family = PF_INET;
  to_addr.sin_port = htons(to_port);
  int res = sendto(sock_fd, (void *)(buf), pktsize, 0,
                   (struct sockaddr *)&to_addr, sizeof(to_addr));
  if (res != pktsize) {
    perror("sendto");
    exit(3);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1150,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x7d0,-0x12c(%rbp)
cmpl   $0x3,-0x4(%rbp)
je     120c <main+0x3c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <usage@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x1134(%rbp)
cmpl   $0x0,-0x1134(%rbp)
jl     1250 <main+0x80>
cmpl   $0x1000,-0x1134(%rbp)
jle    1276 <main+0xa6>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    $0x1000,%edx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x114c(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1148(%rbp)
lea    0xd8b(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    -0x114c(%rbp),%edi
mov    -0x1148(%rbp),%esi
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12d6 <main+0x106>
lea    0xd63(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x14,%edi
call   10e0 <exit@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x120(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x1140(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    -0x1140(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
add    $0x8,%rsi
mov    -0x1140(%rbp),%rax
mov    (%rax),%edx
call   1090 <bcopy@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x12c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1130(%rbp),%rsi
mov    -0x1134(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1050 <sendto@plt>
mov    %eax,-0x1144(%rbp)
mov    -0x1144(%rbp),%eax
cmp    -0x1134(%rbp),%eax
je     1398 <main+0x1c8>
lea    0xcc2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
xor    %eax,%eax
add    $0x1150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1110,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    12d2 <main+0x102>
mov    0x8(%rbx),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
mov    0x10(%rbx),%rdi
call   10d0 <atoi@plt>
cmp    $0x1001,%eax
jae    12e4 <main+0x114>
mov    %eax,%r15d
mov    0x2db9(%rip),%r12
mov    (%r12),%ebp
mov    0x2dc6(%rip),%rax
mov    (%rax),%ebx
lea    0xdfb(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   10f0 <socket@plt>
test   %eax,%eax
js     130a <main+0x13a>
mov    %eax,%ebp
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0x10(%rsp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x8(%rbx),%rsi
lea    0x8(%rsp),%rdi
mov    (%rbx),%edx
call   10a0 <memmove@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
lea    0x110(%rsp),%rsi
mov    %ebp,%edi
mov    %r15d,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1050 <sendto@plt>
cmp    %r15d,%eax
jne    1320 <main+0x150>
xor    %eax,%eax
add    $0x1110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rdi
call   1090 <usage@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd0c(%rip),%rsi
mov    $0x1000,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    0xd19(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x14,%edi
call   10e0 <exit@plt>
lea    0xd24(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Packet size has to be >= 0 and <= %d\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [4 x i8] c"udp\00"
@.str.2 = [14 x i8] c"socket failed\00"
@.str.3 = [19 x i8] c"socket successful\0A\00"
@.str.4 = [7 x i8] c"sendto\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca [256 x i8]
%8 = alloca %struct.in_addr
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i32
%12 = alloca %struct.hostent*
%13 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 2000, i32* %9
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %4
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @usage(i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%24 = load i8**, i8*** %4
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strcpy(i8* %23, i8* %26)
%28 = load i8**, i8*** %4
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp slt i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %22
%35 = load i32, i32* %11
%36 = icmp sgt i32 %35, 4096
br i1 %36, label %37, label %41
37: ; preds = %34, %22
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 4096)
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %34
%42 = load i32, i32* @PF_INET
%43 = load i32, i32* @SOCK_DGRAM
%44 = call %struct.TYPE_4__* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%45 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @socket(i32 %42, i32 %43, i32 %46)
store i32 %47, i32* %5
%48 = load i32, i32* %5
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 20)
unreachable
53: ; preds = %41
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%57 = call %struct.hostent* @gethostbyname(i8* %56)
store %struct.hostent* %57, %struct.hostent** %12
%58 = bitcast %struct.sockaddr_in* %6 to i8*
%59 = call i32 @bzero(i8* %58, i32 12)
%60 = load %struct.hostent*, %struct.hostent** %12
%61 = getelementptr inbounds %struct.hostent, %struct.hostent* %60, i32 0, i32 1
%62 = load i64, i64* %61
%63 = inttoptr i64 %62 to i8*
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%65 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %64, i32 0, i32 0
%66 = bitcast i32* %65 to i8*
%67 = load %struct.hostent*, %struct.hostent** %12
%68 = getelementptr inbounds %struct.hostent, %struct.hostent* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @bcopy(i8* %63, i8* %66, i32 %69)
%71 = load i32, i32* @PF_INET
%72 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %71, i32* %72
%73 = load i32, i32* %9
%74 = call i32 @htons(i32 %73)
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %74, i32* %75
%76 = load i32, i32* %5
%77 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%78 = load i32, i32* %11
%79 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%80 = call i32 @sendto(i32 %76, i8* %77, i32 %78, i32 0, %struct.sockaddr* %79, i32 12)
store i32 %80, i32* %13
%81 = load i32, i32* %13
%82 = load i32, i32* %11
%83 = icmp ne i32 %81, %82
br i1 %83, label %84, label %87
84: ; preds = %53
%85 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%86 = call i32 @exit(i32 3)
unreachable
87: ; preds = %53
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.TYPE_4__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Packet size has to be >= 0 and <= %d\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [4 x i8] c"udp\00"
@.str.2 = [14 x i8] c"socket failed\00"
@.str.3 = [19 x i8] c"socket successful\0A\00"
@.str.4 = [7 x i8] c"sendto\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = alloca [4096 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 3
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @usage(i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strcpy(i8* %7, i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
%21 = icmp ugt i32 %20, 4096
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 4096)
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %14
%27 = load i32, i32* @PF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = call %struct.TYPE_4__* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %29, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %28, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 20)
unreachable
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%40 = call %struct.hostent* @gethostbyname(i8* %7)
%41 = call i32 @bzero(i8* %6, i32 12)
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i64 0, i32 1
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to i8*
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
%46 = bitcast i32* %45 to i8*
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i64 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @PF_INET
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %49, i32* %50
%51 = call i32 @htons(i32 2000)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %51, i32* %52
%53 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%54 = call i32 @sendto(i32 %32, i8* %8, i32 %20, i32 0, %struct.sockaddr* %53, i32 12)
%55 = icmp eq i32 %54, %20
br i1 %55, label %59, label %56
56: ; preds = %37
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 3)
unreachable
59: ; preds = %37
ret i32 0
}