#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_exit(char *);
int /*<<< orphan*/ htons(int);
void sock_4(int *sock, int port) {
  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = INADDR_ANY;
  int succ = bind(*sock, (struct sockaddr *)&sa, sizeof(sa));
  if (succ < 0) {
    close(*sock);
    error_exit("bind of socket for version 4");
  }
}


x86 assembly (clang, -O0):
<sock_4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11aa <sock_4+0x6a>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
lea    0xe5b(%rip),%rdi
call   1050 <error_exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_4>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    1192 <sock_4+0x52>
mov    (%rbx),%edi
call   1040 <close@plt>
lea    0xe73(%rip),%rdi
call   1050 <error_exit@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [29 x i8] c"bind of socket for version 4\00"
define void @sock_4(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* %4
%10 = call i32 @htons(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = load i32*, i32** %3
%16 = load i32, i32* %15
%17 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%18 = call i32 @bind(i32 %16, %struct.sockaddr* %17, i32 12)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i32*, i32** %3
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = call i32 @error_exit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %21, %2
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [29 x i8] c"bind of socket for version 4\00"
define void @sock_4(i32* nocapture %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 %1)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* %0
%12 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%13 = call i32 @bind(i32 %11, %struct.sockaddr* %12, i32 12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* %0
%17 = call i32 @close(i32 %16)
%18 = call i32 @error_exit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %15, %2
ret void
}