#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 */
struct stat {
  int st_mode;
};

/* Variables and functions */
#define S_IFBLK 134
#define S_IFCHR 133
#define S_IFDIR 132
#define S_IFIFO 131
#define S_IFLNK 130
int S_IFMT;
#define S_IFREG 129
#define S_IFSOCK 128
char *memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ strlen(char *);
char *gettype(const char *fpath, char *ptr, struct stat *statdat) {
  char desc[128] = {0};
  switch (statdat->st_mode & S_IFMT) {
  case S_IFBLK:
    sprintf(desc, "block device");
    break;
  case S_IFCHR:
    sprintf(desc, "character device");
    break;
  case S_IFDIR:
    sprintf(desc, "directory");
    break;
  case S_IFIFO:
    sprintf(desc, "FIFO/pipe");
    break;
  case S_IFLNK:
    sprintf(desc, "symlink");
    break;
  case S_IFREG:
    sprintf(desc, "regular file");
    break;
  case S_IFSOCK:
    sprintf(desc, "socket");
    break;
  default:
    sprintf(desc, "UNKNOWN: %u", (statdat->st_mode & S_IFMT));
    break;
  }
  return memcpy(ptr, desc, strlen(desc));
}


x86 assembly (clang, -O0):
<gettype>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xorps  %xmm0,%xmm0
movaps %xmm0,-0xc0(%rbp)
movaps %xmm0,-0x30(%rbp)
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x60(%rbp)
movaps %xmm0,-0x70(%rbp)
movaps %xmm0,-0x80(%rbp)
movaps %xmm0,-0x90(%rbp)
movaps %xmm0,-0xa0(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0xa8(%rbp)
sub    $0x6,%eax
ja     126a <gettype+0x13a>
mov    -0xa8(%rbp),%rax
lea    0xe55(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0xa0(%rbp),%rdi
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    128e <gettype+0x15e>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    0x2d62(%rip),%rax
and    (%rax),%edx
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
lea    -0xa0(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xd0(%rbp),%rdi
mov    -0xc8(%rbp),%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettype>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%rax
mov    %rsi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%edx
and    (%rax),%edx
lea    -0x80(%rdx),%eax
cmp    $0x6,%eax
ja     11a1 <gettype+0x71>
lea    0xe7c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x74656b,0x3(%rsp)
movl   $0x6b636f73,(%rsp)
jmp    123b <gettype+0x10b>
lea    0xeba(%rip),%rsi
mov    %rsp,%rdi
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    123b <gettype+0x10b>
movabs $0x656c6966207261,%rax
mov    %rax,0x5(%rsp)
movabs $0x2072616c75676572,%rax
61 72 20
jmp    1237 <gettype+0x107>
movabs $0x6b6e696c6d7973,%rax
jmp    1237 <gettype+0x107>
movabs $0x7069702f4f464946,%rax
70 69 70
mov    %rax,(%rsp)
movw   $0x65,0x8(%rsp)
jmp    123b <gettype+0x10b>
movabs $0x726f746365726964,%rax
74 6f 72
mov    %rax,(%rsp)
movw   $0x79,0x8(%rsp)
jmp    123b <gettype+0x10b>
movups 0xe16(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
jmp    123b <gettype+0x10b>
movabs $0x65636976656420,%rax
mov    %rax,0x5(%rsp)
movabs $0x6564206b636f6c62,%rax
20 64 65
mov    %rax,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@.str = [13 x i8] c"block device\00"
@.str.1 = [17 x i8] c"character device\00"
@.str.2 = [10 x i8] c"directory\00"
@.str.3 = [10 x i8] c"FIFO/pipe\00"
@.str.4 = [8 x i8] c"symlink\00"
@.str.5 = [13 x i8] c"regular file\00"
@.str.6 = [7 x i8] c"socket\00"
@.str.7 = [12 x i8] c"UNKNOWN: %u\00"
define i8* @gettype(i8* %0, i8* %1, %struct.stat* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca [128 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store %struct.stat* %2, %struct.stat** %6
%8 = bitcast [128 x i8]* %7 to i8*
%9 = load %struct.stat*, %struct.stat** %6
%10 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFMT
%13 = and i32 %11, %12
switch i32 %13, label %35 [
i32 134, label %14
i32 133, label %17
i32 132, label %20
i32 131, label %23
i32 130, label %26
i32 129, label %29
i32 128, label %32
]
14: ; preds = %3
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%16 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %43
17: ; preds = %3
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %43
20: ; preds = %3
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %43
23: ; preds = %3
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %43
26: ; preds = %3
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %43
29: ; preds = %3
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%31 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %43
32: ; preds = %3
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %43
35: ; preds = %3
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%37 = load %struct.stat*, %struct.stat** %6
%38 = getelementptr inbounds %struct.stat, %struct.stat* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @S_IFMT
%41 = and i32 %39, %40
%42 = call i32 (i8*, i8*, ...) @sprintf(i8* %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %35, %32, %29, %26, %23, %20, %17, %14
%44 = load i8*, i8** %5
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = call i8* @memcpy(i8* %44, i8* %45, i32 %47)
ret i8* %48
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@.str = [13 x i8] c"block device\00"
@.str.1 = [17 x i8] c"character device\00"
@.str.2 = [10 x i8] c"directory\00"
@.str.3 = [10 x i8] c"FIFO/pipe\00"
@.str.5 = [13 x i8] c"regular file\00"
@.str.6 = [7 x i8] c"socket\00"
@.str.7 = [12 x i8] c"UNKNOWN: %u\00"
define i8* @gettype(i8* nocapture readnone %0, i8* %1, %struct.stat* nocapture %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = load i32, i32* @S_IFMT
%9 = and i32 %8, %7
switch i32 %9, label %18 [
i32 134, label %10
i32 133, label %11
i32 132, label %12
i32 131, label %13
i32 130, label %14
i32 129, label %16
i32 128, label %17
]
10: ; preds = %3
br label %20
11: ; preds = %3
br label %20
12: ; preds = %3
br label %20
13: ; preds = %3
br label %20
14: ; preds = %3
%15 = bitcast [128 x i8]* %4 to i64*
store i64 30239221577775475, i64* %15
br label %20
16: ; preds = %3
br label %20
17: ; preds = %3
br label %20
18: ; preds = %3
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %9)
br label %20
20: ; preds = %18, %17, %16, %14, %13, %12, %11, %10
%21 = call i32 @strlen(i8* %5)
%22 = call i8* @memcpy(i8* %1, i8* %5, i32 %21)
ret i8* %22
}