#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 int /*<<< orphan*/ val;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  struct in_addr sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcp_bind(char *server, int port) {
  int sd, ret, val = 1;
  struct sockaddr_in sin;
  struct hostent *host;
  host = gethostbyname(server);
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd == -1) {
    exit(3);
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_addr = *((struct in_addr *)host->h_addr);
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  ret = bind(sd, (struct sockaddr *)&sin, sizeof(sin));
  if (ret == -1) {
    exit(4);
  }
  listen(sd, 5);
  return (sd);
}


x86 assembly (clang, -O0):
<tcp_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d3 <tcp_bind+0x53>
mov    $0x3,%edi
call   1090 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1252 <tcp_bind+0xd2>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_bind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r15d
movl   $0x1,0xc(%rsp)
call   1060 <gethostbyname@plt>
mov    %rax,%rbp
mov    0x2e3a(%rip),%r12
mov    (%r12),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     124a <tcp_bind+0xca>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1254 <tcp_bind+0xd4>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%edi
call   1090 <exit@plt>
mov    $0x4,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @tcp_bind(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 1, i32* %7
%10 = load i8*, i8** %3
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %9
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @exit(i32 3)
unreachable
19: ; preds = %2
%20 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%22 = load %struct.hostent*, %struct.hostent** %9
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to %struct.in_addr*
%26 = bitcast %struct.in_addr* %21 to i8*
%27 = bitcast %struct.in_addr* %25 to i8*
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* %4
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %5
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i32* %7, i32 4)
%37 = load i32, i32* %5
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %19
%43 = call i32 @exit(i32 4)
unreachable
44: ; preds = %19
%45 = load i32, i32* %5
%46 = call i32 @listen(i32 %45, i32 5)
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @tcp_bind(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @exit(i32 3)
unreachable
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i32*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
%20 = load i32, i32* %18
store i32 %20, i32* %19
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %10, i32 %25, i32 %26, i32* %3, i32 4)
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i32 @bind(i32 %10, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %14
%32 = call i32 @exit(i32 4)
unreachable
33: ; preds = %14
%34 = call i32 @listen(i32 %10, i32 5)
ret i32 %10
}