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

/* Variables and functions */
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getsockname(int, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(void) {
  struct sockaddr name;
  int res1, res2, res3;
  int len = 10;
  res1 = socket(PF_UNIX, SOCK_STREAM, 0);
  if (res1 == 0) {
    fprintf(stderr, "socket() failed\n");
    exit(1);
  }
  res2 = getsockname(res1, NULL, &len);
  res3 = getsockname(res1, &name, NULL);
  if (res2 == -1) {
    fprintf(stderr, "getsockname(1) failed\n");
  }
  if (res3 == -1) {
    fprintf(stderr, "getsockname(2) failed\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0x18(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    119b <main+0x5b>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x18(%rbp),%rdx
call   1040 <getsockname@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <getsockname@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11e0 <main+0xa0>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1030 <fprintf@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ff <main+0xbf>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0xa,0xc(%rsp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
je     11d1 <main+0x91>
mov    %eax,%ebx
lea    0xc(%rsp),%rdx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <getsockname@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <getsockname@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%ebp
je     11a0 <main+0x60>
cmp    $0xffffffff,%ebx
je     11ba <main+0x7a>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1030 <fprintf@plt>
cmp    $0xffffffff,%ebx
jne    1197 <main+0x57>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1197 <main+0x57>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@.str.1 = [23 x i8] c"getsockname(1) failed\0A\00"
@.str.2 = [23 x i8] c"getsockname(2) failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 10, i32* %6
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %3
%18 = call i32 @getsockname(i32 %17, %struct.sockaddr* null, i32* %6)
store i32 %18, i32* %4
%19 = load i32, i32* %3
%20 = call i32 @getsockname(i32 %19, %struct.sockaddr* %2, i32* null)
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %16
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %29, %26
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@.str.1 = [23 x i8] c"getsockname(1) failed\0A\00"
@.str.2 = [23 x i8] c"getsockname(2) failed\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr
%2 = alloca i32
%3 = bitcast %struct.sockaddr* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 10, i32* %2
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @getsockname(i32 %7, %struct.sockaddr* null, i32* %2)
%15 = call i32 @getsockname(i32 %7, %struct.sockaddr* %1, i32* null)
%16 = icmp eq i32 %14, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17, %13
%21 = icmp eq i32 %15, -1
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %22, %20
ret i32 0
}