#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  int /*<<< orphan*/ *ai_addr;
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ *ai_canonname;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ stderr;
int create_socket_stream(const char *hostname, const char *servname) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int sfd, s;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  s = getaddrinfo(hostname, servname, &hints, &result);
  if (s != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1) {
      perror("socket");
      continue;
    }
    if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) {
      break;
    } else {
      perror("bind");
      fprintf(stderr, "Tried address: %s, service: %s\n",
              inet_ntoa(((const struct sockaddr_in *)(rp->ai_addr))->sin_addr),
              servname);
    }
    close(sfd);
  }
  freeaddrinfo(result);
  if (rp == NULL) {
    fprintf(stderr, "Error: Could not bind\n");
    return -1;
  }
  return sfd;
}


x86 assembly (clang, -O0):
<create_socket_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1050 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     125a <create_socket_stream+0xba>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x68(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <create_socket_stream+0x1bf>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1322 <create_socket_stream+0x182>
mov    -0x60(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%rdx
call   10c0 <socket@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    12a6 <create_socket_stream+0x106>
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
jmp    1311 <create_socket_stream+0x171>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    (%rax),%rsi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jne    12cb <create_socket_stream+0x12b>
jmp    1322 <create_socket_stream+0x182>
lea    0xd46(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x60(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
mov    -0x18(%rbp),%rcx
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x64(%rbp),%edi
call   1060 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1262 <create_socket_stream+0xc2>
mov    -0x58(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
cmpq   $0x0,-0x60(%rbp)
jne    1359 <create_socket_stream+0x1b9>
mov    0x2c93(%rip),%rax
mov    (%rax),%edi
lea    0xcf7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <create_socket_stream+0x1bf>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket_stream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1050 <memset@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0x0,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     124d <create_socket_stream+0xad>
mov    0x2da9(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdc9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12e9 <create_socket_stream+0x149>
mov    0x8(%rsp),%r14
test   %r14,%r14
je     12fa <create_socket_stream+0x15a>
mov    0x2d6e(%rip),%r15
lea    0xdb4(%rip),%r12
mov    0x1c(%r14),%edi
mov    0x18(%r14),%esi
mov    0x10(%r14),%rdx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12d1 <create_socket_stream+0x131>
mov    %eax,%ebp
mov    (%r14),%rsi
mov    0x8(%r14),%edx
mov    %eax,%edi
call   1080 <bind@plt>
test   %rax,%rax
je     12df <create_socket_stream+0x13f>
lea    0xd7d(%rip),%rdi
call   1090 <perror@plt>
mov    (%r15),%ebx
mov    (%r14),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %rax,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x20(%r14),%r14
test   %r14,%r14
jne    1269 <create_socket_stream+0xc9>
jmp    131d <create_socket_stream+0x17d>
lea    0xd39(%rip),%rdi
call   1090 <perror@plt>
jmp    12c6 <create_socket_stream+0x126>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <freeaddrinfo@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xd2c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1243 <create_socket_stream+0xa3>
mov    0x8(%rsp),%rdi
jmp    12fc <create_socket_stream+0x15c>

LLVM IR (-O0):
%struct.addrinfo = type { i32*, i32, i64, i32, i32, %struct.addrinfo*, i32*, i32 }
%struct.sockaddr_in = type { i32 }
@AI_PASSIVE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [32 x i8] c"Tried address: %s, service: %s\0A\00"
@.str.4 = [23 x i8] c"Error: Could not bind\0A\00"
define i32 @create_socket_stream(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 56)
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 7
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %14, i32* %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i32* null, i32** %19
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i32* null, i32** %20
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %21
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = call i32 @getaddrinfo(i8* %22, i8* %23, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %2
%28 = load i32, i32* @stderr
%29 = load i32, i32* %10
%30 = call i8* @gai_strerror(i32 %29)
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
br label %92
32: ; preds = %2
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %33, %struct.addrinfo** %8
br label %34
34: ; preds = %78, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %82
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 4
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %8
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 2
%46 = load i64, i64* %45
%47 = call i32 @socket(i32 %40, i32 %43, i64 %46)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %37
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %78
52: ; preds = %37
%53 = load i32, i32* %9
%54 = load %struct.addrinfo*, %struct.addrinfo** %8
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32*, i32** %55
%57 = load %struct.addrinfo*, %struct.addrinfo** %8
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = call i64 @bind(i32 %53, i32* %56, i32 %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %52
br label %82
63: ; preds = %52
%64 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32, i32* @stderr
%66 = load %struct.addrinfo*, %struct.addrinfo** %8
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 0
%68 = load i32*, i32** %67
%69 = bitcast i32* %68 to %struct.sockaddr_in*
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i8* @inet_ntoa(i32 %71)
%73 = load i8*, i8** %5
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %72, i8* %73)
br label %75
75: ; preds = %63
%76 = load i32, i32* %9
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %50
%79 = load %struct.addrinfo*, %struct.addrinfo** %8
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %79, i32 0, i32 5
%81 = load %struct.addrinfo*, %struct.addrinfo** %80
store %struct.addrinfo* %81, %struct.addrinfo** %8
br label %34
82: ; preds = %62, %34
%83 = load %struct.addrinfo*, %struct.addrinfo** %7
%84 = call i32 @freeaddrinfo(%struct.addrinfo* %83)
%85 = load %struct.addrinfo*, %struct.addrinfo** %8
%86 = icmp eq %struct.addrinfo* %85, null
br i1 %86, label %87, label %90
87: ; preds = %82
%88 = load i32, i32* @stderr
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
90: ; preds = %82
%91 = load i32, i32* %9
store i32 %91, i32* %3
br label %92
92: ; preds = %90, %87, %27
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
%struct.addrinfo = type { i32*, i32, i64, i32, i32, %struct.addrinfo*, i32*, i32 }
%struct.sockaddr_in = type { i32 }
@AI_PASSIVE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [32 x i8] c"Tried address: %s, service: %s\0A\00"
@.str.4 = [23 x i8] c"Error: Could not bind\0A\00"
define i32 @create_socket_stream(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 56)
%8 = load i32, i32* @AI_PASSIVE
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 7
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32* null, i32** %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
%17 = bitcast %struct.addrinfo** %16 to i8*
%18 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %65, label %27
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = call i8* @gai_strerror(i32 %18)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %70
27: ; preds = %20, %56
%28 = phi %struct.addrinfo* [ %58, %56 ], [ %21, %20 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i64, i64* %33
%35 = call i32 @socket(i32 %30, i32 %32, i64 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %56
39: ; preds = %27
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%41 = load i32*, i32** %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%43 = load i32, i32* %42
%44 = call i64 @bind(i32 %35, i32* %41, i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %60, label %46
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @stderr
%49 = bitcast %struct.addrinfo* %28 to %struct.sockaddr_in**
%50 = load %struct.sockaddr_in*, %struct.sockaddr_in** %49
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %50, i64 0, i32 0
%52 = load i32, i32* %51
%53 = call i8* @inet_ntoa(i32 %52)
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %53, i8* %1)
%55 = call i32 @close(i32 %35)
br label %56
56: ; preds = %46, %37
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%58 = load %struct.addrinfo*, %struct.addrinfo** %57
%59 = icmp eq %struct.addrinfo* %58, null
br i1 %59, label %63, label %27
60: ; preds = %39
%61 = load %struct.addrinfo*, %struct.addrinfo** %4
%62 = call i32 @freeaddrinfo(%struct.addrinfo* %61)
br label %70
63: ; preds = %56
%64 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %65
65: ; preds = %63, %20
%66 = phi %struct.addrinfo* [ %64, %63 ], [ null, %20 ]
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %60, %65, %23
%71 = phi i32 [ -1, %23 ], [ -1, %65 ], [ %35, %60 ]
ret i32 %71
}