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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
void *htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int create_socket(struct in_addr *remote_ip) {
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in sockaddr;
  memset(&sockaddr, 0, sizeof(sockaddr));
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_port = htons(2000);
  if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
    printf("Bind failed: %s\n", strerror(errno));
    exit(1);
  }
  sockaddr.sin_port = htons(2000);
  sockaddr.sin_addr = *remote_ip;
  connect(sock, (const struct sockaddr *)&sockaddr, sizeof(sockaddr));
  return sock;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     1214 <create_socket+0x94>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1080 <connect@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e54(%rip),%r15
mov    (%r15),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x18,%edx
call   1060 <bind@plt>
test   %rax,%rax
jne    1216 <create_socket+0x96>
mov    $0x7d0,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%rsp)
mov    (%r14),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1080 <connect@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd5(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [17 x i8] c"Bind failed: %s\0A\00"
@errno = global i32 0
define i32 @create_socket(%struct.in_addr* %0) {
%2 = alloca %struct.in_addr*
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store %struct.in_addr* %0, %struct.in_addr** %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 24)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %9, i32* %10
%11 = call i8* @htons(i32 2000)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i8* %11, i8** %12
%13 = load i32, i32* %3
%14 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%15 = call i64 @bind(i32 %13, %struct.sockaddr* %14, i32 24)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %1
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = call i8* @htons(i32 2000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i8* %23, i8** %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%26 = load %struct.in_addr*, %struct.in_addr** %2
%27 = bitcast %struct.in_addr* %25 to i8*
%28 = bitcast %struct.in_addr* %26 to i8*
%29 = load i32, i32* %3
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 24)
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [17 x i8] c"Bind failed: %s\0A\00"
@errno = global i32 0
define i32 @create_socket(%struct.in_addr* nocapture %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 24)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i8* @htons(i32 2000)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i8* %10, i8** %11
%12 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%13 = call i64 @bind(i32 %5, %struct.sockaddr* %12, i32 24)
%14 = icmp eq i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = call i8* @htons(i32 2000)
store i8* %21, i8** %11
%22 = getelementptr %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%24 = load i32, i32* %22
store i32 %24, i32* %23
%25 = call i32 @connect(i32 %5, %struct.sockaddr* %12, i32 24)
ret i32 %5
}