#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*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ lerror(int, char *);
int /*<<< orphan*/ lprintf(int, char *, ...);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
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) {
    lprintf(2, "Error: %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) {
      lerror(2, "socket");
      continue;
    }
    if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) {
      break;
    } else {
      lerror(2, "bind");
      lprintf(2, "Tried address: %s, service: ",
              inet_ntoa(((const struct sockaddr_in *)(rp->ai_addr))->sin_addr),
              servname);
    }
    close(sfd);
  }
  freeaddrinfo(result);
  if (rp == NULL) {
    lprintf(2, "Error: Could not bind");
    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   1060 <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    0x2df1(%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   1090 <getaddrinfo@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     124f <create_socket_stream+0xaf>
mov    -0x68(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   10a0 <lprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <create_socket_stream+0x1af>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1316 <create_socket_stream+0x176>
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    12a0 <create_socket_stream+0x100>
mov    $0x2,%edi
lea    0xd75(%rip),%rsi
call   1030 <lerror@plt>
jmp    1305 <create_socket_stream+0x165>
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    12c5 <create_socket_stream+0x125>
jmp    1316 <create_socket_stream+0x176>
mov    $0x2,%edi
lea    0xd6f(%rip),%rsi
call   1030 <lerror@plt>
mov    -0x60(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x2,%edi
lea    0xd1c(%rip),%rsi
mov    $0x0,%al
call   10a0 <lprintf@plt>
mov    -0x64(%rbp),%edi
call   1070 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1257 <create_socket_stream+0xb7>
mov    -0x58(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
cmpq   $0x0,-0x60(%rbp)
jne    1349 <create_socket_stream+0x1a9>
mov    $0x2,%edi
lea    0xcf9(%rip),%rsi
mov    $0x0,%al
call   10a0 <lprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134f <create_socket_stream+0x1af>
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),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x38,%edx
call   1060 <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    0x2de7(%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    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1241 <create_socket_stream+0xa1>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdd2(%rip),%rsi
mov    $0x2,%edi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <lprintf@plt>
jmp    1303 <create_socket_stream+0x163>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12e2 <create_socket_stream+0x142>
lea    0xdb5(%rip),%r14
lea    0xde3(%rip),%r15
lea    0xdae(%rip),%r12
jmp    1286 <create_socket_stream+0xe6>
cs nopw 0x0(%rax,%rax,1)
mov    $0x2,%edi
mov    %r14,%rsi
call   1030 <lerror@plt>
mov    0x20(%rbx),%rbx
test   %rbx,%rbx
je     12e6 <create_socket_stream+0x146>
mov    0x1c(%rbx),%edi
mov    0x18(%rbx),%esi
mov    0x10(%rbx),%rdx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1270 <create_socket_stream+0xd0>
mov    %eax,%ebp
mov    (%rbx),%rsi
mov    0x8(%rbx),%edx
mov    %eax,%edi
call   1080 <bind@plt>
test   %rax,%rax
je     1319 <create_socket_stream+0x179>
mov    $0x2,%edi
mov    %r15,%rsi
call   1030 <lerror@plt>
mov    (%rbx),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    $0x2,%edi
mov    %r12,%rsi
mov    %eax,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   10a0 <lprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    127d <create_socket_stream+0xdd>
xor    %edi,%edi
jmp    12eb <create_socket_stream+0x14b>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
lea    0xd38(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   10a0 <lprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
jmp    1308 <create_socket_stream+0x168>

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
@.str = [11 x i8] c"Error: %s\0A\00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [29 x i8] c"Tried address: %s, service: \00"
@.str.4 = [22 x i8] c"Error: Could not bind\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 %31
27: ; preds = %2
%28 = load i32, i32* %10
%29 = call i32 @gai_strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @lprintf(i32 2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 -1, i32* %3
br label %89
31: ; preds = %2
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %32, %struct.addrinfo** %8
br label %33
33: ; preds = %76, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %80
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %8
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 4
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %8
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %8
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i64, i64* %44
%46 = call i32 @socket(i32 %39, i32 %42, i64 %45)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %36
%50 = call i32 @lerror(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %76
51: ; preds = %36
%52 = load i32, i32* %9
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 0
%55 = load i32*, i32** %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %8
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 1
%58 = load i32, i32* %57
%59 = call i64 @bind(i32 %52, i32* %55, i32 %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %51
br label %80
62: ; preds = %51
%63 = call i32 @lerror(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%64 = load %struct.addrinfo*, %struct.addrinfo** %8
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 0
%66 = load i32*, i32** %65
%67 = bitcast i32* %66 to %struct.sockaddr_in*
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @inet_ntoa(i32 %69)
%71 = load i8*, i8** %5
%72 = call i32 (i32, i8*, ...) @lprintf(i32 2, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %70, i8* %71)
br label %73
73: ; preds = %62
%74 = load i32, i32* %9
%75 = call i32 @close(i32 %74)
br label %76
76: ; preds = %73, %49
%77 = load %struct.addrinfo*, %struct.addrinfo** %8
%78 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %77, i32 0, i32 5
%79 = load %struct.addrinfo*, %struct.addrinfo** %78
store %struct.addrinfo* %79, %struct.addrinfo** %8
br label %33
80: ; preds = %61, %33
%81 = load %struct.addrinfo*, %struct.addrinfo** %7
%82 = call i32 @freeaddrinfo(%struct.addrinfo* %81)
%83 = load %struct.addrinfo*, %struct.addrinfo** %8
%84 = icmp eq %struct.addrinfo* %83, null
br i1 %84, label %85, label %87
85: ; preds = %80
%86 = call i32 (i32, i8*, ...) @lprintf(i32 2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %89
87: ; preds = %80
%88 = load i32, i32* %9
store i32 %88, i32* %3
br label %89
89: ; preds = %87, %85, %27
%90 = load i32, i32* %3
ret i32 %90
}

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
@.str = [11 x i8] c"Error: %s\0A\00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [29 x i8] c"Tried address: %s, service: \00"
@.str.4 = [22 x i8] c"Error: Could not bind\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 %63, label %26
23: ; preds = %2
%24 = call i32 @gai_strerror(i32 %18)
%25 = call i32 (i32, i8*, ...) @lprintf(i32 2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %67
26: ; preds = %20, %54
%27 = phi %struct.addrinfo* [ %56, %54 ], [ %21, %20 ]
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 3
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 2
%33 = load i64, i64* %32
%34 = call i32 @socket(i32 %29, i32 %31, i64 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @lerror(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %54
38: ; preds = %26
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%40 = load i32*, i32** %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%42 = load i32, i32* %41
%43 = call i64 @bind(i32 %34, i32* %40, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %58, label %45
45: ; preds = %38
%46 = call i32 @lerror(i32 2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%47 = bitcast %struct.addrinfo* %27 to %struct.sockaddr_in**
%48 = load %struct.sockaddr_in*, %struct.sockaddr_in** %47
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %48, i64 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @inet_ntoa(i32 %50)
%52 = call i32 (i32, i8*, ...) @lprintf(i32 2, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %51, i8* %1)
%53 = call i32 @close(i32 %34)
br label %54
54: ; preds = %45, %36
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 5
%56 = load %struct.addrinfo*, %struct.addrinfo** %55
%57 = icmp eq %struct.addrinfo* %56, null
br i1 %57, label %61, label %26
58: ; preds = %38
%59 = load %struct.addrinfo*, %struct.addrinfo** %4
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
br label %67
61: ; preds = %54
%62 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %63
63: ; preds = %61, %20
%64 = phi %struct.addrinfo* [ %62, %61 ], [ null, %20 ]
%65 = call i32 @freeaddrinfo(%struct.addrinfo* %64)
%66 = call i32 (i32, i8*, ...) @lprintf(i32 2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %58, %63, %23
%68 = phi i32 [ -1, %23 ], [ -1, %63 ], [ %34, %58 ]
ret i32 %68
}