#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 /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
char *word(char *target, char *source, int pos) {
  int i;
  char *found, *string;
  if (target == NULL)
    return NULL;
  if (source == NULL)
    target[0] = '\0';
  else {
    string = strdup(source);
    found = strtok(string, " ");
    for (i = 1; i < pos; found = strtok(NULL, " "), i++) {
    };
    if (found == NULL)
      target[0] = '\0';
    else
      strcpy(target, found);
    free(string);
  }
  return target;
}


x86 assembly (clang, -O0):
<word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116b <word+0x2b>
movq   $0x0,-0x8(%rbp)
jmp    1212 <word+0xd2>
cmpq   $0x0,-0x18(%rbp)
jne    1182 <word+0x42>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    120a <word+0xca>
mov    -0x18(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11dd <word+0x9d>
jmp    11bb <word+0x7b>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe3a(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11aa <word+0x6a>
cmpq   $0x0,-0x28(%rbp)
jne    11f4 <word+0xb4>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1201 <word+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<word>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
test   %rdi,%rdi
je     11b3 <word+0x73>
test   %rsi,%rsi
je     11a1 <word+0x61>
mov    %edx,%ebp
mov    %rsi,%rdi
call   1060 <strdup@plt>
mov    %rax,%r14
lea    0xe98(%rip),%rsi
mov    %rax,%rdi
call   1050 <strtok@plt>
cmp    $0x2,%ebp
jl     118f <word+0x4f>
add    $0xffffffff,%ebp
lea    0xe81(%rip),%rbx
nop
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <strtok@plt>
add    $0xffffffff,%ebp
jne    1180 <word+0x40>
test   %rax,%rax
je     11a7 <word+0x67>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
jmp    11ab <word+0x6b>
movb   $0x0,(%r15)
jmp    11b3 <word+0x73>
movb   $0x0,(%r15)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i8* @word(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %25, label %5
5: ; preds = %3
%6 = icmp eq i8* %1, null
br i1 %6, label %7, label %8
7: ; preds = %5
store i8 0, i8* %0
br label %25
8: ; preds = %5
%9 = tail call i8* @strdup(i8* %1)
%10 = tail call i8* @strtok(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp sgt i32 %2, 1
br i1 %11, label %12, label %17
12: ; preds = %8, %12
%13 = phi i32 [ %15, %12 ], [ 1, %8 ]
%14 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, %2
br i1 %16, label %17, label %12
17: ; preds = %12, %8
%18 = phi i8* [ %10, %8 ], [ %14, %12 ]
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17
store i8 0, i8* %0
br label %23
21: ; preds = %17
%22 = tail call i32 @strcpy(i8* %0, i8* %18)
br label %23
23: ; preds = %21, %20
%24 = tail call i32 @free(i8* %9)
br label %25
25: ; preds = %7, %23, %3
ret i8* %0
}