#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 spwd {
  char *sp_pwdp;
  int /*<<< orphan*/ sp_namp;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ concat(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *,
                          char *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
struct spwd *fgetspent(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ ftruncate(int /*<<< orphan*/, int /*<<< orphan*/);
int putspent(struct spwd *, int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *tmpfile();
int /*<<< orphan*/ weprintf(char *);
int spw_write_file(FILE *fp, const struct spwd *spw, char *pwhash) {
  struct spwd *spwent;
  int r = -1, w = 0;
  FILE *tfp = NULL;
  tfp = tmpfile();
  if (!tfp) {
    weprintf("tmpfile:");
    goto cleanup;
  }
  while ((spwent = fgetspent(fp))) {
    if (strcmp(spwent->sp_namp, spw->sp_namp) == 0) {
      spwent->sp_pwdp = pwhash;
      w++;
    }
    errno = 0;
    if (putspent(spwent, tfp) == -1) {
      weprintf("putspent:");
      goto cleanup;
    }
  }
  if (!w) {
    weprintf("shadow: no matching entry to write to\n");
    goto cleanup;
  }
  fflush(tfp);
  if (fseek(fp, 0, SEEK_SET) == -1 || fseek(tfp, 0, SEEK_SET) == -1) {
    weprintf("fseek:");
    goto cleanup;
  }
  concat(tfp, "tmpfile", fp, "shadow");
  ftruncate(fileno(fp), ftell(tfp));
  r = 0;
cleanup:
  if (tfp)
    fclose(tfp);
  return r;
}


x86 assembly (clang, -O0):
<spw_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1070 <tmpfile@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1211 <spw_write_file+0x51>
lea    0xdf9(%rip),%rdi
call   10c0 <weprintf@plt>
jmp    1348 <spw_write_file+0x188>
jmp    1216 <spw_write_file+0x56>
mov    -0x8(%rbp),%rdi
call   10e0 <fgetspent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1298 <spw_write_file+0xd8>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    125e <spw_write_file+0x9e>
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    0x2d7b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <putspent@plt>
cmp    $0xffffffff,%eax
jne    1293 <spw_write_file+0xd3>
lea    0xd80(%rip),%rdi
call   10c0 <weprintf@plt>
jmp    1348 <spw_write_file+0x188>
jmp    1216 <spw_write_file+0x56>
cmpl   $0x0,-0x28(%rbp)
jne    12b3 <spw_write_file+0xf3>
lea    0xd6a(%rip),%rdi
call   10c0 <weprintf@plt>
jmp    1348 <spw_write_file+0x188>
mov    -0x30(%rbp),%rdi
call   10b0 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    0x2d11(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
cmp    $0xffffffff,%eax
je     12f6 <spw_write_file+0x136>
mov    -0x30(%rbp),%rdi
mov    0x2cf4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
cmp    $0xffffffff,%eax
jne    1307 <spw_write_file+0x147>
lea    0xd3d(%rip),%rdi
call   10c0 <weprintf@plt>
jmp    1348 <spw_write_file+0x188>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xd2b(%rip),%rsi
lea    0xd2c(%rip),%rcx
call   1060 <concat@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <ftell@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%esi
call   1050 <ftruncate@plt>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     135c <spw_write_file+0x19c>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spw_write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%r13
mov    %rdi,%r12
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <tmpfile@plt>
test   %rax,%rax
je     1238 <spw_write_file+0x78>
mov    %rax,%r14
mov    0x2df3(%rip),%r15
jmp    120b <spw_write_file+0x4b>
nop
movq   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <putspent@plt>
cmp    $0xffffffff,%eax
je     12bb <spw_write_file+0xfb>
mov    %r12,%rdi
call   10e0 <fgetspent@plt>
test   %rax,%rax
je     124e <spw_write_file+0x8e>
mov    %rax,%rbx
mov    0x8(%rax),%edi
mov    0x8(%r13),%esi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11f0 <spw_write_file+0x30>
mov    (%rsp),%rax
mov    %rax,(%rbx)
add    $0x1,%ebp
jmp    11f0 <spw_write_file+0x30>
lea    0xdc1(%rip),%rdi
call   10c0 <weprintf@plt>
mov    $0xffffffff,%ebx
jmp    12e6 <spw_write_file+0x126>
test   %ebp,%ebp
je     12c4 <spw_write_file+0x104>
mov    %r14,%rdi
call   10b0 <fflush@plt>
mov    0x2d77(%rip),%rbx
mov    (%rbx),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
cmp    $0xffffffff,%eax
je     12cd <spw_write_file+0x10d>
mov    (%rbx),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
cmp    $0xffffffff,%eax
je     12cd <spw_write_file+0x10d>
lea    0xdb5(%rip),%rsi
lea    0xdb6(%rip),%rcx
mov    %r14,%rdi
mov    %r12,%rdx
call   1060 <concat@plt>
mov    %r12,%rdi
call   10a0 <fileno@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1090 <ftell@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <ftruncate@plt>
jmp    12de <spw_write_file+0x11e>
lea    0xd47(%rip),%rdi
jmp    12d4 <spw_write_file+0x114>
lea    0xd48(%rip),%rdi
jmp    12d4 <spw_write_file+0x114>
lea    0xd66(%rip),%rdi
call   10c0 <weprintf@plt>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.spwd = type { i8*, i32 }
@.str = [9 x i8] c"tmpfile:\00"
@errno = global i64 0
@.str.1 = [10 x i8] c"putspent:\00"
@.str.2 = [39 x i8] c"shadow: no matching entry to write to\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"fseek:\00"
@.str.4 = [8 x i8] c"tmpfile\00"
@.str.5 = [7 x i8] c"shadow\00"
define i32 @spw_write_file(i32* %0, %struct.spwd* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca %struct.spwd*
%6 = alloca i8*
%7 = alloca %struct.spwd*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32* %0, i32** %4
store %struct.spwd* %1, %struct.spwd** %5
store i8* %2, i8** %6
store i32 -1, i32* %8
store i32 0, i32* %9
store i32* null, i32** %10
%11 = call i32* (...) @tmpfile()
store i32* %11, i32** %10
%12 = load i32*, i32** %10
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 @weprintf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %72
16: ; preds = %3
br label %17
17: ; preds = %43, %16
%18 = load i32*, i32** %4
%19 = call %struct.spwd* @fgetspent(i32* %18)
store %struct.spwd* %19, %struct.spwd** %7
%20 = icmp ne %struct.spwd* %19, null
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = load %struct.spwd*, %struct.spwd** %7
%23 = getelementptr inbounds %struct.spwd, %struct.spwd* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load %struct.spwd*, %struct.spwd** %5
%26 = getelementptr inbounds %struct.spwd, %struct.spwd* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i64 @strcmp(i32 %24, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i8*, i8** %6
%32 = load %struct.spwd*, %struct.spwd** %7
%33 = getelementptr inbounds %struct.spwd, %struct.spwd* %32, i32 0, i32 0
store i8* %31, i8** %33
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %36
36: ; preds = %30, %21
store i64 0, i64* @errno
%37 = load %struct.spwd*, %struct.spwd** %7
%38 = load i32*, i32** %10
%39 = call i32 @putspent(%struct.spwd* %37, i32* %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %72
43: ; preds = %36
br label %17
44: ; preds = %17
%45 = load i32, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = call i32 @weprintf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %72
49: ; preds = %44
%50 = load i32*, i32** %10
%51 = call i32 @fflush(i32* %50)
%52 = load i32*, i32** %4
%53 = load i32, i32* @SEEK_SET
%54 = call i32 @fseek(i32* %52, i32 0, i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %61, label %56
56: ; preds = %49
%57 = load i32*, i32** %10
%58 = load i32, i32* @SEEK_SET
%59 = call i32 @fseek(i32* %57, i32 0, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %56, %49
%62 = call i32 @weprintf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %72
63: ; preds = %56
%64 = load i32*, i32** %10
%65 = load i32*, i32** %4
%66 = call i32 @concat(i32* %64, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* %65, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%67 = load i32*, i32** %4
%68 = call i32 @fileno(i32* %67)
%69 = load i32*, i32** %10
%70 = call i32 @ftell(i32* %69)
%71 = call i32 @ftruncate(i32 %68, i32 %70)
store i32 0, i32* %8
br label %72
72: ; preds = %63, %61, %47, %41, %14
%73 = load i32*, i32** %10
%74 = icmp ne i32* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32*, i32** %10
%77 = call i32 @fclose(i32* %76)
br label %78
78: ; preds = %75, %72
%79 = load i32, i32* %8
ret i32 %79
}

LLVM IR (-O2):
%struct.spwd = type { i8*, i32 }
@.str = [9 x i8] c"tmpfile:\00"
@errno = global i64 0
@.str.1 = [10 x i8] c"putspent:\00"
@.str.2 = [39 x i8] c"shadow: no matching entry to write to\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"fseek:\00"
@.str.4 = [8 x i8] c"tmpfile\00"
@.str.5 = [7 x i8] c"shadow\00"
define i32 @spw_write_file(i32* %0, %struct.spwd* nocapture %1, i8* %2) {
%4 = tail call i32* (...) @tmpfile()
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = getelementptr inbounds %struct.spwd, %struct.spwd* %1, i64 0, i32 1
br label %10
8: ; preds = %3
%9 = tail call i32 @weprintf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %52
10: ; preds = %6, %23
%11 = phi i32 [ %24, %23 ], [ 0, %6 ]
%12 = tail call %struct.spwd* @fgetspent(i32* %0)
%13 = icmp eq %struct.spwd* %12, null
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = getelementptr inbounds %struct.spwd, %struct.spwd* %12, i64 0, i32 1
%16 = load i32, i32* %15
%17 = load i32, i32* %7
%18 = tail call i64 @strcmp(i32 %16, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = getelementptr inbounds %struct.spwd, %struct.spwd* %12, i64 0, i32 0
store i8* %2, i8** %21
%22 = add nsw i32 %11, 1
br label %23
23: ; preds = %20, %14
%24 = phi i32 [ %22, %20 ], [ %11, %14 ]
store i64 0, i64* @errno
%25 = tail call i32 @putspent(%struct.spwd* %12, i32* %4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %10
27: ; preds = %23
%28 = tail call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %49
29: ; preds = %10
%30 = icmp eq i32 %11, 0
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = tail call i32 @weprintf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %49
33: ; preds = %29
%34 = tail call i32 @fflush(i32* %4)
%35 = load i32, i32* @SEEK_SET
%36 = tail call i32 @fseek(i32* %0, i32 0, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i32, i32* @SEEK_SET
%40 = tail call i32 @fseek(i32* %4, i32 0, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %38, %33
%43 = tail call i32 @weprintf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %49
44: ; preds = %38
%45 = tail call i32 @concat(i32* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%46 = tail call i32 @fileno(i32* %0)
%47 = tail call i32 @ftell(i32* %4)
%48 = tail call i32 @ftruncate(i32 %46, i32 %47)
br label %49
49: ; preds = %27, %31, %42, %44
%50 = phi i32 [ -1, %27 ], [ -1, %42 ], [ 0, %44 ], [ -1, %31 ]
%51 = tail call i32 @fclose(i32* %4)
br label %52
52: ; preds = %8, %49
%53 = phi i32 [ -1, %8 ], [ %50, %49 ]
ret i32 %53
}