#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*/ FILE;

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setenv(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
char *strtok_r(char *, char *, char **);
int setup_environment(const char *to_user, FILE *fp) {
  int ret = 1;
  char *string;
  char *token;
  char *subtoken;
  char *saveptr1 = NULL;
  char *saveptr2 = NULL;
  char buf[4096];
  char user[13];
  string = malloc(4096);
  if (!string) {
    perror("malloc string");
    ret = 0;
    goto out;
  }
  while (fgets(buf, 4096, fp)) {
    memset(string, 0, 4096);
    sscanf(buf, "%12s\t%4095s[^\n]", user, string);
    if (strcmp(user, to_user) == 0) {
      token = strtok_r(string, ",", &saveptr1);
      while (token != NULL) {
        char *env;
        char *value;
        subtoken = strtok_r(token, "=", &saveptr2);
        env = subtoken;
        subtoken = strtok_r(NULL, "=", &saveptr2);
        value = subtoken;
        if (setenv(env, value, 1) != 0) {
          fprintf(stderr,
                  "setenv: %s: can't set environment "
                  "variable\n",
                  env);
          ;
          ret = 0;
          goto out;
        }
        token = strtok_r(NULL, ",", &saveptr1);
      }
      break;
    }
  }
  free(string);
out:
  return ret;
}


x86 assembly (clang, -O0):
<setup_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x1000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fb <setup_environment+0x5b>
lea    0xe16(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x0,-0x14(%rbp)
jmp    134a <setup_environment+0x1aa>
jmp    1200 <setup_environment+0x60>
lea    -0x1040(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1341 <setup_environment+0x1a1>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x1040(%rbp),%rdi
lea    -0x104d(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xdc6(%rip),%rsi
call   10b0 <sscanf@plt>
lea    -0x104d(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    133c <setup_environment+0x19c>
mov    -0x20(%rbp),%rdi
lea    0xdac(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1337 <setup_environment+0x197>
mov    -0x28(%rbp),%rdi
lea    0xd8b(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x1058(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd68(%rip),%rsi
lea    -0x40(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x1060(%rbp)
mov    -0x1058(%rbp),%rdi
mov    -0x1060(%rbp),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
cmp    $0x0,%rax
je     131a <setup_environment+0x17a>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    -0x1058(%rbp),%rdx
lea    0xd19(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
jmp    134a <setup_environment+0x1aa>
xor    %eax,%eax
mov    %eax,%edi
lea    0xcf9(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
jmp    127f <setup_environment+0xdf>
jmp    1341 <setup_environment+0x1a1>
jmp    1200 <setup_environment+0x60>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_environment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    $0x1000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12f0 <setup_environment+0x150>
mov    %rax,%rbx
lea    0x20(%rsp),%r12
lea    0xe24(%rip),%r13
lea    0x13(%rsp),%rbp
nop
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12b5 <setup_environment+0x115>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   10b0 <sscanf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11f0 <setup_environment+0x50>
lea    0xdde(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1060 <strtok_r@plt>
test   %rax,%rax
je     12b5 <setup_environment+0x115>
lea    0xdc9(%rip),%r14
lea    0x8(%rsp),%r15
lea    0xdbb(%rip),%r12
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <strtok_r@plt>
mov    %rax,%rbp
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <strtok_r@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
test   %rax,%rax
jne    12d4 <setup_environment+0x134>
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <strtok_r@plt>
test   %rax,%rax
jne    1270 <setup_environment+0xd0>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
mov    %rbp,%rdx
call   1090 <fprintf@plt>
xor    %eax,%eax
jmp    12c2 <setup_environment+0x122>
lea    0xd09(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
jmp    12c2 <setup_environment+0x122>

LLVM IR (-O0):
@.str = [14 x i8] c"malloc string\00"
@.str.1 = [16 x i8] c"%12s\09%4095s[^\0A]\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [2 x i8] c"=\00"
@stderr = global i32 0
@.str.4 = [44 x i8] c"setenv: %s: can't set environment variable\0A\00"
define i32 @setup_environment(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [4096 x i8]
%12 = alloca [13 x i8]
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 1, i32* %5
store i8* null, i8** %9
store i8* null, i8** %10
%15 = call i8* @malloc(i32 4096)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %64
20: ; preds = %2
br label %21
21: ; preds = %60, %20
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%23 = load i32*, i32** %4
%24 = call i64 @fgets(i8* %22, i32 4096, i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %61
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = call i32 @memset(i8* %27, i32 0, i32 4096)
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%30 = getelementptr inbounds [13 x i8], [13 x i8]* %12, i64 0, i64 0
%31 = load i8*, i8** %6
%32 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
%33 = getelementptr inbounds [13 x i8], [13 x i8]* %12, i64 0, i64 0
%34 = load i8*, i8** %3
%35 = call i64 @strcmp(i8* %33, i8* %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %60
37: ; preds = %26
%38 = load i8*, i8** %6
%39 = call i8* @strtok_r(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %9)
store i8* %39, i8** %7
br label %40
40: ; preds = %57, %37
%41 = load i8*, i8** %7
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %59
43: ; preds = %40
%44 = load i8*, i8** %7
%45 = call i8* @strtok_r(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %10)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
store i8* %46, i8** %13
%47 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %10)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
store i8* %48, i8** %14
%49 = load i8*, i8** %13
%50 = load i8*, i8** %14
%51 = call i64 @setenv(i8* %49, i8* %50, i32 1)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %43
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %13
%56 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %55)
store i32 0, i32* %5
br label %64
57: ; preds = %43
%58 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %9)
store i8* %58, i8** %7
br label %40
59: ; preds = %40
br label %61
60: ; preds = %26
br label %21
61: ; preds = %59, %21
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
br label %64
64: ; preds = %61, %53, %18
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@.str = [14 x i8] c"malloc string\00"
@.str.1 = [16 x i8] c"%12s\09%4095s[^\0A]\00"
@.str.2 = [2 x i8] c",\00"
@.str.3 = [2 x i8] c"=\00"
@stderr = global i32 0
@.str.4 = [44 x i8] c"setenv: %s: can't set environment variable\0A\00"
define i32 @setup_environment(i8* %0, i32* nocapture %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [4096 x i8]
%6 = alloca [13 x i8]
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [13 x i8], [13 x i8]* %6, i64 0, i64 0
%11 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %40
15: ; preds = %2, %18
%16 = call i64 @fgets(i8* %9, i32 4096, i32* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %38, label %18
18: ; preds = %15
%19 = call i32 @memset(i8* %11, i32 0, i32 4096)
%20 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %11)
%21 = call i64 @strcmp(i8* %10, i8* %0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %15
23: ; preds = %18
%24 = call i8* @strtok_r(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%25 = icmp eq i8* %24, null
br i1 %25, label %38, label %26
26: ; preds = %23, %35
%27 = phi i8* [ %36, %35 ], [ %24, %23 ]
%28 = call i8* @strtok_r(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %4)
%29 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %4)
%30 = call i64 @setenv(i8* %28, i8* %29, i32 1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %28)
br label %40
35: ; preds = %26
%36 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %3)
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %26
38: ; preds = %15, %35, %23
%39 = call i32 @free(i8* %11)
br label %40
40: ; preds = %32, %13, %38
%41 = phi i32 [ 1, %38 ], [ 0, %13 ], [ 0, %32 ]
ret i32 %41
}