#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ servinfo;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ memset(struct addrinfo **, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
struct addrinfo *prepare_addrinfo(char const *, char const *,
                                  int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recv_print_udp(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char const *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "Usage: showip <hostname> <port>\n");
    return 1;
  }
  int recvfd;
  struct addrinfo *servinfo;
  int sockfd;
  int recv_npackets;
  memset(&servinfo, 0, sizeof(servinfo));
  servinfo = prepare_addrinfo(argv[1], argv[2], AF_INET, SOCK_DGRAM);
  sockfd =
      socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
  if (sockfd == -1) {
    perror("\tsocket");
    return 1;
  }
  printf("\tSocket created.\n");
  if (bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
    perror("\tbind");
    return 2;
  }
  printf("\tBind successful.\n");
  freeaddrinfo(servinfo);
  printf("\tWaiting for incoming packets...\n");
  recv_npackets = 0;
  while (1) {
    recv_print_udp(sockfd);
    printf("\t[info]\tReceived %d of %d packets\n", ++recv_npackets, 5);
    if (recv_npackets == 5)
      break;
  }
  printf("\tConnection closed.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d1 <main+0x41>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1308 <main+0x178>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
call   1060 <prepare_addrinfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    124b <main+0xbb>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1308 <main+0x178>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    128f <main+0xff>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1308 <main+0x178>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <recv_print_udp@plt>
mov    -0x28(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
lea    0xda3(%rip),%rdi
mov    $0x5,%edx
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x5,-0x28(%rbp)
jne    12ee <main+0x15e>
jmp    12f3 <main+0x163>
jmp    12bb <main+0x12b>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    12e8 <main+0x148>
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
call   1070 <prepare_addrinfo@plt>
mov    %rax,0x8(%rsp)
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     130d <main+0x16d>
mov    %eax,%ebx
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     131b <main+0x17b>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1090 <recv_print_udp@plt>
lea    0xdd7(%rip),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1090 <recv_print_udp@plt>
mov    %r14,%rdi
mov    $0x2,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1090 <recv_print_udp@plt>
mov    %r14,%rdi
mov    $0x3,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1090 <recv_print_udp@plt>
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1090 <recv_print_udp@plt>
mov    %r14,%rdi
mov    $0x5,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdb5(%rip),%rdi
call   1030 <puts@plt>
jmp    1302 <main+0x162>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd08(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd0d(%rip),%rdi
call   10a0 <perror@plt>
jmp    12fd <main+0x15d>
lea    0xd07(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%ebp
jmp    1302 <main+0x162>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Usage: showip <hostname> <port>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [8 x i8] c"\09socket\00"
@.str.2 = [18 x i8] c"\09Socket created.\0A\00"
@.str.3 = [6 x i8] c"\09bind\00"
@.str.4 = [19 x i8] c"\09Bind successful.\0A\00"
@.str.5 = [34 x i8] c"\09Waiting for incoming packets...\0A\00"
@.str.6 = [35 x i8] c"\09[info]\09Received %d of %d packets\0A\00"
@.str.7 = [21 x i8] c"\09Connection closed.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %70
15: ; preds = %2
%16 = call i32 @memset(%struct.addrinfo** %7, i32 0, i32 8)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_DGRAM
%25 = call %struct.addrinfo* @prepare_addrinfo(i8* %19, i8* %22, i32 %23, i32 %24)
store %struct.addrinfo* %25, %struct.addrinfo** %7
%26 = load %struct.addrinfo*, %struct.addrinfo** %7
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 4
%28 = load i32, i32* %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 3
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %28, i32 %31, i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %15
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %70
40: ; preds = %15
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* %8
%43 = load %struct.addrinfo*, %struct.addrinfo** %7
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %7
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @bind(i32 %42, i32 %45, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %40
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
br label %70
53: ; preds = %40
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%55 = load %struct.addrinfo*, %struct.addrinfo** %7
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %9
br label %58
58: ; preds = %53, %67
%59 = load i32, i32* %8
%60 = call i32 @recv_print_udp(i32 %59)
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %62, i32 5)
%64 = load i32, i32* %9
%65 = icmp eq i32 %64, 5
br i1 %65, label %66, label %67
66: ; preds = %58
br label %68
67: ; preds = %58
br label %58
68: ; preds = %66
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %70
70: ; preds = %68, %51, %38, %12
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [33 x i8] c"Usage: showip <hostname> <port>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [8 x i8] c"\09socket\00"
@.str.3 = [6 x i8] c"\09bind\00"
@.str.6 = [35 x i8] c"\09[info]\09Received %d of %d packets\0A\00"
@str = [17 x i8] c"\09Socket created.\00"
@str.8 = [18 x i8] c"\09Bind successful.\00"
@str.9 = [33 x i8] c"\09Waiting for incoming packets...\00"
@str.10 = [20 x i8] c"\09Connection closed.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %57
8: ; preds = %2
%9 = bitcast %struct.addrinfo** %3 to i8*
%10 = call i32 @memset(%struct.addrinfo** %3, i32 0, i32 8)
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call %struct.addrinfo* @prepare_addrinfo(i8* %12, i8* %14, i32 %15, i32 %16)
store %struct.addrinfo* %17, %struct.addrinfo** %3
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 4
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 3
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i64 0, i32 2
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %19, i32 %21, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %8
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %55
28: ; preds = %8
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%30 = load %struct.addrinfo*, %struct.addrinfo** %3
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @bind(i32 %24, i32 %32, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %55
39: ; preds = %28
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%41 = load %struct.addrinfo*, %struct.addrinfo** %3
%42 = call i32 @freeaddrinfo(%struct.addrinfo* %41)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
%44 = call i32 @recv_print_udp(i32 %24)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 5)
%46 = call i32 @recv_print_udp(i32 %24)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 2, i32 5)
%48 = call i32 @recv_print_udp(i32 %24)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 3, i32 5)
%50 = call i32 @recv_print_udp(i32 %24)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 4, i32 5)
%52 = call i32 @recv_print_udp(i32 %24)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 5, i32 5)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
br label %55
55: ; preds = %39, %37, %26
%56 = phi i32 [ 1, %26 ], [ 2, %37 ], [ 0, %39 ]
br label %57
57: ; preds = %55, %5
%58 = phi i32 [ 1, %5 ], [ %56, %55 ]
ret i32 %58
}