#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;
char fgetc(int /*<<< orphan*/ *);
char *strdup(char *);
char *read_field_from_csv(FILE *fp, char sep) {
  char buffer[4096 + 1];
  int i;
  for (i = 0;
       (buffer[i] = fgetc(fp)) != EOF && buffer[i] != '\n' && buffer[i] != sep;
       i++)
    ;
  if (i == 0)
    return NULL;
  buffer[i] = '\0';
  return (strdup(buffer));
}


x86 assembly (clang, -O0):
<read_field_from_csv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
movslq -0x1024(%rbp),%rcx
mov    %al,-0x1020(%rbp,%rcx,1)
movsbl %al,%ecx
mov    0x2e88(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x1025(%rbp)
je     11b1 <read_field_from_csv+0x91>
movslq -0x1024(%rbp),%rax
movsbl -0x1020(%rbp,%rax,1),%ecx
ff
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x1025(%rbp)
je     11b1 <read_field_from_csv+0x91>
movslq -0x1024(%rbp),%rax
movsbl -0x1020(%rbp,%rax,1),%eax
ff
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x1025(%rbp)
mov    -0x1025(%rbp),%al
test   $0x1,%al
jne    11c4 <read_field_from_csv+0xa4>
jmp    11dd <read_field_from_csv+0xbd>
jmp    11c9 <read_field_from_csv+0xa9>
mov    -0x1024(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
jmp    113f <read_field_from_csv+0x1f>
cmpl   $0x0,-0x1024(%rbp)
jne    11f7 <read_field_from_csv+0xd7>
movq   $0x0,-0x8(%rbp)
jmp    1216 <read_field_from_csv+0xf6>
movslq -0x1024(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_field_from_csv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0xffffffffffffffff,%rbp
mov    0x2ea7(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %al,0x1(%rsp,%rbp,1)
add    $0x1,%rbp
cmp    %r14b,%al
je     116e <read_field_from_csv+0x4e>
cmp    $0xa,%al
je     116e <read_field_from_csv+0x4e>
cmp    (%r15),%al
jne    1150 <read_field_from_csv+0x30>
test   %ebp,%ebp
je     1182 <read_field_from_csv+0x62>
mov    %ebp,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1040 <strdup@plt>
jmp    1184 <read_field_from_csv+0x64>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
define i8* @read_field_from_csv(i32* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8
%6 = alloca [4097 x i8]
%7 = alloca i32
store i32* %0, i32** %4
store i8 %1, i8* %5
store i32 0, i32* %7
br label %8
8: ; preds = %37, %2
%9 = load i32*, i32** %4
%10 = call signext i8 @fgetc(i32* %9)
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %12
store i8 %10, i8* %13
%14 = sext i8 %10 to i32
%15 = load i8, i8* @EOF
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %14, %16
br i1 %17, label %18, label %34
18: ; preds = %8
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 10
br i1 %24, label %25, label %34
25: ; preds = %18
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = load i8, i8* %5
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %30, %32
br label %34
34: ; preds = %25, %18, %8
%35 = phi i1 [ false, %18 ], [ false, %8 ], [ %33, %25 ]
br i1 %35, label %36, label %40
36: ; preds = %34
br label %37
37: ; preds = %36
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %8
40: ; preds = %34
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %40
store i8* null, i8** %3
br label %50
44: ; preds = %40
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %46
store i8 0, i8* %47
%48 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 0
%49 = call i8* @strdup(i8* %48)
store i8* %49, i8** %3
br label %50
50: ; preds = %44, %43
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@EOF = global i8 0
define noalias i8* @read_field_from_csv(i32* nocapture %0, i8 signext %1) {
%3 = alloca [4097 x i8]
%4 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %5, %2
%6 = phi i64 [ %15, %5 ], [ 0, %2 ]
%7 = tail call signext i8 @fgetc(i32* %0)
%8 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 %6
store i8 %7, i8* %8
%9 = load i8, i8* @EOF
%10 = icmp eq i8 %7, %9
%11 = icmp eq i8 %7, 10
%12 = or i1 %11, %10
%13 = icmp eq i8 %7, %1
%14 = or i1 %13, %12
%15 = add nuw i64 %6, 1
br i1 %14, label %16, label %5
16: ; preds = %5
%17 = trunc i64 %6 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = and i64 %6, 4294967295
%21 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i8* @strdup(i8* %4)
br label %23
23: ; preds = %16, %19
%24 = phi i8* [ %22, %19 ], [ null, %16 ]
ret i8* %24
}