#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int create_socket(const struct sockaddr_in *addr) {
  int ret;
  int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock == -1)
    fatal("Unable to create socket: %s", strerror(errno));
  ret = connect(sock, (const struct sockaddr *)addr, sizeof(*addr));
  if (ret != 0)
    fatal("Unable to connect: %s", strerror(errno));
  return sock;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1195 <create_socket+0x55>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    %eax,%esi
lea    0xe70(%rip),%rdi
call   1050 <fatal@plt>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11cf <create_socket+0x8f>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
call   1050 <fatal@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    118a <create_socket+0x4a>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
call   1050 <fatal@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <connect@plt>
test   %eax,%eax
je     11b9 <create_socket+0x79>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
call   1050 <fatal@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [28 x i8] c"Unable to create socket: %s\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Unable to connect: %s\00"
define i32 @create_socket(%struct.sockaddr_in* %0) {
%2 = alloca %struct.sockaddr_in*
%3 = alloca i32
%4 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %1
%16 = load i32, i32* %4
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %2
%18 = bitcast %struct.sockaddr_in* %17 to %struct.sockaddr*
%19 = call i32 @connect(i32 %16, %struct.sockaddr* %18, i32 4)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @fatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %15
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [28 x i8] c"Unable to create socket: %s\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"Unable to connect: %s\00"
define i32 @create_socket(%struct.sockaddr_in* %0) {
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = load i32, i32* @IPPROTO_TCP
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %7, %1
%12 = bitcast %struct.sockaddr_in* %0 to %struct.sockaddr*
%13 = tail call i32 @connect(i32 %5, %struct.sockaddr* %12, i32 4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @fatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %11
ret i32 %5
}