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

/* Variables and functions */
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getl(char *, int, int /*<<< orphan*/ *);
scalar_t__ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int sscanf(char *, char *, int *, int *, int *, int *, int *, int *, int *,
           int *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int scan_shm(int search_uid, int *p_count, FILE *rep_f) {
  int retval = 0;
  char buf[1024];
  FILE *f = fopen("/proc/sysvipc/shm", "r");
  if (!f) {
    fprintf(stderr, "cannot open file '/proc/sysvipc/shm'\n");
    return 1;
  }
  if (getl(buf, sizeof(buf), f) < 0) {
    fprintf(stderr, "unexpected EOF in '/proc/sysvipc/shm'\n");
    return 1;
  }
  while (getl(buf, sizeof(buf), f) >= 0) {
    int key = 0, shmid = 0, perms = 0, size = 0, cpid = 0, lpid = 0, nattch = 0,
        uid = 0, gid = 0;
    if (sscanf(buf, "%d%d%o%d%d%d%d%d%d", &key, &shmid, &perms, &size, &cpid,
               &lpid, &nattch, &uid, &gid) != 9) {
      fprintf(stderr, "format error in '/proc/sysvipc/shm'\n");
      return 1;
    }
    if (uid == search_uid) {
      fprintf(rep_f, "shared memory: key = 0x%08x, shmid = %d, perms = %03o\n",
              key, shmid, perms);
      if (shmctl(shmid, IPC_RMID, NULL) < 0) {
        fprintf(stderr, "shmctl failed: %s\n", strerror(errno));
      }
      retval = 1;
      ++*p_count;
    }
  }
  fclose(f);
  return retval;
}


x86 assembly (clang, -O0):
<scan_shm>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x480,%rsp
mov    %edi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe69(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    11db <scan_shm+0x6b>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    13ec <scan_shm+0x27c>
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rdx
mov    $0x400,%esi
call   1030 <getl@plt>
cmp    $0x0,%rax
jge    1220 <scan_shm+0xb0>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    13ec <scan_shm+0x27c>
jmp    1225 <scan_shm+0xb5>
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rdx
mov    $0x400,%esi
call   1030 <getl@plt>
cmp    $0x0,%rax
jl     13da <scan_shm+0x26a>
movl   $0x0,-0x43c(%rbp)
movl   $0x0,-0x440(%rbp)
movl   $0x0,-0x444(%rbp)
movl   $0x0,-0x448(%rbp)
movl   $0x0,-0x44c(%rbp)
movl   $0x0,-0x450(%rbp)
movl   $0x0,-0x454(%rbp)
movl   $0x0,-0x458(%rbp)
movl   $0x0,-0x45c(%rbp)
lea    -0x430(%rbp),%rdi
lea    0xdb2(%rip),%rsi
lea    -0x43c(%rbp),%rdx
lea    -0x440(%rbp),%rcx
lea    -0x444(%rbp),%r8
lea    -0x448(%rbp),%r9
lea    -0x44c(%rbp),%r14
lea    -0x450(%rbp),%rbx
lea    -0x454(%rbp),%r11
lea    -0x458(%rbp),%r10
lea    -0x45c(%rbp),%rax
mov    %r14,(%rsp)
mov    %rbx,0x8(%rsp)
mov    %r11,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   1060 <sscanf@plt>
cmp    $0x9,%eax
je     1337 <scan_shm+0x1c7>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    13ec <scan_shm+0x27c>
mov    -0x458(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    13d5 <scan_shm+0x265>
mov    -0x28(%rbp),%rax
mov    %eax,%edi
mov    -0x43c(%rbp),%edx
mov    -0x440(%rbp),%ecx
mov    -0x444(%rbp),%r8d
lea    0xd33(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x440(%rbp),%edi
mov    0x2c56(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmctl@plt>
cmp    $0x0,%rax
jge    13c3 <scan_shm+0x253>
mov    0x2c4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x460(%rbp)
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x460(%rbp),%edi
mov    %rax,%rdx
lea    0xd14(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1225 <scan_shm+0xb5>
mov    -0x438(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x480,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_shm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%r13d
lea    0xe6f(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     132c <scan_shm+0x1bc>
mov    %rax,%r12
lea    0x30(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1030 <getl@plt>
test   %rax,%rax
js     133e <scan_shm+0x1ce>
lea    0x30(%rsp),%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1030 <getl@plt>
test   %rax,%rax
js     1350 <scan_shm+0x1e0>
lea    0x30(%rsp),%r15
xor    %r14d,%r14d
jmp    1212 <scan_shm+0xa2>
nopl   0x0(%rax)
addl   $0x1,(%rbx)
mov    $0x1,%r14d
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r12,%rdx
call   1030 <getl@plt>
test   %rax,%rax
js     1353 <scan_shm+0x1e3>
movl   $0x0,0x18(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0x2c(%rsp)
movl   $0x0,0x28(%rsp)
movl   $0x0,0x24(%rsp)
movl   $0x0,0x20(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0x1c(%rsp)
sub    $0x8,%rsp
mov    %r15,%rdi
lea    0xdf9(%rip),%rsi
lea    0x20(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x1c(%rsp),%r8
lea    0x34(%rsp),%r9
lea    0x24(%rsp),%rax
push   %rax
lea    0x20(%rsp),%rax
push   %rax
lea    0x38(%rsp),%rax
push   %rax
lea    0x44(%rsp),%rax
push   %rax
lea    0x50(%rsp),%rax
push   %rax
call   1060 <sscanf@plt>
add    $0x30,%rsp
cmp    $0x9,%eax
jne    135d <scan_shm+0x1ed>
cmp    %r13d,0x10(%rsp)
jne    11f9 <scan_shm+0x89>
mov    0x18(%rsp),%edx
mov    0xc(%rsp),%ecx
mov    0x14(%rsp),%r8d
mov    %ebp,%edi
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0xc(%rsp),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <shmctl@plt>
test   %rax,%rax
jns    11f0 <scan_shm+0x80>
mov    %rbx,%r14
mov    %rbp,%rbx
mov    0x2ce2(%rip),%rax
mov    (%rax),%ebp
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rbp
mov    %r14,%rbx
lea    0xdb3(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11f0 <scan_shm+0x80>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    0xcd8(%rip),%rsi
jmp    136d <scan_shm+0x1fd>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
jmp    136d <scan_shm+0x1fd>
xor    %r14d,%r14d
mov    %r12,%rdi
call   1040 <fclose@plt>
jmp    137a <scan_shm+0x20a>
mov    0x2c7c(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/proc/sysvipc/shm\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"cannot open file '/proc/sysvipc/shm'\0A\00"
@.str.3 = [39 x i8] c"unexpected EOF in '/proc/sysvipc/shm'\0A\00"
@.str.4 = [19 x i8] c"%d%d%o%d%d%d%d%d%d\00"
@.str.5 = [37 x i8] c"format error in '/proc/sysvipc/shm'\0A\00"
@.str.6 = [55 x i8] c"shared memory: key = 0x%08x, shmid = %d, perms = %03o\0A\00"
@IPC_RMID = global i32 0
@.str.7 = [19 x i8] c"shmctl failed: %s\0A\00"
@errno = global i32 0
define i32 @scan_shm(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 0, i32* %8
%20 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %10
%21 = load i32*, i32** %10
%22 = icmp ne i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %3
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %76
26: ; preds = %3
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%28 = load i32*, i32** %10
%29 = call i64 @getl(i8* %27, i32 1024, i32* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %76
34: ; preds = %26
br label %35
35: ; preds = %71, %34
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%37 = load i32*, i32** %10
%38 = call i64 @getl(i8* %36, i32 1024, i32* %37)
%39 = icmp sge i64 %38, 0
br i1 %39, label %40, label %72
40: ; preds = %35
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
store i32 0, i32* %19
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%42 = call i32 @sscanf(i8* %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32* %11, i32* %12, i32* %13, i32* %14, i32* %15, i32* %16, i32* %17, i32* %18, i32* %19)
%43 = icmp ne i32 %42, 9
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %4
br label %76
47: ; preds = %40
%48 = load i32, i32* %18
%49 = load i32, i32* %5
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %71
51: ; preds = %47
%52 = load i32*, i32** %7
%53 = ptrtoint i32* %52 to i32
%54 = load i32, i32* %11
%55 = load i32, i32* %12
%56 = load i32, i32* %13
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 %55, i32 %56)
%58 = load i32, i32* %12
%59 = load i32, i32* @IPC_RMID
%60 = call i64 @shmctl(i32 %58, i32 %59, i32* null)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %51
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %62, %51
store i32 1, i32* %8
%68 = load i32*, i32** %6
%69 = load i32, i32* %68
%70 = add nsw i32 %69, 1
store i32 %70, i32* %68
br label %71
71: ; preds = %67, %47
br label %35
72: ; preds = %35
%73 = load i32*, i32** %10
%74 = call i32 @fclose(i32* %73)
%75 = load i32, i32* %8
store i32 %75, i32* %4
br label %76
76: ; preds = %72, %44, %31, %23
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
@.str = [18 x i8] c"/proc/sysvipc/shm\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"cannot open file '/proc/sysvipc/shm'\0A\00"
@.str.3 = [39 x i8] c"unexpected EOF in '/proc/sysvipc/shm'\0A\00"
@.str.4 = [19 x i8] c"%d%d%o%d%d%d%d%d%d\00"
@.str.5 = [37 x i8] c"format error in '/proc/sysvipc/shm'\0A\00"
@.str.6 = [55 x i8] c"shared memory: key = 0x%08x, shmid = %d, perms = %03o\0A\00"
@IPC_RMID = global i32 0
@.str.7 = [19 x i8] c"shmctl failed: %s\0A\00"
@errno = global i32 0
define i32 @scan_shm(i32 %0, i32* nocapture %1, i32* %2) {
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %75
20: ; preds = %3
%21 = call i64 @getl(i8* %14, i32 1024, i32* %15)
%22 = icmp slt i64 %21, 0
br i1 %22, label %38, label %23
23: ; preds = %20
%24 = call i64 @getl(i8* %14, i32 1024, i32* %15)
%25 = icmp sgt i64 %24, -1
br i1 %25, label %26, label %72
26: ; preds = %23
%27 = bitcast i32* %5 to i8*
%28 = bitcast i32* %6 to i8*
%29 = bitcast i32* %7 to i8*
%30 = bitcast i32* %8 to i8*
%31 = bitcast i32* %9 to i8*
%32 = bitcast i32* %10 to i8*
%33 = bitcast i32* %11 to i8*
%34 = bitcast i32* %12 to i8*
%35 = bitcast i32* %13 to i8*
%36 = ptrtoint i32* %2 to i64
%37 = trunc i64 %36 to i32
br label %41
38: ; preds = %20
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %75
41: ; preds = %26, %68
%42 = phi i32 [ 0, %26 ], [ %69, %68 ]
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%43 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32* %5, i32* %6, i32* %7, i32* %8, i32* %9, i32* %10, i32* %11, i32* %12, i32* %13)
%44 = icmp eq i32 %43, 9
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
br label %75
48: ; preds = %41
%49 = load i32, i32* %12
%50 = icmp eq i32 %49, %0
br i1 %50, label %51, label %68
51: ; preds = %48
%52 = load i32, i32* %5
%53 = load i32, i32* %6
%54 = load i32, i32* %7
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.6, i64 0, i64 0), i32 %52, i32 %53, i32 %54)
%56 = load i32, i32* %6
%57 = load i32, i32* @IPC_RMID
%58 = call i64 @shmctl(i32 %56, i32 %57, i32* null)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %51
%61 = load i32, i32* @stderr
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %63)
br label %65
65: ; preds = %60, %51
%66 = load i32, i32* %1
%67 = add nsw i32 %66, 1
store i32 %67, i32* %1
br label %68
68: ; preds = %48, %65
%69 = phi i32 [ 1, %65 ], [ %42, %48 ]
%70 = call i64 @getl(i8* %14, i32 1024, i32* %15)
%71 = icmp sgt i64 %70, -1
br i1 %71, label %41, label %72
72: ; preds = %68, %23
%73 = phi i32 [ 0, %23 ], [ %69, %68 ]
%74 = call i32 @fclose(i32* %15)
br label %75
75: ; preds = %45, %72, %38, %17
%76 = phi i32 [ 1, %38 ], [ %73, %72 ], [ 1, %17 ], [ 1, %45 ]
ret i32 %76
}