#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*/ uint16_t;
typedef int /*<<< orphan*/ turn_on_option_flag;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(scalar_t__, struct sockaddr *, int);
int /*<<< orphan*/ debug_perror(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/,
                      char *, int);
scalar_t__ socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int32_t tcp_sock_open(struct in_addr ip, int32_t port) {
  int32_t fd, turn_on_option_flag = 1, rcSockopt;
  struct sockaddr_in sa;
  memset((void *)&sa, 0, sizeof(sa));
  fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd < 0) {
    debug_perror("Could not create socket");
    exit(1);
  }
  sa.sin_family = AF_INET;
  memcpy((void *)&sa.sin_addr, (void *)&ip, sizeof(struct in_addr));
  sa.sin_port = htons((uint16_t)port);
  rcSockopt =
      setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&turn_on_option_flag,
                 sizeof(turn_on_option_flag));
  if (bind(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0) {
    debug_perror("Could not bind to port");
    exit(1);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<tcp_sock_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ec <tcp_sock_open+0x6c>
lea    0xe23(%rip),%rdi
call   1030 <debug_perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x4,%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1277 <tcp_sock_open+0xf7>
lea    0xdb0(%rip),%rdi
call   1030 <debug_perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_sock_open>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,0x8(%rsp)
movq   $0x1,0x20(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %rax,%rax
js     1244 <tcp_sock_open+0xc4>
mov    %rax,%rbx
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x14(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rcx
mov    %rbx,%rdi
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     124d <tcp_sock_open+0xcd>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb5(%rip),%rdi
jmp    1254 <tcp_sock_open+0xd4>
lea    0xdc4(%rip),%rdi
call   1030 <debug_perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@AF_INET = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [23 x i8] c"Could not bind to port\00"
define i64 @tcp_sock_open(i32 %0, i64 %1) {
%3 = alloca %struct.in_addr
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.sockaddr_in
%9 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %0, i32* %9
store i64 %1, i64* %4
store i64 1, i64* %6
%10 = bitcast %struct.sockaddr_in* %8 to i8*
%11 = call i32 @memset(i8* %10, i32 0, i32 12)
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = load i32, i32* @IPPROTO_TCP
%15 = call i64 @socket(i32 %12, i32 %13, i32 %14)
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @debug_perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%25 = bitcast i32* %24 to i8*
%26 = bitcast %struct.in_addr* %3 to i8*
%27 = call i32 @memcpy(i8* %25, i8* %26, i32 4)
%28 = load i64, i64* %4
%29 = trunc i64 %28 to i32
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i64, i64* %5
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = bitcast i64* %6 to i8*
%36 = call i64 @setsockopt(i64 %32, i32 %33, i32 %34, i8* %35, i32 8)
store i64 %36, i64* %7
%37 = load i64, i64* %5
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i64 @bind(i64 %37, %struct.sockaddr* %38, i32 4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %21
%42 = call i32 @debug_perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %21
%45 = load i64, i64* %5
ret i64 %45
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@AF_INET = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [23 x i8] c"Could not bind to port\00"
define i64 @tcp_sock_open(i32 %0, i64 %1) {
%3 = alloca %struct.in_addr
%4 = alloca i64
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
store i32 %0, i32* %6
%7 = bitcast i64* %4 to i8*
store i64 1, i64* %4
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = call i32 @memset(i8* %8, i32 0, i32 12)
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i64 @socket(i32 %10, i32 %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @debug_perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%22 = bitcast i32* %21 to i8*
%23 = bitcast %struct.in_addr* %3 to i8*
%24 = call i32 @memcpy(i8* %22, i8* %23, i32 4)
%25 = trunc i64 %1 to i32
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i64 @setsockopt(i64 %13, i32 %28, i32 %29, i8* %7, i32 8)
%31 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%32 = call i64 @bind(i64 %13, %struct.sockaddr* %31, i32 4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %18
%35 = call i32 @debug_perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %18
ret i64 %13
}