#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char EOF;
int /*<<< orphan*/ exit(int);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int get_referer(FILE *fp, char *buf, int maxsz, char *target) {
  char ch;
  int i = 0;
  while ((ch = fgetc(fp)) != '\n') {
    if (ch == EOF)
      return -1;
    buf[i++] = ch;
    if (i == maxsz) {
      printf("Referer buffer[%d] if full\n", maxsz);
      exit(1);
    }
  }
  buf[i] = '\0';
  strncpy(target, buf + 1, maxsz);
  return 0;
}


x86 assembly (clang, -O0):
<get_referer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     11da <get_referer+0x9a>
movsbl -0x29(%rbp),%eax
mov    0x2e67(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1198 <get_referer+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <get_referer+0xc1>
mov    -0x29(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x30(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11d5 <get_referer+0x95>
mov    -0x1c(%rbp),%esi
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    115e <get_referer+0x1e>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_referer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,(%rsp)
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbp
mov    %edx,%r13d
xor    %ebx,%ebx
mov    0x2e84(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1050 <fgetc@plt>
cmp    $0xa,%al
je     11a7 <get_referer+0x67>
cmp    (%r14),%al
je     11c5 <get_referer+0x85>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1170 <get_referer+0x30>
lea    0xe6b(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %ebx,%eax
movb   $0x0,(%r12,%rax,1)
add    $0x1,%r12
mov    (%rsp),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    11ca <get_referer+0x8a>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
@.str = [28 x i8] c"Referer buffer[%d] if full\0A\00"
define i32 @get_referer(i32* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %11
br label %12
12: ; preds = %38, %4
%13 = load i32*, i32** %6
%14 = call signext i8 @fgetc(i32* %13)
store i8 %14, i8* %10
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 10
br i1 %16, label %17, label %39
17: ; preds = %12
%18 = load i8, i8* %10
%19 = sext i8 %18 to i32
%20 = load i8, i8* @EOF
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %19, %21
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %5
br label %49
24: ; preds = %17
%25 = load i8, i8* %10
%26 = load i8*, i8** %7
%27 = load i32, i32* %11
%28 = add nsw i32 %27, 1
store i32 %28, i32* %11
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 %25, i8* %30
%31 = load i32, i32* %11
%32 = load i32, i32* %8
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %24
%35 = load i32, i32* %8
%36 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %24
br label %12
39: ; preds = %12
%40 = load i8*, i8** %7
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %9
%45 = load i8*, i8** %7
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i32, i32* %8
%48 = call i32 @strncpy(i8* %44, i8* %46, i32 %47)
store i32 0, i32* %5
br label %49
49: ; preds = %39, %23
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@EOF = global i8 0
@.str = [28 x i8] c"Referer buffer[%d] if full\0A\00"
define i32 @get_referer(i32* nocapture %0, i8* %1, i32 %2, i8* %3) {
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %13, %4
%7 = phi i64 [ %14, %13 ], [ 0, %4 ]
%8 = tail call signext i8 @fgetc(i32* %0)
%9 = icmp eq i8 %8, 10
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = load i8, i8* @EOF
%12 = icmp eq i8 %8, %11
br i1 %12, label %25, label %13
13: ; preds = %10
%14 = add nuw nsw i64 %7, 1
%15 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %8, i8* %15
%16 = icmp eq i64 %14, %5
br i1 %16, label %17, label %6
17: ; preds = %13
%18 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %2)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %6
%21 = and i64 %7, 4294967295
%22 = getelementptr inbounds i8, i8* %1, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds i8, i8* %1, i64 1
%24 = tail call i32 @strncpy(i8* %3, i8* %23, i32 %2)
br label %25
25: ; preds = %10, %20
%26 = phi i32 [ 0, %20 ], [ -1, %10 ]
ret i32 %26
}