#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__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                  int /*<<< orphan*/ *);
int sscanf(char *, char *, int *, int *, int *, int *, int *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int scan_sem(int search_uid, int *p_count, FILE *rep_f) {
  int retval = 0;
  char buf[1024];
  FILE *f = fopen("/proc/sysvipc/sem", "r");
  if (!f) {
    fprintf(stderr, "cannot open file '/proc/sysvipc/sem'\n");
    return 1;
  }
  if (getl(buf, sizeof(buf), f) < 0) {
    fprintf(stderr, "unexpected EOF in '/proc/sysvipc/sem'\n");
    return 1;
  }
  while (getl(buf, sizeof(buf), f) >= 0) {
    int key = 0, semid = 0, perms = 0, nsems = 0, uid = 0, gid = 0;
    if (sscanf(buf, "%d%d%o%d%d%d", &key, &semid, &perms, &nsems, &uid, &gid) !=
        6) {
      fprintf(stderr, "format error in '/proc/sysvipc/sem'\n");
      return 1;
    }
    if (uid == search_uid) {
      fprintf(rep_f,
              "semaphore array: key = 0x%08x, semid = %d, perms = %03o\n", key,
              semid, perms);
      if (semctl(semid, 0, IPC_RMID, NULL) < 0) {
        fprintf(stderr, "semctl failed: %s\n", strerror(errno));
      }
      retval = 1;
      ++*p_count;
    }
  }
  fclose(f);
  return retval;
}


x86 assembly (clang, -O0):
<scan_sem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe6c(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11d8 <scan_sem+0x68>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a9 <scan_sem+0x239>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1030 <getl@plt>
cmp    $0x0,%rax
jge    121d <scan_sem+0xad>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a9 <scan_sem+0x239>
jmp    1222 <scan_sem+0xb2>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1030 <getl@plt>
cmp    $0x0,%rax
jl     1397 <scan_sem+0x227>
movl   $0x0,-0x42c(%rbp)
movl   $0x0,-0x430(%rbp)
movl   $0x0,-0x434(%rbp)
movl   $0x0,-0x438(%rbp)
movl   $0x0,-0x43c(%rbp)
movl   $0x0,-0x440(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xdd3(%rip),%rsi
lea    -0x42c(%rbp),%rdx
lea    -0x430(%rbp),%rcx
lea    -0x434(%rbp),%r8
lea    -0x438(%rbp),%r9
lea    -0x43c(%rbp),%r10
lea    -0x440(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <sscanf@plt>
cmp    $0x6,%eax
je     12f2 <scan_sem+0x182>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a9 <scan_sem+0x239>
mov    -0x43c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1392 <scan_sem+0x222>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x42c(%rbp),%edx
mov    -0x430(%rbp),%ecx
mov    -0x434(%rbp),%r8d
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x430(%rbp),%edi
mov    0x2c9b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <semctl@plt>
cmp    $0x0,%rax
jge    1380 <scan_sem+0x210>
mov    0x2c8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    %rax,%rdx
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1222 <scan_sem+0xb2>
mov    -0x428(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_sem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rdx,%rbp
mov    %rsi,0x18(%rsp)
mov    %edi,%r13d
lea    0xe6d(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12f8 <scan_sem+0x188>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1030 <getl@plt>
test   %rax,%rax
js     130a <scan_sem+0x19a>
lea    0x20(%rsp),%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1030 <getl@plt>
test   %rax,%rax
js     131c <scan_sem+0x1ac>
lea    0x20(%rsp),%r15
lea    0x14(%rsp),%r12
xor    %r14d,%r14d
jmp    1217 <scan_sem+0xa7>
mov    0x18(%rsp),%rax
addl   $0x1,(%rax)
mov    $0x1,%r14d
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1030 <getl@plt>
test   %rax,%rax
js     131f <scan_sem+0x1af>
movl   $0x0,0xc(%rsp)
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0x4(%rsp)
movl   $0x0,0x10(%rsp)
mov    %r15,%rdi
lea    0xe11(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rsp,%rcx
lea    0x8(%rsp),%r8
mov    %r12,%r9
lea    0x10(%rsp),%rax
push   %rax
lea    0xc(%rsp),%rax
push   %rax
call   1060 <sscanf@plt>
add    $0x10,%rsp
cmp    $0x6,%eax
jne    1329 <scan_sem+0x1b9>
cmp    %r13d,0x4(%rsp)
jne    11fe <scan_sem+0x8e>
mov    0xc(%rsp),%edx
mov    (%rsp),%ecx
mov    0x8(%rsp),%r8d
mov    %ebp,%edi
lea    0xdf5(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rsp),%edi
mov    0x2d21(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <semctl@plt>
test   %rax,%rax
jns    11f0 <scan_sem+0x80>
mov    %rbp,%r14
mov    0x2d13(%rip),%rax
mov    (%rax),%ebp
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %ebp,%edi
mov    %r14,%rbp
lea    0xde3(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11f0 <scan_sem+0x80>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd0c(%rip),%rsi
jmp    1339 <scan_sem+0x1c9>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
jmp    1339 <scan_sem+0x1c9>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1346 <scan_sem+0x1d6>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xd35(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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