#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 /*<<< orphan*/ EXIT_FAILURE;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
void example_bind(int sock, struct addrinfo *ai) {
  if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
    perror("server: bind() failed: ");
    printf("\n");
    printf("If bind failed because the address is already in use,\n");
    printf("it is likely because a server is already running on\n");
    printf("the same port. Only one server can listen on each port\n");
    printf("at any given time.\n");
    close(sock);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<example_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    11db <example_bind+0x8b>
lea    0xe7c(%rip),%rdi
call   1060 <perror@plt>
lea    0xebd(%rip),%rdi
call   1030 <printf@plt>
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
lea    0xea7(%rip),%rdi
call   1030 <printf@plt>
lea    0xed0(%rip),%rdi
call   1030 <printf@plt>
lea    0xefc(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<example_bind>:
push   %rbx
mov    %edi,%ebx
mov    (%rsi),%edx
mov    0x4(%rsi),%esi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1174 <example_bind+0x14>
pop    %rbx
ret
lea    0xe85(%rip),%rdi
call   1070 <perror@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe87(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb1(%rip),%rdi
call   1040 <puts@plt>
lea    0xed9(%rip),%rdi
call   1040 <puts@plt>
lea    0xf04(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@.str = [24 x i8] c"server: bind() failed: \00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [55 x i8] c"If bind failed because the address is already in use,\0A\00"
@.str.3 = [53 x i8] c"it is likely because a server is already running on\0A\00"
@.str.4 = [56 x i8] c"the same port. Only one server can listen on each port\0A\00"
@.str.5 = [20 x i8] c"at any given time.\0A\00"
@EXIT_FAILURE = global i32 0
define void @example_bind(i32 %0, %struct.addrinfo* %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo*
store i32 %0, i32* %3
store %struct.addrinfo* %1, %struct.addrinfo** %4
%5 = load i32, i32* %3
%6 = load %struct.addrinfo*, %struct.addrinfo** %4
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
%8 = load i32, i32* %7
%9 = load %struct.addrinfo*, %struct.addrinfo** %4
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @bind(i32 %5, i32 %8, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@.str = [24 x i8] c"server: bind() failed: \00"
@EXIT_FAILURE = global i32 0
@str = [54 x i8] c"If bind failed because the address is already in use,\00"
@str.6 = [52 x i8] c"it is likely because a server is already running on\00"
@str.7 = [55 x i8] c"the same port. Only one server can listen on each port\00"
@str.8 = [19 x i8] c"at any given time.\00"
define void @example_bind(i32 %0, %struct.addrinfo* nocapture %1) {
%3 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 1
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i32 @bind(i32 %0, i32 %4, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %19
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @putchar(i32 10)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.6, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.7, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
%16 = tail call i32 @close(i32 %0)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
ret void
}