#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 /*<<< orphan*/ strncpy(char *, char *, int);
int get_user_agent(FILE *fp, char *buf, int maxsz, char *target) {
  char ch;
  int i = 0;
  while ((ch = fgetc(fp)) != '|') {
    if (ch == EOF)
      return -1;
    buf[i++] = ch;
    if (i == maxsz) {
      printf("User agent buffer[%d] if full\n", maxsz);
      printf("\n%s\n", buf);
      exit(1);
    }
  }
  buf[i] = '\0';
  strncpy(target, buf, maxsz);
  return 0;
}


x86 assembly (clang, -O0):
<get_user_agent>:
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    $0x7c,%eax
je     11ee <get_user_agent+0xae>
movsbl -0x29(%rbp),%eax
mov    0x2e67(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1198 <get_user_agent+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <get_user_agent+0xd1>
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    11e9 <get_user_agent+0xa9>
mov    -0x1c(%rbp),%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    115e <get_user_agent+0x1e>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%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_user_agent>:
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    $0x7c,%al
je     11ba <get_user_agent+0x7a>
cmp    (%r14),%al
je     11d4 <get_user_agent+0x94>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1170 <get_user_agent+0x30>
lea    0xe6b(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe79(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %ebx,%eax
movb   $0x0,(%r12,%rax,1)
mov    (%rsp),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    11d9 <get_user_agent+0x99>
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 = [31 x i8] c"User agent buffer[%d] if full\0A\00"
@.str.1 = [5 x i8] c"\0A%s\0A\00"
define i32 @get_user_agent(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 = %40, %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, 124
br i1 %16, label %17, label %41
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 %50
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 %40
34: ; preds = %24
%35 = load i32, i32* %8
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %7
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %24
br label %12
41: ; preds = %12
%42 = load i8*, i8** %7
%43 = load i32, i32* %11
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %9
%47 = load i8*, i8** %7
%48 = load i32, i32* %8
%49 = call i32 @strncpy(i8* %46, i8* %47, i32 %48)
store i32 0, i32* %5
br label %50
50: ; preds = %41, %23
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@EOF = global i8 0
@.str = [31 x i8] c"User agent buffer[%d] if full\0A\00"
@.str.1 = [5 x i8] c"\0A%s\0A\00"
define i32 @get_user_agent(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, 124
br i1 %9, label %21, 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %2)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %6
%22 = and i64 %7, 4294967295
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 0, i8* %23
%24 = tail call i32 @strncpy(i8* %3, i8* %1, i32 %2)
br label %25
25: ; preds = %10, %21
%26 = phi i32 [ 0, %21 ], [ -1, %10 ]
ret i32 %26
}