#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 *csvgetfield(char *, int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *);
char *strdup(char *);
int main() {
  char line[1024];
  const char filename[] = "file.csv";
  FILE *file = fopen(filename, "r");
  while (fgets(line, 1024, file)) {
    char *tmp = strdup(line);
    printf("Field3 would be %s\n", csvgetfield(tmp, 3, ","));
    free(tmp);
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x419(%rbp)
mov    0xe72(%rip),%al
mov    %al,-0x411(%rbp)
lea    -0x419(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1223 <main+0xb3>
lea    -0x410(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
mov    $0x3,%esi
lea    0xe21(%rip),%rdx
call   1080 <csvgetfield@plt>
mov    %rax,%rsi
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
mov    -0x430(%rbp),%rdi
call   1030 <free@plt>
jmp    11b6 <main+0x46>
mov    -0x428(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
movabs $0x7673632e656c6966,%rax
63 73 76
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
lea    0xe6f(%rip),%rsi
mov    %rsp,%rdi
call   1070 <fopen@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1213 <main+0xa3>
lea    0x10(%rsp),%r13
lea    0xe57(%rip),%r15
lea    0xe3c(%rip),%r12
nop
mov    %r13,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1080 <csvgetfield@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11d0 <main+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"file.csv\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Field3 would be %s\0A\00"
@.str.2 = [2 x i8] c",\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca [9 x i8]
%4 = alloca i32*
%5 = alloca i8*
store i32 0, i32* %1
%6 = bitcast [9 x i8]* %3 to i8*
%7 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
br label %9
9: ; preds = %14, %0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = load i32*, i32** %4
%12 = call i64 @fgets(i8* %10, i32 1024, i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = call i8* @csvgetfield(i8* %17, i32 3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %5
%21 = call i32 @free(i8* %20)
br label %9
22: ; preds = %9
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"file.csv\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"Field3 would be %s\0A\00"
@.str.2 = [2 x i8] c",\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca [9 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = call i64 @fgets(i8* %3, i32 1024, i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %0, %8
%9 = call i8* @strdup(i8* %3)
%10 = call i8* @csvgetfield(i8* %9, i32 3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @free(i8* %9)
%13 = call i64 @fgets(i8* %3, i32 1024, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %8
15: ; preds = %8, %0
%16 = call i32 @fclose(i32* %5)
ret i32 0
}