#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*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int strlen(char *);
char *strtok_r(char *, char *, char **);
int main(int argc, char *argv[]) {
  char cmd[128];
  char *cp;
  char *sp;
  printf("input a string of command\n");
  fgets(cmd, sizeof(cmd), stdin);
  cmd[strlen(cmd) - 1] = '\0';
  for (cp = strtok_r(cmd, " ", &sp); cp != NULL;
       cp = strtok_r(NULL, " ", &sp)) {
    printf("\tcp='%s'", cp);
    printf("\tsp='%s'\n", sp);
  }
  printf("cmd = '%s', sp = '%s'\n", cmd, sp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1060 <fgets@plt>
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rdi
lea    0xe73(%rip),%rsi
lea    -0xa0(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
je     1216 <main+0xd6>
mov    -0x98(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xa0(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe1d(%rip),%rsi
lea    -0xa0(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x98(%rbp)
jmp    11bb <main+0x7b>
lea    -0x90(%rbp),%rsi
mov    -0xa0(%rbp),%rdx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x98,%rsp
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe65(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     11fe <main+0xae>
lea    0xe4e(%rip),%r14
lea    0xe50(%rip),%r15
lea    0xe3e(%rip),%r12
lea    0x8(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
jne    11d0 <main+0x80>
mov    0x8(%rsp),%rdx
lea    0xe0b(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"input a string of command\0A\00"
@stdin = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [9 x i8] c"\09cp='%s'\00"
@.str.3 = [10 x i8] c"\09sp='%s'\0A\00"
@.str.4 = [23 x i8] c"cmd = '%s', sp = '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [128 x i8]
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %10, i32 128, i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %16
store i8 0, i8* %17
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%19 = call i8* @strtok_r(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %8)
store i8* %19, i8** %7
br label %20
20: ; preds = %28, %2
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i8*, i8** %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23
%29 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %8)
store i8* %29, i8** %7
br label %20
30: ; preds = %20
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%32 = load i8*, i8** %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %31, i8* %32)
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [9 x i8] c"\09cp='%s'\00"
@.str.3 = [10 x i8] c"\09sp='%s'\0A\00"
@.str.4 = [23 x i8] c"cmd = '%s', sp = '%s'\0A\00"
@str = [26 x i8] c"input a string of command\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %5, i32 128, i32 %8)
%10 = call i32 @strlen(i8* %5)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i8* @strtok_r(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %4)
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %2, %16
%17 = phi i8* [ %21, %16 ], [ %14, %2 ]
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %4
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %4)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %16
23: ; preds = %16, %2
%24 = load i8*, i8** %4
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %5, i8* %24)
ret i32 0
}