#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_socket(int *out_sock, struct sockaddr *sa, socklen_t salen) {
  int ret;
  ret = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (ret == -1) {
    perror("tftp: socket() failure");
    return -errno;
  }
  if (-1 == connect(ret, sa, salen)) {
    perror("tftp: connect() failure");
    close(ret);
    return -errno;
  }
  *out_sock = ret;
  return 0;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    119f <open_socket+0x5f>
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
mov    0x2e3b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <open_socket+0xb4>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11e4 <open_socket+0xa4>
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
mov    0x2df6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <open_socket+0xb4>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     1199 <open_socket+0x59>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
je     11a7 <open_socket+0x67>
mov    %ebp,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe60(%rip),%rdi
call   1040 <perror@plt>
jmp    11ba <open_socket+0x7a>
lea    0xe69(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2e0f(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    118e <open_socket+0x4e>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [23 x i8] c"tftp: socket() failure\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"tftp: connect() failure\00"
define i32 @open_socket(i32* %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store %struct.sockaddr* %1, %struct.sockaddr** %6
store i32 %2, i32* %7
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %4
br label %34
19: ; preds = %3
%20 = load i32, i32* %8
%21 = load %struct.sockaddr*, %struct.sockaddr** %6
%22 = load i32, i32* %7
%23 = call i32 @connect(i32 %20, %struct.sockaddr* %21, i32 %22)
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
store i32 %30, i32* %4
br label %34
31: ; preds = %19
%32 = load i32, i32* %8
%33 = load i32*, i32** %5
store i32 %32, i32* %33
store i32 0, i32* %4
br label %34
34: ; preds = %31, %25, %15
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [23 x i8] c"tftp: socket() failure\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"tftp: connect() failure\00"
define i32 @open_socket(i32* nocapture writeonly %0, %struct.sockaddr* %1, i32 %2) {
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_UDP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %22
13: ; preds = %3
%14 = tail call i32 @connect(i32 %7, %struct.sockaddr* %1, i32 %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @close(i32 %7)
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
br label %22
21: ; preds = %13
store i32 %7, i32* %0
br label %22
22: ; preds = %21, %16, %9
%23 = phi i32 [ %12, %9 ], [ %20, %16 ], [ 0, %21 ]
ret i32 %23
}