#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ MSG_WAITALL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_packet(struct sockaddr_in *, int, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ strtol(char *, char **, int);
int /*<<< orphan*/ usage(char **);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    usage(argv);
  }
  const char *ip = argv[1];
  uint16_t port = strtol(argv[2], (char **)NULL, 10);
  struct sockaddr_in name;
  socklen_t namelen = sizeof(struct sockaddr_in);
  bzero(&name, namelen);
  if (inet_aton(ip, &name.sin_addr) != 1) {
    usage(argv);
  }
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  int sock;
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    err(1, "socket");
  }
  char buf[BUFSIZ];
  strncpy(buf, "some payload", BUFSIZ);
  ssize_t buf_len = strlen(buf);
  ssize_t nsent;
  if ((nsent = sendto(sock, buf, buf_len, 0, (const struct sockaddr *)&name,
                      namelen)) == -1) {
    err(1, "sendto");
  }
  log_packet(&name, 0, nsent);
  while (1) {
    ssize_t nread;
    if ((nread = recvfrom(sock, buf, BUFSIZ - 1, MSG_WAITALL,
                          (struct sockaddr *)&name, &namelen)) == -1) {
      err(1, "recvfrom");
    }
    assert(nread > 0);
    assert(namelen = sizeof(struct sockaddr_in));
    log_packet(&name, 1, nread);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11f9 <main+0x29>
mov    -0x10(%rbp),%rdi
call   10c0 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xc,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdi
call   10d0 <bzero@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x8,%rsi
call   1090 <inet_aton@plt>
cmp    $0x1,%eax
je     1254 <main+0x84>
mov    -0x10(%rbp),%rdi
call   10c0 <usage@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jge    12a8 <main+0xd8>
mov    $0x1,%edi
lea    0xd5d(%rip),%rsi
call   10a0 <err@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
lea    0xd23(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rsi
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x44(%rbp),%edx
lea    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0x48(%rbp)
cmp    $0xffffffff,%eax
jne    132b <main+0x15b>
mov    $0x1,%edi
lea    0xcee(%rip),%rsi
call   10a0 <err@plt>
mov    -0x48(%rbp),%edx
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
call   10e0 <log_packet@plt>
jmp    133e <main+0x16e>
mov    -0x58(%rbp),%rsi
mov    -0x30(%rbp),%edi
mov    0x2c84(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
mov    0x2c68(%rip),%rax
mov    (%rax),%ecx
lea    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
jne    1384 <main+0x1b4>
mov    $0x1,%edi
lea    0xc9c(%rip),%rsi
call   10a0 <err@plt>
cmpl   $0x0,-0x4c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0xc,-0x2c(%rbp)
mov    $0xc,%edi
call   1070 <assert@plt>
mov    -0x4c(%rbp),%edx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   10e0 <log_packet@plt>
jmp    1339 <main+0x169>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     11f1 <main+0x21>
mov    %rbx,%rdi
call   10c0 <usage@plt>
mov    0x8(%rbx),%r15
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,%r14d
movl   $0xc,-0x2c(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
lea    -0x30(%rbp),%rsi
mov    %r15,%rdi
call   1090 <inet_aton@plt>
cmp    $0x1,%eax
je     1236 <main+0x66>
mov    %rbx,%rdi
call   10c0 <usage@plt>
mov    0x2dab(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,-0x34(%rbp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    (%rbx),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    127e <main+0xae>
lea    0xd8c(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r15
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xd65(%rip),%rsi
mov    %r15,%rdi
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    -0x38(%rbp),%r8
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    12e4 <main+0x114>
lea    0xd3a(%rip),%rsi
mov    $0x1,%edi
call   10a0 <err@plt>
lea    -0x38(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   10e0 <log_packet@plt>
lea    -0x2c(%rbp),%r12
jmp    132f <main+0x15f>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
test   %r13d,%r13d
setg   %dil
call   1070 <assert@plt>
movl   $0xc,-0x2c(%rbp)
mov    $0xc,%edi
call   1070 <assert@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
call   10e0 <log_packet@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edx
add    $0xffffffff,%edx
mov    0x2c7e(%rip),%rax
mov    (%rax),%ecx
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%r8
mov    %r12,%r9
call   10b0 <recvfrom@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1300 <main+0x130>
mov    $0x1,%edi
lea    0xcb2(%rip),%rsi
call   10a0 <err@plt>
jmp    1300 <main+0x130>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@BUFSIZ = global i32 0
@.str.1 = [13 x i8] c"some payload\00"
@.str.2 = [7 x i8] c"sendto\00"
@MSG_WAITALL = global i32 0
@.str.3 = [9 x i8] c"recvfrom\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = call i32 @usage(i8** %19)
br label %21
21: ; preds = %18, %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i8** null, i32 10)
store i32 %28, i32* %7
store i32 12, i32* %9
%29 = load i32, i32* %9
%30 = call i32 @bzero(%struct.sockaddr_in* %8, i32 %29)
%31 = load i8*, i8** %6
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%33 = call i32 @inet_aton(i8* %31, i32* %32)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %38
35: ; preds = %21
%36 = load i8**, i8*** %5
%37 = call i32 @usage(i8** %36)
br label %38
38: ; preds = %35, %21
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %39, i32* %40
%41 = load i32, i32* %7
%42 = call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* @AF_INET
%45 = load i32, i32* @SOCK_DGRAM
%46 = load i32, i32* @IPPROTO_UDP
%47 = call i32 @socket(i32 %44, i32 %45, i32 %46)
store i32 %47, i32* %10
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %38
%50 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %51
51: ; preds = %49, %38
%52 = load i32, i32* @BUFSIZ
%53 = zext i32 %52 to i64
%54 = call i8* @llvm.stacksave()
store i8* %54, i8** %11
%55 = alloca i8, i64 %53
store i64 %53, i64* %12
%56 = load i32, i32* @BUFSIZ
%57 = call i32 @strncpy(i8* %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %56)
%58 = call i32 @strlen(i8* %55)
store i32 %58, i32* %13
%59 = load i32, i32* %10
%60 = load i32, i32* %13
%61 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%62 = load i32, i32* %9
%63 = call i32 @sendto(i32 %59, i8* %55, i32 %60, i32 0, %struct.sockaddr* %61, i32 %62)
store i32 %63, i32* %14
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %51
%66 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %51
%68 = load i32, i32* %14
%69 = call i32 @log_packet(%struct.sockaddr_in* %8, i32 0, i32 %68)
br label %70
70: ; preds = %81, %67
br label %71
71: ; preds = %70
%72 = load i32, i32* %10
%73 = load i32, i32* @BUFSIZ
%74 = sub nsw i32 %73, 1
%75 = load i32, i32* @MSG_WAITALL
%76 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%77 = call i32 @recvfrom(i32 %72, i8* %55, i32 %74, i32 %75, %struct.sockaddr* %76, i32* %9)
store i32 %77, i32* %15
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %71
%80 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %81
81: ; preds = %79, %71
%82 = load i32, i32* %15
%83 = icmp sgt i32 %82, 0
%84 = zext i1 %83 to i32
%85 = call i32 @assert(i32 %84)
store i32 12, i32* %9
%86 = call i32 @assert(i32 12)
%87 = load i32, i32* %15
%88 = call i32 @log_packet(%struct.sockaddr_in* %8, i32 1, i32 %87)
br label %70
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@BUFSIZ = global i32 0
@.str.1 = [13 x i8] c"some payload\00"
@.str.2 = [7 x i8] c"sendto\00"
@MSG_WAITALL = global i32 0
@.str.3 = [9 x i8] c"recvfrom\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @usage(i8** %1)
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i8** null, i32 10)
%14 = bitcast %struct.sockaddr_in* %3 to i8*
%15 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%16 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%18 = call i32 @inet_aton(i8* %10, i32* %17)
%19 = icmp eq i32 %18, 1
br i1 %19, label %22, label %20
20: ; preds = %8
%21 = call i32 @usage(i8** %1)
br label %22
22: ; preds = %20, %8
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = call i32 @htons(i32 %13)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_DGRAM
%29 = load i32, i32* @IPPROTO_UDP
%30 = call i32 @socket(i32 %27, i32 %28, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %22
%35 = load i32, i32* @BUFSIZ
%36 = zext i32 %35 to i64
%37 = alloca i8, i64 %36
%38 = call i32 @strncpy(i8* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%39 = call i32 @strlen(i8* %37)
%40 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%41 = call i32 @sendto(i32 %30, i8* %37, i32 %39, i32 0, %struct.sockaddr* %40, i32 12)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %34
%46 = call i32 @log_packet(%struct.sockaddr_in* %3, i32 0, i32 %41)
br label %47
47: ; preds = %55, %45
%48 = load i32, i32* @BUFSIZ
%49 = add nsw i32 %48, -1
%50 = load i32, i32* @MSG_WAITALL
%51 = call i32 @recvfrom(i32 %30, i8* %37, i32 %49, i32 %50, %struct.sockaddr* %40, i32* %4)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = icmp sgt i32 %51, 0
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
store i32 12, i32* %4
%59 = call i32 @assert(i32 12)
%60 = call i32 @log_packet(%struct.sockaddr_in* %3, i32 1, i32 %51)
br label %47
}