#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 group {
  char *gr_name;
  char *gr_passwd;
  int gr_gid;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ endgrent();
size_t errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *, ...);
struct group *getgrent();
int getpid();
int /*<<< orphan*/ setgrent();
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ stderr;
char **sys_errlist;
void dump_grent(char *id) {
  struct group *gr;
  char fname[255];
  FILE *fptr;
  sprintf(fname, "/tmp/getgrent_r-%s.out-%d", id, getpid());
  if ((fptr = fopen(fname, "w")) < 0) {
    fprintf(stderr, "ERROR: could not open file %s: %s\n", fname,
            sys_errlist[errno]);
    return;
  }
  setgrent();
  while ((gr = getgrent()) != NULL) {
    fprintf(fptr, "%s:%s:%d:%d\n", gr->gr_name, gr->gr_passwd, gr->gr_gid);
  }
  endgrent();
  fclose(fptr);
}


x86 assembly (clang, -O0):
<dump_grent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x128(%rbp),%rdi
mov    -0x120(%rbp),%rdx
mov    %eax,%ecx
lea    0xe3a(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jae    122b <dump_grent+0xab>
mov    0x2de2(%rip),%rax
movslq (%rax),%rdi
lea    -0x110(%rbp),%rdx
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    128b <dump_grent+0x10b>
mov    $0x0,%al
call   1070 <setgrent@plt>
mov    $0x0,%al
call   1040 <getgrent@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1278 <dump_grent+0xf8>
mov    -0x118(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%r8d
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1232 <dump_grent+0xb2>
mov    $0x0,%al
call   1030 <endgrent@plt>
mov    -0x118(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_grent>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe65(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %eax,%ecx
call   10a0 <sprintf@plt>
lea    0xe68(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <setgrent@plt>
xor    %eax,%eax
call   1040 <getgrent@plt>
test   %rax,%rax
je     1204 <dump_grent+0x84>
lea    0xe45(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%rax),%rdx
mov    0x8(%rax),%rcx
mov    0x10(%rax),%r8d
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1040 <getgrent@plt>
test   %rax,%rax
jne    11e0 <dump_grent+0x60>
xor    %eax,%eax
call   1030 <endgrent@plt>
mov    %rbx,%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r14
jmp    1060 <fclose@plt>

LLVM IR (-O0):
%struct.group = type { i8*, i8*, i32 }
@.str = [26 x i8] c"/tmp/getgrent_r-%s.out-%d\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"ERROR: could not open file %s: %s\0A\00"
@sys_errlist = global i8** null
@errno = global i64 0
@.str.3 = [13 x i8] c"%s:%s:%d:%d\0A\00"
define void @dump_grent(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.group*
%4 = alloca [255 x i8]
%5 = alloca i32*
store i8* %0, i8** %2
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 (...) @getpid()
%9 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %8)
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = icmp ult i32* %11, null
br i1 %12, label %13, label %23
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i32*
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%18 = load i8**, i8*** @sys_errlist
%19 = load i64, i64* @errno
%20 = getelementptr inbounds i8*, i8** %18, i64 %19
%21 = load i8*, i8** %20
%22 = call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %17, i8* %21)
br label %44
23: ; preds = %1
%24 = call i32 (...) @setgrent()
br label %25
25: ; preds = %28, %23
%26 = call %struct.group* (...) @getgrent()
store %struct.group* %26, %struct.group** %3
%27 = icmp ne %struct.group* %26, null
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = load i32*, i32** %5
%30 = load %struct.group*, %struct.group** %3
%31 = getelementptr inbounds %struct.group, %struct.group* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = load %struct.group*, %struct.group** %3
%34 = getelementptr inbounds %struct.group, %struct.group* %33, i32 0, i32 1
%35 = load i8*, i8** %34
%36 = load %struct.group*, %struct.group** %3
%37 = getelementptr inbounds %struct.group, %struct.group* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %35, i32 %38)
br label %25
40: ; preds = %25
%41 = call i32 (...) @endgrent()
%42 = load i32*, i32** %5
%43 = call i32 @fclose(i32* %42)
br label %44
44: ; preds = %40, %13
ret void
}

LLVM IR (-O2):
%struct.group = type { i8*, i8*, i32 }
@.str = [26 x i8] c"/tmp/getgrent_r-%s.out-%d\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@sys_errlist = global i8** null
@errno = global i64 0
@.str.3 = [13 x i8] c"%s:%s:%d:%d\0A\00"
define void @dump_grent(i8* %0) {
%2 = alloca [255 x i8]
%3 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %4)
%6 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 (...) @setgrent()
%8 = tail call %struct.group* (...) @getgrent()
%9 = icmp eq %struct.group* %8, null
br i1 %9, label %21, label %10
10: ; preds = %1, %10
%11 = phi %struct.group* [ %19, %10 ], [ %8, %1 ]
%12 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds %struct.group, %struct.group* %11, i64 0, i32 2
%17 = load i32, i32* %16
%18 = tail call i32 (i32*, i8*, i8*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %13, i8* %15, i32 %17)
%19 = tail call %struct.group* (...) @getgrent()
%20 = icmp eq %struct.group* %19, null
br i1 %20, label %21, label %10
21: ; preds = %10, %1
%22 = tail call i32 (...) @endgrent()
%23 = tail call i32 @fclose(i32* %6)
ret void
}