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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int sock_alloc(int listen, struct sockaddr_in *addr) {
  int fd;
  int rc;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  addr->sin_family = AF_INET;
  if (fd < 0) {
    fprintf(stderr, "Failed to open socket: %s\n", strerror(errno));
    return fd;
  }
  if (listen) {
    addr->sin_addr.s_addr = INADDR_ANY;
    rc = bind(fd, (struct sockaddr *)addr, sizeof(*addr));
    if (rc) {
      fprintf(stderr, "Failed to bind: %s\n", strerror(errno));
      return -1;
    }
  } else {
    rc = connect(fd, (struct sockaddr *)addr, sizeof(*addr));
    if (rc) {
      fprintf(stderr, "Failed to connect: %s\n", strerror(errno));
      return -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<sock_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpl   $0x0,-0x14(%rbp)
jge    11cc <sock_alloc+0x7c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe44(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a1 <sock_alloc+0x151>
cmpl   $0x0,-0x8(%rbp)
je     1240 <sock_alloc+0xf0>
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1040 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     123b <sock_alloc+0xeb>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdf1(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <sock_alloc+0x151>
jmp    129b <sock_alloc+0x14b>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1296 <sock_alloc+0x146>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdaa(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a1 <sock_alloc+0x151>
jmp    129b <sock_alloc+0x14b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_alloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e84(%rip),%r15
mov    (%r15),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0x4(%rbx)
test   %ebp,%ebp
js     11b4 <sock_alloc+0x64>
test   %r14d,%r14d
je     11bd <sock_alloc+0x6d>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <bind@plt>
test   %eax,%eax
je     1200 <sock_alloc+0xb0>
mov    $0xffffffff,%ebp
lea    0xe69(%rip),%r14
jmp    11dc <sock_alloc+0x8c>
lea    0xe45(%rip),%r14
jmp    11dc <sock_alloc+0x8c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
test   %eax,%eax
je     1200 <sock_alloc+0xb0>
mov    $0xffffffff,%ebp
lea    0xe53(%rip),%r14
mov    0x2de5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Failed to open socket: %s\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [20 x i8] c"Failed to bind: %s\0A\00"
@.str.2 = [23 x i8] c"Failed to connect: %s\0A\00"
define i32 @sock_alloc(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* @AF_INET
%12 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %11, i32* %13
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %6
store i32 %21, i32* %3
br label %57
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %22
%26 = load i32, i32* @INADDR_ANY
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %26, i32* %29
%30 = load i32, i32* %6
%31 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%32 = bitcast %struct.sockaddr_in* %31 to %struct.sockaddr*
%33 = call i32 @bind(i32 %30, %struct.sockaddr* %32, i32 8)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %25
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32 -1, i32* %3
br label %57
41: ; preds = %25
br label %55
42: ; preds = %22
%43 = load i32, i32* %6
%44 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%45 = bitcast %struct.sockaddr_in* %44 to %struct.sockaddr*
%46 = call i32 @connect(i32 %43, %struct.sockaddr* %45, i32 8)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %52)
store i32 -1, i32* %3
br label %57
54: ; preds = %42
br label %55
55: ; preds = %54, %41
%56 = load i32, i32* %6
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %49, %36, %16
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Failed to open socket: %s\0A\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [20 x i8] c"Failed to bind: %s\0A\00"
@.str.2 = [23 x i8] c"Failed to connect: %s\0A\00"
define i32 @sock_alloc(i32 %0, %struct.sockaddr_in* %1) {
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %6, i32* %7
%8 = icmp slt i32 %5, 0
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = icmp eq i32 %0, 0
br i1 %10, label %17, label %11
11: ; preds = %9
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = tail call i32 @bind(i32 %5, %struct.sockaddr* %14, i32 8)
%16 = icmp eq i32 %15, 0
br i1 %16, label %28, label %21
17: ; preds = %9
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = tail call i32 @connect(i32 %5, %struct.sockaddr* %18, i32 8)
%20 = icmp eq i32 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %17, %11, %2
%22 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %17 ]
%23 = phi i32 [ %5, %2 ], [ -1, %11 ], [ -1, %17 ]
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* %22, i8* %26)
br label %28
28: ; preds = %21, %11, %17
%29 = phi i32 [ %5, %17 ], [ %5, %11 ], [ %23, %21 ]
ret i32 %29
}