#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 ai_socktype;
  int ai_protocol;
  scalar_t__ ai_flags;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IPPROTO_TCP;
int IPPROTO_UDP;
int SOCK_DGRAM;
int SOCK_STREAM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
struct addrinfo *lookup(const char *proto, const char *host, const char *port) {
  struct addrinfo hints, *r;
  int err, socktype, protocol;
  if (proto == NULL) {
    fprintf(stderr, "protocol is not set\n");
    return NULL;
  }
  if (strcmp(proto, "tcp") == 0) {
    socktype = SOCK_STREAM;
    protocol = IPPROTO_TCP;
  } else if (strcmp(proto, "udp") == 0) {
    socktype = SOCK_DGRAM;
    protocol = IPPROTO_UDP;
  } else {
    fprintf(stderr, "protocol '%s' invalid\n", proto);
    return NULL;
  }
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET;
  hints.ai_socktype = socktype;
  hints.ai_flags = 0;
  hints.ai_protocol = protocol;
  err = getaddrinfo(host, port, &hints, &r);
  if (err != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
    return NULL;
  }
  return r;
}


x86 assembly (clang, -O0):
<lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <lookup+0x43>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bd <lookup+0x16d>
mov    -0x10(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11ca <lookup+0x7a>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    122e <lookup+0xde>
mov    -0x10(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1201 <lookup+0xb1>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    1229 <lookup+0xd9>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bd <lookup+0x16d>
jmp    122e <lookup+0xde>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     12b5 <lookup+0x165>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12bd <lookup+0x16d>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
test   %rdi,%rdi
je     11aa <lookup+0x5a>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xea4(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c9 <lookup+0x79>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11d9 <lookup+0x89>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
jmp    1251 <lookup+0x101>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    125e <lookup+0x10e>
mov    0x2e10(%rip),%rax
mov    0x2de9(%rip),%rcx
jmp    11e7 <lookup+0x97>
mov    0x2dd0(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rax),%ebp
mov    (%rcx),%ebx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebp,0xc(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     125a <lookup+0x10a>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdeb(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    125e <lookup+0x10e>
mov    (%rsp),%r14
mov    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, i32 }
@stderr = global i32 0
@.str = [21 x i8] c"protocol is not set\0A\00"
@.str.1 = [4 x i8] c"tcp\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.3 = [23 x i8] c"protocol '%s' invalid\0A\00"
@AF_INET = global i32 0
@.str.4 = [17 x i8] c"getaddrinfo: %s\0A\00"
define %struct.addrinfo* @lookup(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store %struct.addrinfo* null, %struct.addrinfo** %4
br label %58
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @SOCK_STREAM
store i32 %23, i32* %11
%24 = load i32, i32* @IPPROTO_TCP
store i32 %24, i32* %12
br label %37
25: ; preds = %18
%26 = load i8*, i8** %5
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @SOCK_DGRAM
store i32 %30, i32* %11
%31 = load i32, i32* @IPPROTO_UDP
store i32 %31, i32* %12
br label %36
32: ; preds = %25
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %5
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %34)
store %struct.addrinfo* null, %struct.addrinfo** %4
br label %58
36: ; preds = %29
br label %37
37: ; preds = %36, %22
%38 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %39, i32* %40
%41 = load i32, i32* %11
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i64 0, i64* %43
%44 = load i32, i32* %12
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %44, i32* %45
%46 = load i8*, i8** %6
%47 = load i8*, i8** %7
%48 = call i32 @getaddrinfo(i8* %46, i8* %47, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %48, i32* %10
%49 = load i32, i32* %10
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %37
%52 = load i32, i32* @stderr
%53 = load i32, i32* %10
%54 = call i8* @gai_strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %54)
store %struct.addrinfo* null, %struct.addrinfo** %4
br label %58
56: ; preds = %37
%57 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %57, %struct.addrinfo** %4
br label %58
58: ; preds = %56, %51, %32, %15
%59 = load %struct.addrinfo*, %struct.addrinfo** %4
ret %struct.addrinfo* %59
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32 }
@stderr = global i32 0
@.str = [21 x i8] c"protocol is not set\0A\00"
@.str.1 = [4 x i8] c"tcp\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [4 x i8] c"udp\00"
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str.3 = [23 x i8] c"protocol '%s' invalid\0A\00"
@AF_INET = global i32 0
@.str.4 = [17 x i8] c"getaddrinfo: %s\0A\00"
define %struct.addrinfo* @lookup(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = icmp eq i8* %0, null
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %40
12: ; preds = %3
%13 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %40
21: ; preds = %15, %12
%22 = phi i32* [ @SOCK_STREAM, %12 ], [ @SOCK_DGRAM, %15 ]
%23 = phi i32* [ @IPPROTO_TCP, %12 ], [ @IPPROTO_UDP, %15 ]
%24 = load i32, i32* %23
%25 = load i32, i32* %22
%26 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %25, i32* %29
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %24, i32* %31
%32 = call i32 @getaddrinfo(i8* %1, i8* %2, %struct.addrinfo* %4, %struct.addrinfo** %5)
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %21
%35 = load i32, i32* @stderr
%36 = call i8* @gai_strerror(i32 %32)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %40
38: ; preds = %21
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %40
40: ; preds = %38, %34, %18, %9
%41 = phi %struct.addrinfo* [ null, %9 ], [ null, %34 ], [ %39, %38 ], [ null, %18 ]
ret %struct.addrinfo* %41
}