#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 addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int example_socket(struct addrinfo *ai) {
  int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  if (sock == -1) {
    perror("socket() error:");
    exit(EXIT_FAILURE);
  }
  return sock;
}


x86 assembly (clang, -O0):
<example_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    117c <example_socket+0x4c>
lea    0xe97(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<example_socket>:
push   %rax
mov    0x8(%rdi),%eax
mov    (%rdi),%edx
mov    0x4(%rdi),%esi
mov    %eax,%edi
call   1050 <socket@plt>
cmp    $0xffffffff,%eax
je     1147 <example_socket+0x17>
pop    %rcx
ret
lea    0xeb2(%rip),%rdi
call   1030 <perror@plt>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [16 x i8] c"socket() error:\00"
@EXIT_FAILURE = global i32 0
define i32 @example_socket(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %2
%4 = load %struct.addrinfo*, %struct.addrinfo** %2
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 2
%6 = load i32, i32* %5
%7 = load %struct.addrinfo*, %struct.addrinfo** %2
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
%9 = load i32, i32* %8
%10 = load %struct.addrinfo*, %struct.addrinfo** %2
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @socket(i32 %6, i32 %9, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [16 x i8] c"socket() error:\00"
@EXIT_FAILURE = global i32 0
define i32 @example_socket(%struct.addrinfo* nocapture %0) {
%2 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%3 = load i32, i32* %2
%4 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @socket(i32 %3, i32 %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
ret i32 %8
}