#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 */
typedef size_t uint32_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct sockaddr_in *ai_next;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ serverName;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct sockaddr_in *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct sockaddr_in *,
                struct sockaddr_in **);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unboxString(int /*<<< orphan*/, char *, int, size_t *);
int /*<<< orphan*/ unboxTuple(size_t *, int);
scalar_t__ unboxUnsigned(int /*<<< orphan*/);
uint32_t *socketBind(uint32_t context_len, uint32_t *tuple) {
  int sd;
  int port;
  char hostname[256];
  uint32_t hostname_len;
  int status = 0;
  struct sockaddr_in serverName = {0};
  struct addrinfo hints, *res, *r;
  int error;
  sd = (int)unboxUnsigned(unboxTuple(tuple, 0));
  unboxString(unboxTuple(tuple, 1), hostname, 256, &hostname_len);
  hostname[hostname_len] = (char)0;
  port = (int)unboxUnsigned(unboxTuple(tuple, 2));
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  error = getaddrinfo(hostname, NULL, &hints, &res);
  if (error != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(error));
    exit(1);
  }
  for (r = res; r != NULL; r = r->ai_next) {
    memset(&serverName, 0x0, sizeof(serverName));
    memcpy(&serverName, r->ai_addr, r->ai_addrlen);
    serverName.sin_port = htons(port);
    status = bind(sd, (struct sockaddr *)&serverName, sizeof(serverName));
    if (status == 0) {
      break;
    }
  }
  freeaddrinfo(res);
  if (r == NULL) {
    perror("bind:");
    exit(1);
  }
  return (uint32_t *)NULL;
}


x86 assembly (clang, -O0):
<socketBind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x12c(%rbp)
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1080 <unboxTuple@plt>
mov    %eax,%edi
call   1070 <unboxUnsigned@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1080 <unboxTuple@plt>
mov    %eax,%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
lea    -0x128(%rbp),%rcx
call   1060 <unboxString@plt>
mov    -0x128(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1080 <unboxTuple@plt>
mov    %eax,%edi
call   1070 <unboxUnsigned@plt>
mov    %eax,-0x18(%rbp)
lea    -0x170(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x158(%rbp)
lea    -0x120(%rbp),%rdi
lea    -0x170(%rbp),%rdx
lea    -0x178(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   10e0 <getaddrinfo@plt>
mov    %eax,-0x184(%rbp)
cmpl   $0x0,-0x184(%rbp)
je     12f0 <socketBind+0x120>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x188(%rbp)
mov    -0x184(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x188(%rbp),%edi
mov    %rax,%rdx
lea    0xd1f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x178(%rbp),%rax
mov    %rax,-0x180(%rbp)
cmpq   $0x0,-0x180(%rbp)
je     1395 <socketBind+0x1c5>
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0x180(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x180(%rbp),%rax
mov    0x4(%rax),%edx
lea    -0x150(%rbp),%rdi
call   10a0 <memcpy@plt>
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x150(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    $0x20,%edx
call   10b0 <bind@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0x0,-0x12c(%rbp)
jne    1379 <socketBind+0x1a9>
jmp    1395 <socketBind+0x1c5>
jmp    137e <socketBind+0x1ae>
mov    -0x180(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x180(%rbp)
jmp    12fe <socketBind+0x12e>
mov    -0x178(%rbp),%rdi
call   10f0 <freeaddrinfo@plt>
cmpq   $0x0,-0x180(%rbp)
jne    13c5 <socketBind+0x1f5>
lea    0xc61(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
xor    %eax,%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketBind>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x158,%rsp
mov    %rsi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    %rsi,%rdi
xor    %esi,%esi
call   1080 <unboxTuple@plt>
mov    %eax,%edi
call   1070 <unboxUnsigned@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <unboxTuple@plt>
lea    0x50(%rsp),%r12
lea    0x8(%rsp),%rcx
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x100,%edx
call   1060 <unboxString@plt>
mov    0x8(%rsp),%rax
movb   $0x0,0x50(%rsp,%rax,1)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1080 <unboxTuple@plt>
mov    %eax,%edi
call   1070 <unboxUnsigned@plt>
mov    %rax,%r15
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x48(%rsp)
mov    %rsp,%rcx
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   10e0 <getaddrinfo@plt>
test   %eax,%eax
jne    1312 <socketBind+0x142>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     12da <socketBind+0x10a>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %r12,%rdi
call   10a0 <memcpy@plt>
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x20,%edx
call   10b0 <bind@plt>
test   %eax,%eax
je     12f8 <socketBind+0x128>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    1290 <socketBind+0xc0>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   10f0 <freeaddrinfo@plt>
lea    0xd2e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    (%rsp),%rdi
call   10f0 <freeaddrinfo@plt>
xor    %eax,%eax
add    $0x158,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2cbf(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xcd7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

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

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