#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 */
char *calloc(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *realloc(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char *strtok(char *, char *);
char *longest_word(char *str) {
  char *token = strtok(str, " ,.!?");
  int len = strlen(token);
  char *result = calloc(1, sizeof(char));
  if (!result) {
    fprintf(stderr, "%s\n", strerror(errno));
  }
  int biggestWord = 0;
  while (token) {
    len = strlen(token);
    if (len > biggestWord) {
      biggestWord = len;
      char *relocResult = realloc(result, biggestWord + 1);
      if (!result) {
        fprintf(stderr, "%s\n", strerror(errno));
      }
      result = relocResult;
      strcpy(result, token);
    }
    token = strtok(NULL, " ,.!?");
  }
  return result;
}


x86 assembly (clang, -O0):
<longest_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    $0x1,%esi
mov    %esi,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e3 <longest_word+0x73>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe28(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     128b <longest_word+0x11b>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    1272 <longest_word+0x102>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125d <longest_word+0xed>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdae(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd83(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,-0x10(%rbp)
jmp    11ea <longest_word+0x7a>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<longest_word>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rsi
call   1080 <strtok@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11b3 <longest_word+0x43>
test   %r13,%r13
jne    11e4 <longest_word+0x74>
jmp    126b <longest_word+0xfb>
mov    0x2e26(%rip),%rax
mov    (%rax),%ebx
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe35(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
test   %r13,%r13
je     126b <longest_word+0xfb>
xor    %ebp,%ebp
lea    0xe19(%rip),%r15
lea    0xe0c(%rip),%r12
jmp    1222 <longest_word+0xb2>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%r14
mov    %ebx,%ebp
xor    %edi,%edi
mov    %r12,%rsi
call   1080 <strtok@plt>
mov    %rax,%r13
test   %rax,%rax
je     126b <longest_word+0xfb>
mov    %r13,%rdi
call   1040 <strlen@plt>
cmp    %ebp,%eax
jle    1210 <longest_word+0xa0>
mov    %eax,%ebx
lea    0x1(%rbx),%esi
mov    %r14,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbp
test   %r14,%r14
jne    1200 <longest_word+0x90>
mov    0x2d96(%rip),%rax
mov    (%rax),%r14d
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    1200 <longest_word+0x90>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c" ,.!?\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i8* @longest_word(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i8* @strtok(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %4
%12 = call i8* @calloc(i32 1, i32 1)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %20, label %15
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %15, %1
store i32 0, i32* %6
br label %21
21: ; preds = %48, %20
%22 = load i8*, i8** %3
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %50
24: ; preds = %21
%25 = load i8*, i8** %3
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = load i32, i32* %6
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %48
30: ; preds = %24
%31 = load i32, i32* %4
store i32 %31, i32* %6
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
%35 = call i8* @realloc(i8* %32, i32 %34)
store i8* %35, i8** %7
%36 = load i8*, i8** %5
%37 = icmp ne i8* %36, null
br i1 %37, label %43, label %38
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %38, %30
%44 = load i8*, i8** %7
store i8* %44, i8** %5
%45 = load i8*, i8** %5
%46 = load i8*, i8** %3
%47 = call i32 @strcpy(i8* %45, i8* %46)
br label %48
48: ; preds = %43, %24
%49 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %49, i8** %3
br label %21
50: ; preds = %21
%51 = load i8*, i8** %5
ret i8* %51
}

LLVM IR (-O2):
@.str = [6 x i8] c" ,.!?\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
define i8* @longest_word(i8* %0) {
%2 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @strlen(i8* %2)
%4 = tail call dereferenceable_or_null(1) i8* @calloc(i32 1, i32 1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %6, %1
%12 = icmp eq i8* %2, null
br i1 %12, label %35, label %13
13: ; preds = %11, %30
%14 = phi i8* [ %33, %30 ], [ %2, %11 ]
%15 = phi i32 [ %32, %30 ], [ 0, %11 ]
%16 = phi i8* [ %31, %30 ], [ %4, %11 ]
%17 = tail call i32 @strlen(i8* %14)
%18 = icmp sgt i32 %17, %15
br i1 %18, label %19, label %30
19: ; preds = %13
%20 = add nsw i32 %17, 1
%21 = tail call i8* @realloc(i8* %16, i32 %20)
%22 = icmp eq i8* %16, null
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %19
%29 = tail call i32 @strcpy(i8* %21, i8* %14)
br label %30
30: ; preds = %28, %13
%31 = phi i8* [ %21, %28 ], [ %16, %13 ]
%32 = phi i32 [ %17, %28 ], [ %15, %13 ]
%33 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %13
35: ; preds = %30, %11
%36 = phi i8* [ %4, %11 ], [ %31, %30 ]
ret i8* %36
}