#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 */

/* Variables and functions */
scalar_t__ ELOOP;
int /*<<< orphan*/ O_RDWR;
scalar_t__ close(int);
int /*<<< orphan*/ die(char *, int, ...);
scalar_t__ errno;
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
void test_link_traversal(bool nosymfollow) {
  int link;
  link = open("/tmp/symlink", 0, O_RDWR);
  if (nosymfollow) {
    if ((link != -1 || errno != ELOOP)) {
      die("link traversal unexpected result: %d, %s\n", link, strerror(errno));
    }
  } else {
    if (link < 0)
      die("link traversal failed: %s\n", strerror(errno));
    if (close(link) != 0)
      die("close of link failed: %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<test_link_traversal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
lea    0xea5(%rip),%rdi
xor    %esi,%esi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11c0 <test_link_traversal+0x80>
cmpl   $0xffffffff,-0x8(%rbp)
jne    1193 <test_link_traversal+0x53>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
je     11bb <test_link_traversal+0x7b>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <die@plt>
jmp    121f <test_link_traversal+0xdf>
cmpl   $0x0,-0x8(%rbp)
jge    11e9 <test_link_traversal+0xa9>
mov    0x2dff(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <die@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     121a <test_link_traversal+0xda>
mov    0x2dce(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    %eax,%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <die@plt>
jmp    121f <test_link_traversal+0xdf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_link_traversal>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edx
lea    0xeab(%rip),%rdi
xor    %esi,%esi
call   1040 <open@plt>
mov    %eax,%ebx
test   %ebp,%ebp
je     119a <test_link_traversal+0x5a>
mov    0x2e67(%rip),%rax
mov    (%rax),%rdi
cmp    $0xffffffff,%ebx
jne    117d <test_link_traversal+0x3d>
mov    0x2e68(%rip),%rax
cmp    (%rax),%rdi
je     11ee <test_link_traversal+0xae>
call   1060 <strerror@plt>
lea    0xe84(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <die@plt>
test   %ebx,%ebx
jns    11bd <test_link_traversal+0x7d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xe83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <die@plt>
mov    %ebx,%edi
call   1030 <close@plt>
test   %rax,%rax
je     11ee <test_link_traversal+0xae>
mov    0x2e00(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xe73(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <die@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/tmp/symlink\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ELOOP = global i64 0
@.str.1 = [42 x i8] c"link traversal unexpected result: %d, %s\0A\00"
@.str.2 = [27 x i8] c"link traversal failed: %s\0A\00"
@.str.3 = [26 x i8] c"close of link failed: %s\0A\00"
define void @test_link_traversal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0, i32 %4)
store i32 %5, i32* %3
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %21
8: ; preds = %1
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ELOOP
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11, %8
%16 = load i32, i32* %3
%17 = load i64, i64* @errno
%18 = call i32 @strerror(i64 %17)
%19 = call i32 (i8*, i32, ...) @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %18)
br label %20
20: ; preds = %15, %11
br label %37
21: ; preds = %1
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = call i32 @strerror(i64 %25)
%27 = call i32 (i8*, i32, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %24, %21
%29 = load i32, i32* %3
%30 = call i64 @close(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = call i32 @strerror(i64 %33)
%35 = call i32 (i8*, i32, ...) @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %28
br label %37
37: ; preds = %36, %20
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/tmp/symlink\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ELOOP = global i64 0
@.str.1 = [42 x i8] c"link traversal unexpected result: %d, %s\0A\00"
@.str.2 = [27 x i8] c"link traversal failed: %s\0A\00"
@.str.3 = [26 x i8] c"close of link failed: %s\0A\00"
define void @test_link_traversal(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0, i32 %2)
%4 = icmp eq i32 %0, 0
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = icmp eq i32 %3, -1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ELOOP
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %27, label %11
11: ; preds = %5
%12 = tail call i32 @strerror(i64 %7)
%13 = tail call i32 (i8*, i32, ...) @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %12)
br label %27
14: ; preds = %1
%15 = icmp slt i32 %3, 0
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = load i64, i64* @errno
%18 = tail call i32 @strerror(i64 %17)
%19 = tail call i32 (i8*, i32, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %16, %14
%21 = tail call i64 @close(i32 %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = tail call i32 @strerror(i64 %24)
%26 = tail call i32 (i8*, i32, ...) @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %5, %20, %23, %11
ret void
}