#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;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int find_socket(struct addrinfo *res) {
  assert(res != NULL);
  printf("creating socket...\n");
  int socketfd;
  for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) {
    socketfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (socketfd == -1) {
      perror("find_socket-socket()");
      continue;
    } else {
      return socketfd;
    }
  }
  assert(socketfd == -1);
  fprintf(stderr, "Couldn't find a socket\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<find_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ee <find_socket+0x8e>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d4 <find_socket+0x74>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
jmp    11dd <find_socket+0x7d>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1192 <find_socket+0x32>
cmpl   $0xffffffff,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<find_socket>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
lea    0xeb1(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
je     11b0 <find_socket+0x50>
lea    0xe73(%rip),%r14
mov    0x8(%rbx),%edi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
jne    11dd <find_socket+0x7d>
mov    %r14,%rdi
call   1060 <perror@plt>
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
jne    118d <find_socket+0x2d>
mov    $0x1,%edi
call   1040 <assert@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo* }
@.str = [20 x i8] c"creating socket...\0A\00"
@.str.1 = [21 x i8] c"find_socket-socket()\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Couldn't find a socket\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @find_socket(%struct.addrinfo* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = alloca %struct.addrinfo*
store %struct.addrinfo* %0, %struct.addrinfo** %2
%5 = load %struct.addrinfo*, %struct.addrinfo** %2
%6 = icmp ne %struct.addrinfo* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = load %struct.addrinfo*, %struct.addrinfo** %2
store %struct.addrinfo* %10, %struct.addrinfo** %4
br label %11
11: ; preds = %31, %1
%12 = load %struct.addrinfo*, %struct.addrinfo** %4
%13 = icmp ne %struct.addrinfo* %12, null
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 2
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %17, i32 %20, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %31
29: ; preds = %14
%30 = load i32, i32* %3
ret i32 %30
31: ; preds = %27
%32 = load %struct.addrinfo*, %struct.addrinfo** %4
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load %struct.addrinfo*, %struct.addrinfo** %33
store %struct.addrinfo* %34, %struct.addrinfo** %4
br label %11
35: ; preds = %11
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, -1
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo* }
@.str.1 = [21 x i8] c"find_socket-socket()\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Couldn't find a socket\0A\00"
@EXIT_FAILURE = global i32 0
@str = [19 x i8] c"creating socket...\00"
define i32 @find_socket(%struct.addrinfo* %0) {
%2 = icmp ne %struct.addrinfo* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%6 = icmp eq %struct.addrinfo* %0, null
br i1 %6, label %22, label %7
7: ; preds = %1, %17
%8 = phi %struct.addrinfo* [ %20, %17 ], [ %0, %1 ]
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 2
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @socket(i32 %10, i32 %12, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %28
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i64 0, i32 3
%20 = load %struct.addrinfo*, %struct.addrinfo** %19
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %22, label %7
22: ; preds = %17, %1
%23 = tail call i32 @assert(i32 1)
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %7
ret i32 %15
}