#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*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createSocket(struct addrinfo *addr) {
  printf("Creating new socket with given address info\n");
  int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
  if (sock < 0) {
    printf("Error occurred creating socket so now I am sad");
    return -1;
  }
  printf("Successfully created socket: %ld\n", sock);
  return sock;
}


x86 assembly (clang, -O0):
<createSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xecd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1040 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    117a <createSocket+0x5a>
lea    0xec6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1191 <createSocket+0x71>
mov    -0x14(%rbp),%esi
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createSocket>:
push   %rbx
mov    %rdi,%rbx
lea    0xf16(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%edi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   1050 <socket@plt>
test   %eax,%eax
js     1165 <createSocket+0x35>
mov    %eax,%ebx
lea    0xed5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1178 <createSocket+0x48>
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [45 x i8] c"Creating new socket with given address info\0A\00"
@.str.1 = [47 x i8] c"Error occurred creating socket so now I am sad\00"
@.str.2 = [34 x i8] c"Successfully created socket: %ld\0A\00"
define i32 @createSocket(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%6 = load %struct.addrinfo*, %struct.addrinfo** %3
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
%8 = load i32, i32* %7
%9 = load %struct.addrinfo*, %struct.addrinfo** %3
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load %struct.addrinfo*, %struct.addrinfo** %3
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @socket(i32 %8, i32 %11, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
20: ; preds = %1
%21 = load i32, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %24
24: ; preds = %20, %18
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@.str.1 = [47 x i8] c"Error occurred creating socket so now I am sad\00"
@.str.2 = [34 x i8] c"Successfully created socket: %ld\0A\00"
@str = [44 x i8] c"Creating new socket with given address info\00"
define i32 @createSocket(%struct.addrinfo* nocapture %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%3 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @socket(i32 %4, i32 %6, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %15
13: ; preds = %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ -1, %11 ], [ %9, %13 ]
ret i32 %16
}