#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_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int prepare_socket() {
  int s;
  struct sockaddr_in addr;
  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    perror("error: prepare_socket()");
    exit(EXIT_FAILURE);
  };
  bzero(&addr, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(40118);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    perror("error: prepare_socket()");
    exit(EXIT_FAILURE);
  };
  return s;
}


x86 assembly (clang, -O0):
<prepare_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11be <prepare_socket+0x4e>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x9cb6,%edi
call   1030 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    122a <prepare_socket+0xba>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%r15
mov    (%r15),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f7 <prepare_socket+0x87>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0x9cb6,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     11f7 <prepare_socket+0x87>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe02(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [24 x i8] c"error: prepare_socket()\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
define i32 @prepare_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = load i32, i32* @IPPROTO_UDP
%6 = call i32 @socket(i32 %3, i32 %4, i32 %5)
store i32 %6, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 40118)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %1
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %12
%27 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %12
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [24 x i8] c"error: prepare_socket()\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
define i32 @prepare_socket() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = load i32, i32* @IPPROTO_UDP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 40118)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %6, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %12
%25 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %12
ret i32 %6
}