#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 */
int field_end(char *);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *csv_get_string(char *start) {
  int length = field_end(start);
  if (length == 0)
    return NULL;
  char tmp = start[length];
  start[length] = 0;
  char *out = malloc(length + 1);
  strcpy(out, start);
  start[length] = tmp;
  return out;
}


x86 assembly (clang, -O0):
<csv_get_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <field_end@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    115f <csv_get_string+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11ab <csv_get_string+0x7b>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<csv_get_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <field_end@plt>
test   %eax,%eax
je     116d <csv_get_string+0x3d>
movslq %eax,%r15
mov    (%rbx,%r15,1),%bpl
movb   $0x0,(%rbx,%r15,1)
add    $0x1,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %bpl,(%rbx,%r15,1)
jmp    1170 <csv_get_string+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @csv_get_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @field_end(i8* %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %34
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
store i8 %17, i8* %5
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = load i8*, i8** %3
%27 = call i32 @strcpy(i8* %25, i8* %26)
%28 = load i8, i8* %5
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
%33 = load i8*, i8** %6
store i8* %33, i8** %2
br label %34
34: ; preds = %12, %11
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
define i8* @csv_get_string(i8* %0) {
%2 = tail call i32 @field_end(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
store i8 0, i8* %6
%8 = add nsw i32 %2, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i32 @strcpy(i8* %9, i8* %0)
store i8 %7, i8* %6
br label %11
11: ; preds = %1, %4
%12 = phi i8* [ %9, %4 ], [ null, %1 ]
ret i8* %12
}