#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 pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
bool pid_is_zombie(pid_t pid) {
  FILE *fh = NULL;
  char buf[200] = {0};
  char status;
  bool result = false;
  snprintf(buf, 200, "/proc/%d/stat", pid);
  fh = fopen(buf, "r");
  if (fh == NULL) {
    result = true;
    goto cleanup;
  }
  if (fscanf(fh, "%*d %*s %c", &status) != 1) {
    result = true;
    goto cleanup;
  }
  if (status == 'Z') {
    result = true;
  }
cleanup : {
  if (fh != NULL) {
    fclose(fh);
  }
  return result;
}
}


x86 assembly (clang, -O0):
<pid_is_zombie>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0xe0(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1050 <memset@plt>
movl   $0x0,-0xe8(%rbp)
lea    -0xe0(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0xc8,%esi
lea    0xe67(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xe0(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cf <pid_is_zombie+0x7f>
movl   $0x1,-0xe8(%rbp)
jmp    121d <pid_is_zombie+0xcd>
mov    -0x10(%rbp),%rdi
lea    0xe36(%rip),%rsi
lea    -0xe1(%rbp),%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     11fe <pid_is_zombie+0xae>
movl   $0x1,-0xe8(%rbp)
jmp    121d <pid_is_zombie+0xcd>
movsbl -0xe1(%rbp),%eax
cmp    $0x5a,%eax
jne    1218 <pid_is_zombie+0xc8>
movl   $0x1,-0xe8(%rbp)
jmp    121d <pid_is_zombie+0xcd>
cmpq   $0x0,-0x10(%rbp)
je     1231 <pid_is_zombie+0xe1>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xe8(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_zombie>:
push   %rbp
push   %rbx
sub    $0xd8,%rsp
mov    %edi,%ecx
xorps  %xmm0,%xmm0
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
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)
movq   $0x0,0xd0(%rsp)
lea    0xe54(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc8,%esi
call   1040 <snprintf@plt>
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1206 <pid_is_zombie+0xc6>
mov    %rax,%rbx
lea    0xe34(%rip),%rsi
lea    0xf(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
setne  %al
cmpb   $0x5a,0xf(%rsp)
sete   %cl
or     %al,%cl
movzbl %cl,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    120b <pid_is_zombie+0xcb>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"%*d %*s %c\00"
define i32 @pid_is_zombie(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [200 x i8]
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %2
store i32* null, i32** %3
%7 = bitcast [200 x i8]* %4 to i8*
store i32 0, i32* %6
%8 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %2
%10 = call i32 @snprintf(i8* %8, i32 200, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 1, i32* %6
br label %27
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 1, i32* %6
br label %27
21: ; preds = %16
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 90
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 1, i32* %6
br label %26
26: ; preds = %25, %21
br label %27
27: ; preds = %26, %20, %15
%28 = load i32*, i32** %3
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
br label %33
33: ; preds = %30, %27
%34 = load i32, i32* %6
ret i32 %34
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"%*d %*s %c\00"
define i32 @pid_is_zombie(i32 %0) {
%2 = alloca [200 x i8]
%3 = alloca i8
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 200, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %16, label %8
8: ; preds = %1
%9 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%10 = icmp ne i32 %9, 1
%11 = load i8, i8* %3
%12 = icmp eq i8 %11, 90
%13 = select i1 %10, i1 true, i1 %12
%14 = zext i1 %13 to i32
%15 = call i32 @fclose(i32* %6)
br label %16
16: ; preds = %1, %8
%17 = phi i32 [ %14, %8 ], [ 1, %1 ]
ret i32 %17
}