#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 dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket_or_die(const char *port) {
  struct addrinfo hints;
  struct addrinfo *res;
  int sfd, ret;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_PASSIVE;
  if ((ret = getaddrinfo(NULL, port, &hints, &res)) != 0)
    fail("Error in getaddrinfo()");
  if ((sfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
    fail("Unable to open socket, look some more at res[] list..");
  if (bind(sfd, res->ai_addr, sizeof(struct sockaddr_in)) == -1)
    fail("Unable to bind socket..");
  freeaddrinfo(res);
  return sfd;
}


x86 assembly (clang, -O0):
<open_socket_or_die>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     11cd <open_socket_or_die+0x6d>
lea    0xe38(%rip),%rdi
call   1030 <fail@plt>
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11ff <open_socket_or_die+0x9f>
lea    0xe1d(%rip),%rdi
call   1030 <fail@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    1227 <open_socket_or_die+0xc7>
lea    0xe2b(%rip),%rdi
call   1030 <fail@plt>
mov    -0x28(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket_or_die>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11c7 <open_socket_or_die+0x67>
lea    0xe3e(%rip),%rdi
call   1030 <fail@plt>
mov    0x8(%rsp),%rax
mov    0xc(%rax),%edi
mov    0x4(%rax),%edx
mov    0x8(%rax),%esi
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ec <open_socket_or_die+0x8c>
lea    0xe30(%rip),%rdi
call   1030 <fail@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    1210 <open_socket_or_die+0xb0>
lea    0xe42(%rip),%rdi
call   1030 <fail@plt>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [23 x i8] c"Error in getaddrinfo()\00"
@.str.1 = [54 x i8] c"Unable to open socket, look some more at res[] list..\00"
@.str.2 = [24 x i8] c"Unable to bind socket..\00"
define i32 @open_socket_or_die(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 20)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 3
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_DGRAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i8*, i8** %2
%15 = call i32 @getaddrinfo(i32* null, i8* %14, %struct.addrinfo* %3, %struct.addrinfo** %4)
store i32 %15, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @fail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %1
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 3
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %22, i32 %25, i32 %28)
store i32 %29, i32* %5
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 @fail(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %19
%34 = load i32, i32* %5
%35 = load %struct.addrinfo*, %struct.addrinfo** %4
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @bind(i32 %34, i32 %37, i32 4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 @fail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %33
%43 = load %struct.addrinfo*, %struct.addrinfo** %4
%44 = call i32 @freeaddrinfo(%struct.addrinfo* %43)
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [23 x i8] c"Error in getaddrinfo()\00"
@.str.1 = [54 x i8] c"Unable to open socket, look some more at res[] list..\00"
@.str.2 = [24 x i8] c"Unable to bind socket..\00"
define i32 @open_socket_or_die(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 20)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 @fail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %1
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 1
%24 = load i32, i32* %23
%25 = call i32 @socket(i32 %20, i32 %22, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @fail(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %17
%30 = load %struct.addrinfo*, %struct.addrinfo** %3
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @bind(i32 %25, i32 %32, i32 4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @fail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = load %struct.addrinfo*, %struct.addrinfo** %3
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
ret i32 %25
}