#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;
};

/* Variables and functions */
int INET6_ADDRSTRLEN;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int get_port_and_ip(struct addrinfo *, char *);
int get_socket(struct addrinfo *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ stderr;
int bind_socket(struct addrinfo *res) {
  int socketfd, b;
  int port;
  char ip[INET6_ADDRSTRLEN];
  socketfd = get_socket(res);
  if (socketfd != -1) {
    b = bind(socketfd, res->ai_addr, res->ai_addrlen);
    port = get_port_and_ip(res, ip);
    if (b != 0) {
      fprintf(stderr, "ERROR: bind to port %d on %s failed: ", port, ip);
      perror(NULL);
      close(socketfd);
      socketfd = -1;
    } else {
      printf("bind to port %d on %s OK\n", port, ip);
    }
  }
  return socketfd;
}


x86 assembly (clang, -O0):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <get_socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     1241 <bind_socket+0xd1>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <get_port_and_ip@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1229 <bind_socket+0xb9>
mov    -0x30(%rbp),%rcx
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdf9(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    123c <bind_socket+0xcc>
mov    -0x30(%rbp),%rdx
mov    -0x14(%rbp),%esi
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
jmp    1241 <bind_socket+0xd1>
mov    -0xc(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_socket>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1030 <get_socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     11e5 <bind_socket+0x75>
mov    %eax,%r15d
mov    (%r12),%edx
mov    0x4(%r12),%esi
mov    %eax,%edi
call   1080 <bind@plt>
mov    %eax,%r13d
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <get_port_and_ip@plt>
test   %r13d,%r13d
jne    11f7 <bind_socket+0x87>
lea    0xe4e(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
mov    %r15d,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dda(%rip),%rcx
mov    (%rcx),%edi
lea    0xdf9(%rip),%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1090 <perror@plt>
mov    %r15d,%edi
call   1050 <close@plt>
jmp    11e5 <bind_socket+0x75>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"ERROR: bind to port %d on %s failed: \00"
@.str.1 = [26 x i8] c"bind to port %d on %s OK\0A\00"
define i32 @bind_socket(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store %struct.addrinfo* %0, %struct.addrinfo** %2
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %6
%11 = alloca i8, i64 %9
store i64 %9, i64* %7
%12 = load %struct.addrinfo*, %struct.addrinfo** %2
%13 = call i32 @get_socket(%struct.addrinfo* %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %40
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load %struct.addrinfo*, %struct.addrinfo** %2
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.addrinfo*, %struct.addrinfo** %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @bind(i32 %17, i32 %20, i32 %23)
store i32 %24, i32* %4
%25 = load %struct.addrinfo*, %struct.addrinfo** %2
%26 = call i32 @get_port_and_ip(%struct.addrinfo* %25, i8* %11)
store i32 %26, i32* %5
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = load i32, i32* %5
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %31, i8* %11)
%33 = call i32 @perror(i32* null)
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %3
br label %39
36: ; preds = %16
%37 = load i32, i32* %5
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %37, i8* %11)
br label %39
39: ; preds = %36, %29
br label %40
40: ; preds = %39, %1
%41 = load i32, i32* %3
%42 = load i8*, i8** %6
ret i32 %41
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@INET6_ADDRSTRLEN = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"ERROR: bind to port %d on %s failed: \00"
@.str.1 = [26 x i8] c"bind to port %d on %s OK\0A\00"
define i32 @bind_socket(%struct.addrinfo* %0) {
%2 = load i32, i32* @INET6_ADDRSTRLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32 @get_socket(%struct.addrinfo* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %22, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @bind(i32 %5, i32 %9, i32 %11)
%13 = call i32 @get_port_and_ip(%struct.addrinfo* %0, i8* %4)
%14 = icmp eq i32 %12, 0
br i1 %14, label %20, label %15
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %4)
%18 = call i32 @perror(i32* null)
%19 = call i32 @close(i32 %5)
br label %22
20: ; preds = %7
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %4)
br label %22
22: ; preds = %15, %20, %1
%23 = phi i32 [ -1, %15 ], [ %5, %20 ], [ -1, %1 ]
ret i32 %23
}