#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 sgrp {
  char *sg_namp;
  char *sg_passwd;
  char **sg_adm;
  char **sg_mem;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ _IO_flockfile(int /*<<< orphan*/ *);
int /*<<< orphan*/ _IO_funlockfile(int /*<<< orphan*/ *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, char *, char *);
scalar_t__ putc_unlocked(char, int /*<<< orphan*/ *);
int putsgent(const struct sgrp *g, FILE *stream) {
  int errors = 0;
  _IO_flockfile(stream);
  if (fprintf(stream, "%s:%s:", g->sg_namp, g->sg_passwd ? g->sg_passwd : "") <
      0)
    ++errors;
  bool first = true;
  char **sp = g->sg_adm;
  if (sp != NULL)
    while (*sp != NULL) {
      if (fprintf(stream, "%s%s", first ? "" : ",", *sp++) < 0) {
        ++errors;
        break;
      }
      first = false;
    }
  if (putc_unlocked(':', stream) == EOF)
    ++errors;
  first = true;
  sp = g->sg_mem;
  if (sp != NULL)
    while (*sp != NULL) {
      if (fprintf(stream, "%s%s", first ? "" : ",", *sp++) < 0) {
        ++errors;
        break;
      }
      first = false;
    }
  if (putc_unlocked('\n', stream) == EOF)
    ++errors;
  _IO_funlockfile(stream);
  return errors ? -1 : 0;
}


x86 assembly (clang, -O0):
<putsgent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <_IO_flockfile@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1193 <putsgent+0x53>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    11a3 <putsgent+0x63>
lea    0xe73(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11a3 <putsgent+0x63>
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rcx
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    11ce <putsgent+0x8e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1262 <putsgent+0x122>
jmp    11f1 <putsgent+0xb1>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     125d <putsgent+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%ecx
lea    0xdff(%rip),%rdx
lea    0xdf9(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%rcx
lea    0xdd3(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    1251 <putsgent+0x111>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    125d <putsgent+0x11d>
movl   $0x0,-0x18(%rbp)
jmp    11f1 <putsgent+0xb1>
jmp    1262 <putsgent+0x122>
mov    -0x10(%rbp),%rsi
mov    $0x3a,%edi
call   1060 <putc_unlocked@plt>
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
jne    1289 <putsgent+0x149>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     131d <putsgent+0x1dd>
jmp    12ac <putsgent+0x16c>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1318 <putsgent+0x1d8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%ecx
lea    0xd44(%rip),%rdx
lea    0xd3e(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%rcx
lea    0xd18(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    130c <putsgent+0x1cc>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1318 <putsgent+0x1d8>
movl   $0x0,-0x18(%rbp)
jmp    12ac <putsgent+0x16c>
jmp    131d <putsgent+0x1dd>
mov    -0x10(%rbp),%rsi
mov    $0xa,%edi
call   1060 <putc_unlocked@plt>
mov    0x2cb6(%rip),%rcx
cmp    (%rcx),%rax
jne    1344 <putsgent+0x204>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <_IO_funlockfile@plt>
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putsgent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <_IO_flockfile@plt>
mov    (%r14),%rdx
mov    0x8(%r14),%rcx
test   %rcx,%rcx
lea    0xea3(%rip),%rbx
cmove  %rbx,%rcx
lea    0xe8b(%rip),%rsi
mov    %r12,%rdi
call   1050 <fprintf@plt>
mov    %rax,%r15
shr    $0x3f,%r15
mov    0x10(%r14),%rbp
test   %rbp,%rbp
je     11dd <putsgent+0x9d>
mov    0x0(%rbp),%rcx
test   %rcx,%rcx
je     11dd <putsgent+0x9d>
mov    %r14,(%rsp)
add    $0x8,%rbp
lea    0xe62(%rip),%r13
lea    0xe60(%rip),%r14
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
test   %rax,%rax
js     11d5 <putsgent+0x95>
mov    0x0(%rbp),%rcx
add    $0x8,%rbp
mov    %r14,%rbx
test   %rcx,%rcx
jne    11b0 <putsgent+0x70>
jmp    11d9 <putsgent+0x99>
add    $0x1,%r15d
mov    (%rsp),%r14
mov    $0x3a,%edi
mov    %r12,%rsi
call   1060 <putc_unlocked@plt>
mov    0x2df7(%rip),%rdx
xor    %r13d,%r13d
cmp    (%rdx),%rax
sete   %r13b
add    %r15d,%r13d
mov    0x18(%r14),%rbp
test   %rbp,%rbp
je     1252 <putsgent+0x112>
mov    0x0(%rbp),%rcx
mov    %rdx,%r15
test   %rcx,%rcx
je     125b <putsgent+0x11b>
add    $0x8,%rbp
lea    0xdef(%rip),%rdx
lea    0xde2(%rip),%rbx
lea    0xde0(%rip),%r14
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <fprintf@plt>
test   %rax,%rax
js     1257 <putsgent+0x117>
mov    0x0(%rbp),%rcx
add    $0x8,%rbp
mov    %r14,%rdx
test   %rcx,%rcx
jne    1230 <putsgent+0xf0>
jmp    125b <putsgent+0x11b>
mov    %rdx,%r15
jmp    125b <putsgent+0x11b>
add    $0x1,%r13d
mov    $0xa,%edi
mov    %r12,%rsi
call   1060 <putc_unlocked@plt>
xor    %ebx,%ebx
cmp    (%r15),%rax
sete   %bl
neg    %ebx
mov    %r12,%rdi
call   1030 <_IO_funlockfile@plt>
xor    %eax,%eax
cmp    %ebx,%r13d
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sgrp = type { i8*, i8*, i8**, i8** }
@.str = [7 x i8] c"%s:%s:\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [2 x i8] c",\00"
@EOF = global i64 0
define i32 @putsgent(%struct.sgrp* %0, i32* %1) {
%3 = alloca %struct.sgrp*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
store %struct.sgrp* %0, %struct.sgrp** %3
store i32* %1, i32** %4
store i32 0, i32* %5
%8 = load i32*, i32** %4
%9 = call i32 @_IO_flockfile(i32* %8)
%10 = load i32*, i32** %4
%11 = load %struct.sgrp*, %struct.sgrp** %3
%12 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %11, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = load %struct.sgrp*, %struct.sgrp** %3
%15 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %14, i32 0, i32 1
%16 = load i8*, i8** %15
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load %struct.sgrp*, %struct.sgrp** %3
%20 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %19, i32 0, i32 1
%21 = load i8*, i8** %20
br label %23
22: ; preds = %2
br label %23
23: ; preds = %22, %18
%24 = phi i8* [ %21, %18 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %22 ]
%25 = call i64 @fprintf(i32* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %30
30: ; preds = %27, %23
store i32 1, i32* %6
%31 = load %struct.sgrp*, %struct.sgrp** %3
%32 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %31, i32 0, i32 2
%33 = load i8**, i8*** %32
store i8** %33, i8*** %7
%34 = load i8**, i8*** %7
%35 = icmp ne i8** %34, null
br i1 %35, label %36, label %57
36: ; preds = %30
br label %37
37: ; preds = %55, %36
%38 = load i8**, i8*** %7
%39 = load i8*, i8** %38
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %56
41: ; preds = %37
%42 = load i32*, i32** %4
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%47 = load i8**, i8*** %7
%48 = getelementptr inbounds i8*, i8** %47, i32 1
store i8** %48, i8*** %7
%49 = load i8*, i8** %47
%50 = call i64 @fprintf(i32* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %41
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %56
55: ; preds = %41
store i32 0, i32* %6
br label %37
56: ; preds = %52, %37
br label %57
57: ; preds = %56, %30
%58 = load i32*, i32** %4
%59 = call i64 @putc_unlocked(i8 signext 58, i32* %58)
%60 = load i64, i64* @EOF
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
store i32 %64, i32* %5
br label %65
65: ; preds = %62, %57
store i32 1, i32* %6
%66 = load %struct.sgrp*, %struct.sgrp** %3
%67 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %66, i32 0, i32 3
%68 = load i8**, i8*** %67
store i8** %68, i8*** %7
%69 = load i8**, i8*** %7
%70 = icmp ne i8** %69, null
br i1 %70, label %71, label %92
71: ; preds = %65
br label %72
72: ; preds = %90, %71
%73 = load i8**, i8*** %7
%74 = load i8*, i8** %73
%75 = icmp ne i8* %74, null
br i1 %75, label %76, label %91
76: ; preds = %72
%77 = load i32*, i32** %4
%78 = load i32, i32* %6
%79 = icmp ne i32 %78, 0
%80 = zext i1 %79 to i64
%81 = select i1 %79, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%82 = load i8**, i8*** %7
%83 = getelementptr inbounds i8*, i8** %82, i32 1
store i8** %83, i8*** %7
%84 = load i8*, i8** %82
%85 = call i64 @fprintf(i32* %77, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %81, i8* %84)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %76
%88 = load i32, i32* %5
%89 = add nsw i32 %88, 1
store i32 %89, i32* %5
br label %91
90: ; preds = %76
store i32 0, i32* %6
br label %72
91: ; preds = %87, %72
br label %92
92: ; preds = %91, %65
%93 = load i32*, i32** %4
%94 = call i64 @putc_unlocked(i8 signext 10, i32* %93)
%95 = load i64, i64* @EOF
%96 = icmp eq i64 %94, %95
br i1 %96, label %97, label %100
97: ; preds = %92
%98 = load i32, i32* %5
%99 = add nsw i32 %98, 1
store i32 %99, i32* %5
br label %100
100: ; preds = %97, %92
%101 = load i32*, i32** %4
%102 = call i32 @_IO_funlockfile(i32* %101)
%103 = load i32, i32* %5
%104 = icmp ne i32 %103, 0
%105 = zext i1 %104 to i64
%106 = select i1 %104, i32 -1, i32 0
ret i32 %106
}

LLVM IR (-O2):
%struct.sgrp = type { i8*, i8*, i8**, i8** }
@.str = [7 x i8] c"%s:%s:\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [5 x i8] c"%s%s\00"
@.str.3 = [2 x i8] c",\00"
@EOF = global i64 0
define i32 @putsgent(%struct.sgrp* nocapture %0, i32* %1) {
%3 = tail call i32 @_IO_flockfile(i32* %1)
%4 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %0, i64 0, i32 0
%5 = load i8*, i8** %4
%6 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %0, i64 0, i32 1
%7 = load i8*, i8** %6
%8 = icmp eq i8* %7, null
%9 = select i1 %8, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %7
%10 = tail call i64 @fprintf(i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %9)
%11 = lshr i64 %10, 63
%12 = trunc i64 %11 to i32
%13 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %0, i64 0, i32 2
%14 = load i8**, i8*** %13
%15 = icmp eq i8** %14, null
br i1 %15, label %31, label %16
16: ; preds = %2
%17 = load i8*, i8** %14
%18 = icmp eq i8* %17, null
br i1 %18, label %31, label %22
19: ; preds = %22
%20 = load i8*, i8** %28
%21 = icmp eq i8* %20, null
br i1 %21, label %31, label %22
22: ; preds = %16, %19
%23 = phi i8* [ %20, %19 ], [ %17, %16 ]
%24 = phi i8** [ %28, %19 ], [ %14, %16 ]
%25 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), %19 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %16 ]
%26 = tail call i64 @fprintf(i32* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %23)
%27 = icmp slt i64 %26, 0
%28 = getelementptr inbounds i8*, i8** %24, i64 1
br i1 %27, label %29, label %19
29: ; preds = %22
%30 = add nuw nsw i32 %12, 1
br label %31
31: ; preds = %19, %16, %29, %2
%32 = phi i32 [ %30, %29 ], [ %12, %2 ], [ %12, %16 ], [ %12, %19 ]
%33 = tail call i64 @putc_unlocked(i8 signext 58, i32* %1)
%34 = load i64, i64* @EOF
%35 = icmp eq i64 %33, %34
%36 = zext i1 %35 to i32
%37 = add nsw i32 %32, %36
%38 = getelementptr inbounds %struct.sgrp, %struct.sgrp* %0, i64 0, i32 3
%39 = load i8**, i8*** %38
%40 = icmp eq i8** %39, null
br i1 %40, label %56, label %41
41: ; preds = %31
%42 = load i8*, i8** %39
%43 = icmp eq i8* %42, null
br i1 %43, label %56, label %47
44: ; preds = %47
%45 = load i8*, i8** %53
%46 = icmp eq i8* %45, null
br i1 %46, label %56, label %47
47: ; preds = %41, %44
%48 = phi i8* [ %45, %44 ], [ %42, %41 ]
%49 = phi i8** [ %53, %44 ], [ %39, %41 ]
%50 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), %44 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %41 ]
%51 = tail call i64 @fprintf(i32* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %50, i8* %48)
%52 = icmp slt i64 %51, 0
%53 = getelementptr inbounds i8*, i8** %49, i64 1
br i1 %52, label %54, label %44
54: ; preds = %47
%55 = add nsw i32 %37, 1
br label %56
56: ; preds = %44, %41, %54, %31
%57 = phi i32 [ %55, %54 ], [ %37, %31 ], [ %37, %41 ], [ %37, %44 ]
%58 = tail call i64 @putc_unlocked(i8 signext 10, i32* %1)
%59 = load i64, i64* @EOF
%60 = icmp eq i64 %58, %59
%61 = sext i1 %60 to i32
%62 = tail call i32 @_IO_funlockfile(i32* %1)
%63 = icmp ne i32 %57, %61
%64 = sext i1 %63 to i32
ret i32 %64
}