#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_next;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ do_ping(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s hostname/ip\n", argv[0]);
    return 0;
  }
  struct addrinfo *res;
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_protocol = IPPROTO_UDP;
  int s;
  if ((s = getaddrinfo(argv[1], "12000", &hints, &res)) != 0) {
    fprintf(stderr, "Couldn't locate PING server: %s\n", gai_strerror(s));
    exit(EXIT_FAILURE);
  }
  if (res == NULL) {
    fprintf(stderr, "Couldn't find PING service on the target host.\n");
    freeaddrinfo(res);
    exit(EXIT_FAILURE);
  }
  if (res->ai_next != NULL) {
    fprintf(stderr,
            "Oops! Multiple PING services found on the targest host?!\n");
    freeaddrinfo(res);
    exit(EXIT_FAILURE);
  }
  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (sock < 0) {
    perror("Unable to create socket");
    freeaddrinfo(res);
    exit(EXIT_FAILURE);
  }
  do_ping(argv[1], sock, res->ai_addr, res->ai_addrlen);
  freeaddrinfo(res);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11da <main+0x4a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x1cf>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfa(%rip),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     126c <main+0xdc>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12a5 <main+0x115>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     12e2 <main+0x152>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    0x2cbd(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1332 <main+0x1a2>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x40(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
call   1040 <do_ping@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11c4 <main+0x34>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe48(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1264 <main+0xd4>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e09(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x28(%rsp)
mov    0x2df3(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x24(%rsp)
mov    0x2dbc(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x20(%rsp)
mov    0x8(%rbx),%rdi
lea    0xe08(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1274 <main+0xe4>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     1299 <main+0x109>
cmpq   $0x0,0x8(%rax)
jne    12ab <main+0x11b>
mov    (%r15),%edi
mov    (%r12),%esi
mov    0x0(%r13),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12c4 <main+0x134>
mov    0x8(%rbx),%rdi
mov    0x8(%rsp),%rdx
mov    (%rdx),%ecx
mov    0x4(%rdx),%edx
mov    %eax,%esi
call   1040 <do_ping@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d55(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xd92(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12da <main+0x14a>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
jmp    12bb <main+0x12b>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12d0 <main+0x140>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s hostname/ip\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [6 x i8] c"12000\00"
@.str.2 = [33 x i8] c"Couldn't locate PING server: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [48 x i8] c"Couldn't find PING service on the target host.\0A\00"
@.str.4 = [58 x i8] c"Oops! Multiple PING services found on the targest host?!\0A\00"
@.str.5 = [24 x i8] c"Unable to create socket\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo*
%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 slt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 0, i32* %3
br label %87
18: ; preds = %2
%19 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 32)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 5
store i32 %20, i32* %21
%22 = load i32, i32* @SOCK_DGRAM
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 4
store i32 %22, i32* %23
%24 = load i32, i32* @IPPROTO_UDP
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %24, i32* %25
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @getaddrinfo(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %7, %struct.addrinfo** %6)
store i32 %29, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %18
%32 = load i32, i32* @stderr
%33 = load i32, i32* %8
%34 = call i8* @gai_strerror(i32 %33)
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %18
%39 = load %struct.addrinfo*, %struct.addrinfo** %6
%40 = icmp eq %struct.addrinfo* %39, null
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%44 = load %struct.addrinfo*, %struct.addrinfo** %6
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %38
%49 = load %struct.addrinfo*, %struct.addrinfo** %6
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 2
%51 = load i32*, i32** %50
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %60
53: ; preds = %48
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%56 = load %struct.addrinfo*, %struct.addrinfo** %6
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %48
%61 = load i32, i32* @AF_INET
%62 = load i32, i32* @SOCK_DGRAM
%63 = load i32, i32* @IPPROTO_UDP
%64 = call i32 @socket(i32 %61, i32 %62, i32 %63)
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %60
%68 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%69 = load %struct.addrinfo*, %struct.addrinfo** %6
%70 = call i32 @freeaddrinfo(%struct.addrinfo* %69)
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %60
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = load i32, i32* %9
%78 = load %struct.addrinfo*, %struct.addrinfo** %6
%79 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %78, i32 0, i32 1
%80 = load i32, i32* %79
%81 = load %struct.addrinfo*, %struct.addrinfo** %6
%82 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = call i32 @do_ping(i8* %76, i32 %77, i32 %80, i32 %83)
%85 = load %struct.addrinfo*, %struct.addrinfo** %6
%86 = call i32 @freeaddrinfo(%struct.addrinfo* %85)
store i32 0, i32* %3
br label %87
87: ; preds = %73, %12
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, i32 }
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s hostname/ip\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.1 = [6 x i8] c"12000\00"
@.str.2 = [33 x i8] c"Couldn't locate PING server: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [48 x i8] c"Couldn't find PING service on the target host.\0A\00"
@.str.4 = [58 x i8] c"Oops! Multiple PING services found on the targest host?!\0A\00"
@.str.5 = [24 x i8] c"Unable to create socket\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %73
10: ; preds = %2
%11 = bitcast %struct.addrinfo** %3 to i8*
%12 = bitcast %struct.addrinfo* %4 to i8*
%13 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_DGRAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @IPPROTO_UDP
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %18, i32* %19
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @getaddrinfo(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %10
%25 = load i32, i32* @stderr
%26 = call i8* @gai_strerror(i32 %22)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %10
%31 = load %struct.addrinfo*, %struct.addrinfo** %3
%32 = icmp eq %struct.addrinfo* %31, null
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
%36 = load %struct.addrinfo*, %struct.addrinfo** %3
%37 = call i32 @freeaddrinfo(%struct.addrinfo* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %30
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 2
%42 = load i32*, i32** %41
%43 = icmp eq i32* %42, null
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%47 = load %struct.addrinfo*, %struct.addrinfo** %3
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %40
%52 = load i32, i32* @AF_INET
%53 = load i32, i32* @SOCK_DGRAM
%54 = load i32, i32* @IPPROTO_UDP
%55 = call i32 @socket(i32 %52, i32 %53, i32 %54)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%59 = load %struct.addrinfo*, %struct.addrinfo** %3
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %51
%64 = load i8*, i8** %20
%65 = load %struct.addrinfo*, %struct.addrinfo** %3
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i64 0, i32 1
%67 = load i32, i32* %66
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i64 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @do_ping(i8* %64, i32 %55, i32 %67, i32 %69)
%71 = load %struct.addrinfo*, %struct.addrinfo** %3
%72 = call i32 @freeaddrinfo(%struct.addrinfo* %71)
br label %73
73: ; preds = %63, %6
ret i32 0
}